Skip to content

Commit d4d1433

Browse files
committed
Fix code to work with hibernate 7 and eclipselink 5
1 parent 2b50ff2 commit d4d1433

File tree

18 files changed

+84
-64
lines changed

18 files changed

+84
-64
lines changed

querydsl-libraries/querydsl-core/src/main/java/com/querydsl/core/types/Template.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,19 @@ public Object convert(List<?> args) {
223223
Expression<?> expr1 = asExpression(arg1);
224224
Expression<?> expr2 = asExpression(arg2);
225225

226-
if (arg2 instanceof Number) {
226+
if (arg2 instanceof Number number) {
227227
if (CONVERTIBLES.contains(operator)
228228
&& expr1 instanceof com.querydsl.core.types.Operation<?> operation) {
229229
if (CONVERTIBLES.contains(operation.getOperator())
230230
&& operation.getArg(1) instanceof Constant) {
231231
var num1 = ((Constant<Number>) operation.getArg(1)).getConstant();
232232
Number num2;
233233
if (operator == operation.getOperator()) {
234-
num2 = MathUtils.result(num1, (Number) arg2, Ops.ADD);
234+
num2 = MathUtils.result(num1, number, Ops.ADD);
235235
} else if (operator == Ops.ADD) {
236-
num2 = MathUtils.result((Number) arg2, num1, Ops.SUB);
236+
num2 = MathUtils.result(number, num1, Ops.SUB);
237237
} else {
238-
num2 = MathUtils.result(num1, (Number) arg2, Ops.SUB);
238+
num2 = MathUtils.result(num1, number, Ops.SUB);
239239
}
240240
return ExpressionUtils.operation(
241241
expr1.getType(), operator, operation.getArg(0), Expressions.constant(num2));

querydsl-libraries/querydsl-core/src/main/java/com/querydsl/core/types/TemplateFactory.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ public class TemplateFactory {
5050

5151
private static final Pattern elementPattern =
5252
Pattern.compile(
53-
"\\{"
54-
+ "(%?%?)"
55-
+ "(\\d+)"
56-
+ "(?:([+-/*])(?:(\\d+)|'(-?\\d+(?:\\.\\d+)?)'))?"
57-
+ "([slu%]?%?)"
58-
+ "\\}");
53+
"""
54+
\\{\
55+
(%?%?)\
56+
(\\d+)\
57+
(?:([+-/*])(?:(\\d+)|'(-?\\d+(?:\\.\\d+)?)'))?\
58+
([slu%]?%?)\
59+
\\}\
60+
""");
5961

6062
private final Map<String, Template> cache = Collections.synchronizedMap(new WeakHashMap<>());
6163

querydsl-libraries/querydsl-core/src/main/java/com/querydsl/core/types/dsl/CaseBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ public Initial(Predicate b) {
158158

159159
@SuppressWarnings("unchecked")
160160
public <A> Cases<A, SimpleExpression<A>> then(Expression<A> expr) {
161-
if (expr instanceof Predicate) {
162-
return (Cases) then((Predicate) expr);
161+
if (expr instanceof Predicate predicate) {
162+
return (Cases) then(predicate);
163163
} else if (expr instanceof StringExpression se) {
164164
return (Cases) then(se);
165165
} else if (expr instanceof NumberExpression ne) {

querydsl-libraries/querydsl-jpa/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
</exclusion>
3636
</exclusions>
3737
</dependency>
38+
<dependency>
39+
<groupId>org.hibernate.orm</groupId>
40+
<artifactId>hibernate-community-dialects</artifactId>
41+
<version>${hibernate.version}</version>
42+
<scope>provided</scope>
43+
<optional>true</optional>
44+
</dependency>
3845

3946
<dependency>
4047
<groupId>jakarta.persistence</groupId>

querydsl-libraries/querydsl-jpa/src/main/java/com/querydsl/jpa/JPQLSerializer.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -517,17 +517,19 @@ protected void visitOperation(
517517
if (operator.getClass().getName().endsWith("SQLOps")) {
518518
throw new IllegalArgumentException(
519519
String.format(
520-
"SQL Expressions like %s are not supported in JPQL - the query language for JPA."
521-
+ " SQLExpressions.* can only be used in JPQL queries when these functions"
522-
+ " are registered as custom function in your ORM.%n\tTo fix this issue, you"
523-
+ " have three options:%n\t1) If you do want to use advanced, dialect"
524-
+ " specific, SQL functions within JPQL, make sure to make these functions"
525-
+ " available to your ORM through custom functions and register these with"
526-
+ " your JPATemplates instance.%n\t2) Use JPASQLQuery instead. This allows"
527-
+ " you to generate a pure SQL query based on your JPA metamodel.%n\t3)"
528-
+ " Consider using the Blaze-Persistence QueryDSL integration."
529-
+ " Blaze-Persistence is an extension on top of JPA that makes various SQL"
530-
+ " specific functions like window functions available to JPQL.",
520+
"""
521+
SQL Expressions like %s are not supported in JPQL - the query language for JPA.\
522+
SQLExpressions.* can only be used in JPQL queries when these functions\
523+
are registered as custom function in your ORM.%n To fix this issue, you\
524+
have three options:%n 1) If you do want to use advanced, dialect\
525+
specific, SQL functions within JPQL, make sure to make these functions\
526+
available to your ORM through custom functions and register these with\
527+
your JPATemplates instance.%n 2) Use JPASQLQuery instead. This allows\
528+
you to generate a pure SQL query based on your JPA metamodel.%n 3)\
529+
Consider using the Blaze-Persistence QueryDSL integration.\
530+
Blaze-Persistence is an extension on top of JPA that makes various SQL\
531+
specific functions like window functions available to JPQL.\
532+
""",
531533
operator.name()),
532534
e);
533535
} else {

querydsl-libraries/querydsl-jpa/src/main/java/com/querydsl/jpa/impl/AbstractJPAQuery.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ public long fetchCount() {
107107
try {
108108
if (getMetadata().getGroupBy().size() > 1 || getMetadata().getHaving() != null) {
109109
logger.warning(
110-
"Fetchable#fetchCount() was computed in memory! See the Javadoc for"
111-
+ " AbstractJPAQuery#fetchCount for more details.");
110+
"""
111+
Fetchable#fetchCount() was computed in memory! See the Javadoc for\
112+
AbstractJPAQuery#fetchCount for more details.\
113+
""");
112114
var query = createQuery(null, false);
113115
return query.getResultList().size();
114116
}
@@ -286,8 +288,10 @@ public QueryResults<T> fetchResults() {
286288
var modifiers = getMetadata().getModifiers();
287289
if (getMetadata().getGroupBy().size() > 1 || getMetadata().getHaving() != null) {
288290
logger.warning(
289-
"Fetchable#fetchResults() was computed in memory! See the Javadoc for"
290-
+ " AbstractJPAQuery#fetchResults for more details.");
291+
"""
292+
Fetchable#fetchResults() was computed in memory! See the Javadoc for\
293+
AbstractJPAQuery#fetchResults for more details.\
294+
""");
291295
var query = createQuery(null, false);
292296
@SuppressWarnings("unchecked")
293297
List<T> resultList = query.getResultList();

querydsl-libraries/querydsl-jpa/src/main/java/com/querydsl/jpa/support/QDerbyDialect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import com.querydsl.core.types.Ops;
1717
import com.querydsl.sql.DerbyTemplates;
1818
import org.hibernate.boot.model.FunctionContributions;
19-
import org.hibernate.dialect.DerbyDialect;
19+
import org.hibernate.community.dialect.DerbyDialect;
2020

2121
/** {@code QDerbyDialect} extends {@code DerbyDialect} with additional functions */
2222
public class QDerbyDialect extends DerbyDialect {

querydsl-libraries/querydsl-jpa/src/test/java/com/querydsl/jpa/AbstractJPATest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ public void aggregates_uniqueResult_min() {
283283
}
284284

285285
@Test
286+
@NoEclipseLink
286287
public void alias() {
287288
assertThat(query().from(cat).select(cat.id.as(cat.id)).fetch()).hasSize(6);
288289
}

querydsl-libraries/querydsl-jpa/src/test/java/com/querydsl/jpa/HibernateBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void setSession(Session session) {
7777

7878
@Override
7979
protected void save(Object entity) {
80-
session.save(entity);
80+
session.persist(entity);
8181
}
8282

8383
@Test

querydsl-libraries/querydsl-jpa/src/test/java/com/querydsl/jpa/HibernateSQLBase.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public void setSession(Session session) {
5656
@Before
5757
public void setUp() {
5858
if (query().from(cat).fetchCount() == 0) {
59-
session.save(new Cat("Beck", 1, Color.BLACK));
60-
session.save(new Cat("Kate", 2, Color.BLACK));
61-
session.save(new Cat("Kitty", 3, Color.BLACK));
62-
session.save(new Cat("Bobby", 4, Color.BLACK));
63-
session.save(new Cat("Harold", 5, Color.BLACK));
64-
session.save(new Cat("Tim", 6, Color.BLACK));
59+
session.persist(new Cat("Beck", 1, Color.BLACK));
60+
session.persist(new Cat("Kate", 2, Color.BLACK));
61+
session.persist(new Cat("Kitty", 3, Color.BLACK));
62+
session.persist(new Cat("Bobby", 4, Color.BLACK));
63+
session.persist(new Cat("Harold", 5, Color.BLACK));
64+
session.persist(new Cat("Tim", 6, Color.BLACK));
6565
session.flush();
6666
}
6767
}

0 commit comments

Comments
 (0)