Skip to content

Commit

Permalink
Merge pull request #41547 from yrodiere/panache-selection-mutation
Browse files Browse the repository at this point in the history
Use SelectionQuery/MutationQuery wherever possible in Panache + avoid warning HHH000104
  • Loading branch information
gsmet authored Jul 1, 2024
2 parents 322d112 + f6630d1 commit 1df964d
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 183 deletions.
6 changes: 3 additions & 3 deletions docs/src/main/asciidoc/hibernate-orm-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ If you need to mock entity instance methods, such as `persist()` you can do it b
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.hibernate.query.SelectionQuery;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -1259,9 +1259,9 @@ public class PanacheMockingTest {
@BeforeEach
public void setup() {
Query mockQuery = Mockito.mock(Query.class);
SelectionQuery mockQuery = Mockito.mock(SelectionQuery.class);
Mockito.doNothing().when(session).persist(Mockito.any());
Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(mockQuery);
Mockito.when(session.createSelectionQuery(Mockito.anyString(), Mockito.any())).thenReturn(mockQuery);
Mockito.when(mockQuery.getSingleResult()).thenReturn(0l);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

import jakarta.persistence.EntityManager;
import jakarta.persistence.LockModeType;
import jakarta.persistence.Query;
import jakarta.transaction.SystemException;
import jakarta.transaction.TransactionManager;

import org.hibernate.Session;
import org.hibernate.query.CommonQueryContract;
import org.hibernate.query.MutationQuery;
import org.hibernate.query.SelectionQuery;

import io.agroal.api.AgroalDataSource;
import io.quarkus.agroal.DataSource;
Expand Down Expand Up @@ -155,7 +157,7 @@ public static TransactionManager getTransactionManager() {
return Arc.container().instance(TransactionManager.class).get();
}

public static Query bindParameters(Query query, Object[] params) {
public static <T extends CommonQueryContract> T bindParameters(T query, Object[] params) {
if (params == null || params.length == 0)
return query;
for (int i = 0; i < params.length; i++) {
Expand All @@ -164,8 +166,8 @@ public static Query bindParameters(Query query, Object[] params) {
return query;
}

public static Query bindParameters(Query query, Map<String, Object> params) {
if (params == null || params.size() == 0)
public static <T extends CommonQueryContract> T bindParameters(T query, Map<String, Object> params) {
if (params == null || params.isEmpty())
return query;
for (Entry<String, Object> entry : params.entrySet()) {
query.setParameter(entry.getKey(), entry.getValue());
Expand Down Expand Up @@ -327,39 +329,41 @@ public Stream<?> streamAll(Class<?> entityClass, Sort sort) {
}

public long count(Class<?> entityClass) {
return (long) getSession(entityClass)
.createQuery("SELECT COUNT(*) FROM " + PanacheJpaUtil.getEntityName(entityClass))
return getSession(entityClass)
.createSelectionQuery("SELECT COUNT(*) FROM " + PanacheJpaUtil.getEntityName(entityClass), Long.class)
.getSingleResult();
}

public long count(Class<?> entityClass, String panacheQuery, Object... params) {
if (PanacheJpaUtil.isNamedQuery(panacheQuery)) {
Query namedQuery = extractNamedQuery(entityClass, panacheQuery);
SelectionQuery namedQuery = extractNamedSelectionQuery(entityClass, panacheQuery);
return (long) bindParameters(namedQuery, params).getSingleResult();
}

try {
return (long) bindParameters(
return bindParameters(
getSession(entityClass)
.createQuery(PanacheJpaUtil.createCountQuery(entityClass, panacheQuery, paramCount(params))),
.createSelectionQuery(
PanacheJpaUtil.createCountQuery(entityClass, panacheQuery, paramCount(params)), Long.class),
params).getSingleResult();
} catch (IllegalArgumentException x) {
} catch (RuntimeException x) {
throw NamedQueryUtil.checkForNamedQueryMistake(x, panacheQuery);
}
}

public long count(Class<?> entityClass, String panacheQuery, Map<String, Object> params) {
if (PanacheJpaUtil.isNamedQuery(panacheQuery)) {
Query namedQuery = extractNamedQuery(entityClass, panacheQuery);
SelectionQuery namedQuery = extractNamedSelectionQuery(entityClass, panacheQuery);
return (long) bindParameters(namedQuery, params).getSingleResult();
}

try {
return (long) bindParameters(
return bindParameters(
getSession(entityClass)
.createQuery(PanacheJpaUtil.createCountQuery(entityClass, panacheQuery, paramCount(params))),
.createSelectionQuery(
PanacheJpaUtil.createCountQuery(entityClass, panacheQuery, paramCount(params)), Long.class),
params).getSingleResult();
} catch (IllegalArgumentException x) {
} catch (RuntimeException x) {
throw NamedQueryUtil.checkForNamedQueryMistake(x, panacheQuery);
}

Expand All @@ -369,13 +373,23 @@ public long count(Class<?> entityClass, String query, Parameters params) {
return count(entityClass, query, params.map());
}

private Query extractNamedQuery(Class<?> entityClass, String query) {
private SelectionQuery<?> extractNamedSelectionQuery(Class<?> entityClass, String query) {
String namedQueryName = extractNamedQueryName(entityClass, query);
return getSession(entityClass).createNamedSelectionQuery(namedQueryName);
}

private MutationQuery extractNamedMutationQuery(Class<?> entityClass, String query) {
String namedQueryName = extractNamedQueryName(entityClass, query);
return getSession(entityClass).createNamedMutationQuery(namedQueryName);
}

private String extractNamedQueryName(Class<?> entityClass, String query) {
if (!PanacheJpaUtil.isNamedQuery(query))
throw new IllegalArgumentException("Must be a named query!");

String namedQueryName = query.substring(1);
NamedQueryUtil.checkNamedQuery(entityClass, namedQueryName);
return getSession(entityClass).createNamedQuery(namedQueryName);
return namedQueryName;
}

public boolean exists(Class<?> entityClass) {
Expand All @@ -395,7 +409,7 @@ public boolean exists(Class<?> entityClass, String query, Parameters params) {
}

public long deleteAll(Class<?> entityClass) {
return getSession(entityClass).createQuery("DELETE FROM " + PanacheJpaUtil.getEntityName(entityClass))
return getSession(entityClass).createMutationQuery("DELETE FROM " + PanacheJpaUtil.getEntityName(entityClass))
.executeUpdate();
}

Expand All @@ -412,34 +426,33 @@ public boolean deleteById(Class<?> entityClass, Object id) {

public long delete(Class<?> entityClass, String panacheQuery, Object... params) {
if (PanacheJpaUtil.isNamedQuery(panacheQuery)) {
Query namedQuery = extractNamedQuery(entityClass, panacheQuery);
return bindParameters(namedQuery, params).executeUpdate();
return bindParameters(extractNamedMutationQuery(entityClass, panacheQuery), params).executeUpdate();
}

try {
return bindParameters(
getSession(entityClass)
.createQuery(PanacheJpaUtil.createDeleteQuery(entityClass, panacheQuery, paramCount(params))),
getSession(entityClass).createMutationQuery(
PanacheJpaUtil.createDeleteQuery(entityClass, panacheQuery, paramCount(params))),
params)
.executeUpdate();
} catch (IllegalArgumentException x) {
} catch (RuntimeException x) {
throw NamedQueryUtil.checkForNamedQueryMistake(x, panacheQuery);
}
}

public long delete(Class<?> entityClass, String panacheQuery, Map<String, Object> params) {
if (PanacheJpaUtil.isNamedQuery(panacheQuery)) {
Query namedQuery = extractNamedQuery(entityClass, panacheQuery);
return bindParameters(namedQuery, params).executeUpdate();
return bindParameters(extractNamedMutationQuery(entityClass, panacheQuery), params).executeUpdate();
}

try {
return bindParameters(
getSession(entityClass)
.createQuery(PanacheJpaUtil.createDeleteQuery(entityClass, panacheQuery, paramCount(params))),
.createMutationQuery(
PanacheJpaUtil.createDeleteQuery(entityClass, panacheQuery, paramCount(params))),
params)
.executeUpdate();
} catch (IllegalArgumentException x) {
} catch (RuntimeException x) {
throw NamedQueryUtil.checkForNamedQueryMistake(x, panacheQuery);
}
}
Expand All @@ -457,48 +470,42 @@ public static IllegalStateException implementationInjectionMissing() {
* Execute update on default persistence unit
*/
public int executeUpdate(String query, Object... params) {
Query jpaQuery = getSession(DEFAULT_PERSISTENCE_UNIT_NAME).createQuery(query);
bindParameters(jpaQuery, params);
return jpaQuery.executeUpdate();
return bindParameters(getSession(DEFAULT_PERSISTENCE_UNIT_NAME).createMutationQuery(query), params)
.executeUpdate();
}

/**
* Execute update on default persistence unit
*/
public int executeUpdate(String query, Map<String, Object> params) {
Query jpaQuery = getSession(DEFAULT_PERSISTENCE_UNIT_NAME).createQuery(query);
bindParameters(jpaQuery, params);
return jpaQuery.executeUpdate();
return bindParameters(getSession(DEFAULT_PERSISTENCE_UNIT_NAME).createMutationQuery(query), params)
.executeUpdate();
}

public int executeUpdate(Class<?> entityClass, String panacheQuery, Object... params) {
if (PanacheJpaUtil.isNamedQuery(panacheQuery)) {
Query namedQuery = extractNamedQuery(entityClass, panacheQuery);
return bindParameters(namedQuery, params).executeUpdate();
return bindParameters(extractNamedMutationQuery(entityClass, panacheQuery), params).executeUpdate();
}

try {
String updateQuery = PanacheJpaUtil.createUpdateQuery(entityClass, panacheQuery, paramCount(params));
Query jpaQuery = getSession(entityClass).createQuery(updateQuery);
bindParameters(jpaQuery, params);
return jpaQuery.executeUpdate();
} catch (IllegalArgumentException x) {
return bindParameters(getSession(DEFAULT_PERSISTENCE_UNIT_NAME).createMutationQuery(updateQuery), params)
.executeUpdate();
} catch (RuntimeException x) {
throw NamedQueryUtil.checkForNamedQueryMistake(x, panacheQuery);
}
}

public int executeUpdate(Class<?> entityClass, String panacheQuery, Map<String, Object> params) {
if (PanacheJpaUtil.isNamedQuery(panacheQuery)) {
Query namedQuery = extractNamedQuery(entityClass, panacheQuery);
return bindParameters(namedQuery, params).executeUpdate();
return bindParameters(extractNamedMutationQuery(entityClass, panacheQuery), params).executeUpdate();
}

try {
String updateQuery = PanacheJpaUtil.createUpdateQuery(entityClass, panacheQuery, paramCount(params));
Query jpaQuery = getSession(entityClass).createQuery(updateQuery);
bindParameters(jpaQuery, params);
return jpaQuery.executeUpdate();
} catch (IllegalArgumentException x) {
return bindParameters(getSession(entityClass).createMutationQuery(updateQuery), params)
.executeUpdate();
} catch (RuntimeException x) {
throw NamedQueryUtil.checkForNamedQueryMistake(x, panacheQuery);
}
}
Expand Down
Loading

0 comments on commit 1df964d

Please sign in to comment.