Skip to content

Commit

Permalink
Fix code to work with hibernate 7 and eclipselink 5
Browse files Browse the repository at this point in the history
  • Loading branch information
velo committed Sep 2, 2024
1 parent 37c744a commit de009aa
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public class TemplateFactory {
private static final Pattern elementPattern =
Pattern.compile(
"""
\\{\
(%?%?)\
(\\d+)\
(?:([+-/*])(?:(\\d+)|'(-?\\d+(?:\\.\\d+)?)'))?\
([slu%]?%?)\
\\}""");
\\{\
(%?%?)\
(\\d+)\
(?:([+-/*])(?:(\\d+)|'(-?\\d+(?:\\.\\d+)?)'))?\
([slu%]?%?)\
\\}\
""");

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

Expand Down
7 changes: 7 additions & 0 deletions querydsl-libraries/querydsl-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-community-dialects</artifactId>
<version>${hibernate.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>jakarta.persistence</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,17 +518,18 @@ protected void visitOperation(
throw new IllegalArgumentException(
String.format(
"""
SQL Expressions like %s are not supported in JPQL - the query language for JPA.\
SQLExpressions.* can only be used in JPQL queries when these functions\
are registered as custom function in your ORM.%n To fix this issue, you\
have three options:%n 1) If you do want to use advanced, dialect\
specific, SQL functions within JPQL, make sure to make these functions\
available to your ORM through custom functions and register these with\
your JPATemplates instance.%n 2) Use JPASQLQuery instead. This allows\
you to generate a pure SQL query based on your JPA metamodel.%n 3)\
Consider using the Blaze-Persistence QueryDSL integration.\
Blaze-Persistence is an extension on top of JPA that makes various SQL\
specific functions like window functions available to JPQL.""",
SQL Expressions like %s are not supported in JPQL - the query language for JPA.\
SQLExpressions.* can only be used in JPQL queries when these functions\
are registered as custom function in your ORM.%n To fix this issue, you\
have three options:%n 1) If you do want to use advanced, dialect\
specific, SQL functions within JPQL, make sure to make these functions\
available to your ORM through custom functions and register these with\
your JPATemplates instance.%n 2) Use JPASQLQuery instead. This allows\
you to generate a pure SQL query based on your JPA metamodel.%n 3)\
Consider using the Blaze-Persistence QueryDSL integration.\
Blaze-Persistence is an extension on top of JPA that makes various SQL\
specific functions like window functions available to JPQL.\
""",
operator.name()),
e);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ public long fetchCount() {
try {
if (getMetadata().getGroupBy().size() > 1 || getMetadata().getHaving() != null) {
logger.warning(
"Fetchable#fetchCount() was computed in memory! See the Javadoc for"
+ " AbstractJPAQuery#fetchCount for more details.");
"""
Fetchable#fetchCount() was computed in memory! See the Javadoc for\
AbstractJPAQuery#fetchCount for more details.\
""");
var query = createQuery(null, false);
return query.getResultList().size();
}
Expand Down Expand Up @@ -286,8 +288,10 @@ public QueryResults<T> fetchResults() {
var modifiers = getMetadata().getModifiers();
if (getMetadata().getGroupBy().size() > 1 || getMetadata().getHaving() != null) {
logger.warning(
"Fetchable#fetchResults() was computed in memory! See the Javadoc for"
+ " AbstractJPAQuery#fetchResults for more details.");
"""
Fetchable#fetchResults() was computed in memory! See the Javadoc for\
AbstractJPAQuery#fetchResults for more details.\
""");
var query = createQuery(null, false);
@SuppressWarnings("unchecked")
List<T> resultList = query.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.querydsl.core.types.Ops;
import com.querydsl.sql.DerbyTemplates;
import org.hibernate.boot.model.FunctionContributions;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.community.dialect.DerbyDialect;

/** {@code QDerbyDialect} extends {@code DerbyDialect} with additional functions */
public class QDerbyDialect extends DerbyDialect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public void aggregates_uniqueResult_min() {
}

@Test
@NoEclipseLink
public void alias() {
assertThat(query().from(cat).select(cat.id.as(cat.id)).fetch()).hasSize(6);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void setSession(Session session) {

@Override
protected void save(Object entity) {
session.save(entity);
session.persist(entity);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public void setSession(Session session) {
@Before
public void setUp() {
if (query().from(cat).fetchCount() == 0) {
session.save(new Cat("Beck", 1, Color.BLACK));
session.save(new Cat("Kate", 2, Color.BLACK));
session.save(new Cat("Kitty", 3, Color.BLACK));
session.save(new Cat("Bobby", 4, Color.BLACK));
session.save(new Cat("Harold", 5, Color.BLACK));
session.save(new Cat("Tim", 6, Color.BLACK));
session.persist(new Cat("Beck", 1, Color.BLACK));
session.persist(new Cat("Kate", 2, Color.BLACK));
session.persist(new Cat("Kitty", 3, Color.BLACK));
session.persist(new Cat("Bobby", 4, Color.BLACK));
session.persist(new Cat("Harold", 5, Color.BLACK));
session.persist(new Cat("Tim", 6, Color.BLACK));
session.flush();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ private HibernateInsertClause insert(EntityPath<?> entity) {

@Test
public void scroll() {
session.save(new Cat("Bob", 10));
session.save(new Cat("Steve", 11));
session.persist(new Cat("Bob", 10));
session.persist(new Cat("Steve", 11));

var cat = QCat.cat;
HibernateQuery<?> query = new HibernateQuery<Void>(session);
Expand All @@ -112,7 +112,7 @@ public void scroll() {

@Test
public void insert() {
session.save(new Cat("Bob", 10));
session.persist(new Cat("Bob", 10));

var cat = QCat.cat;
var amount = insert(cat).set(cat.name, "Bobby").set(cat.alive, false).execute();
Expand All @@ -123,7 +123,7 @@ public void insert() {

@Test
public void insert2() {
session.save(new Cat("Bob", 10));
session.persist(new Cat("Bob", 10));

var cat = QCat.cat;
var amount = insert(cat).columns(cat.name, cat.alive).values("Bobby", false).execute();
Expand All @@ -134,7 +134,7 @@ public void insert2() {

@Test
public void insert3() {
session.save(new Cat("Bob", 10));
session.persist(new Cat("Bob", 10));

var cat = QCat.cat;
var bob = new QCat("Bob");
Expand All @@ -151,8 +151,8 @@ public void insert3() {

@Test
public void update() {
session.save(new Cat("Bob", 10));
session.save(new Cat("Steve", 11));
session.persist(new Cat("Bob", 10));
session.persist(new Cat("Steve", 11));

var cat = QCat.cat;
var amount =
Expand All @@ -168,8 +168,8 @@ public void update() {

@Test
public void update_with_null() {
session.save(new Cat("Bob", 10));
session.save(new Cat("Steve", 11));
session.persist(new Cat("Bob", 10));
session.persist(new Cat("Steve", 11));

var cat = QCat.cat;
var amount =
Expand All @@ -183,8 +183,8 @@ public void update_with_null() {

@Test
public void delete() {
session.save(new Cat("Bob", 10));
session.save(new Cat("Steve", 11));
session.persist(new Cat("Bob", 10));
session.persist(new Cat("Steve", 11));

var cat = QCat.cat;
var amount = delete(cat).where(cat.name.eq("Bob")).execute();
Expand All @@ -195,7 +195,7 @@ public void delete() {
public void collection() throws Exception {
List<Cat> cats = Arrays.asList(new Cat("Bob", 10), new Cat("Steve", 11));
for (Cat cat : cats) {
session.save(cat);
session.persist(cat);
}

query().from(cat).innerJoin(cat.kittens, kitten).where(kitten.in(cats)).parse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class UniqueResultsTest implements HibernateTest {

@Test
public void test() {
session.save(new Cat("Bob1", 1));
session.save(new Cat("Bob2", 2));
session.save(new Cat("Bob3", 3));
session.persist(new Cat("Bob1", 1));
session.persist(new Cat("Bob2", 2));
session.persist(new Cat("Bob3", 3));

assertThat(
query()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public abstract class AbstractR2DBCDeleteClause<C extends AbstractR2DBCDeleteCla
protected static final ValidatingVisitor validatingVisitor =
new ValidatingVisitor(
"""
Undeclared path '%s'. \
A delete operation can only reference a single table. \
Consider this alternative: DELETE ... WHERE EXISTS (subquery)""");
Undeclared path '%s'. \
A delete operation can only reference a single table. \
Consider this alternative: DELETE ... WHERE EXISTS (subquery)\
""");

protected final RelationalPath<?> entity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ public SQLiteTemplates(char escape, boolean quote) {

add(
Ops.DateTimeOps.YEAR_MONTH,
"cast(strftime('%Y',{0} / 1000, 'unixepoch', 'localtime') * 100 + strftime('%m',{0} / 1000,"
+ " 'unixepoch', 'localtime') as integer)");
"""
cast(strftime('%Y',{0} / 1000, 'unixepoch', 'localtime') * 100 + strftime('%m',{0} / 1000,\
'unixepoch', 'localtime') as integer)\
""");
add(
Ops.DateTimeOps.YEAR_WEEK,
"cast(strftime('%Y%W',{0} / 1000, 'unixepoch', 'localtime') as integer)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public abstract class AbstractSQLDeleteClause<C extends AbstractSQLDeleteClause<
protected static final ValidatingVisitor validatingVisitor =
new ValidatingVisitor(
"""
Undeclared path '%s'. \
A delete operation can only reference a single table. \
Consider this alternative: DELETE ... WHERE EXISTS (subquery)""");
Undeclared path '%s'. \
A delete operation can only reference a single table. \
Consider this alternative: DELETE ... WHERE EXISTS (subquery)\
""");

protected final RelationalPath<?> entity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public HibernateConfiguration(
protected List<Class<? extends Annotation>> getAnnotations() {
try {
List<Class<? extends Annotation>> annotations = new ArrayList<>(super.getAnnotations());
for (String simpleName : Arrays.asList("Type", "Cascade", "LazyCollection", "OnDelete")) {
for (String simpleName : Arrays.asList("Type", "Cascade", "OnDelete")) {
annotations.add(
(Class<? extends Annotation>) Class.forName("org.hibernate.annotations." + simpleName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public void setId(Long id) {

@Entity
@Table(name = "USERS")
// @org.hibernate.annotations.AccessType("field")
@org.hibernate.annotations.Proxy(proxyClass = User.class)
public static class UserImpl extends EntityImpl implements User {

@NaturalId(mutable = true)
Expand Down Expand Up @@ -85,8 +83,6 @@ public void setParty(Party party) {

@jakarta.persistence.Entity
@Table(name = "PARTY")
// @org.hibernate.annotations.AccessType("field")
@org.hibernate.annotations.Proxy(proxyClass = Party.class)
public abstract static class PartyImpl extends EntityImpl implements Party {

@Column(name = "NAME", nullable = false)
Expand Down

0 comments on commit de009aa

Please sign in to comment.