diff --git a/hibernate-core/src/main/java/org/hibernate/query/criteria/CriteriaDefinition.java b/hibernate-core/src/main/java/org/hibernate/query/criteria/CriteriaDefinition.java index ee622c6af8..57dfd54e63 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/criteria/CriteriaDefinition.java +++ b/hibernate-core/src/main/java/org/hibernate/query/criteria/CriteriaDefinition.java @@ -10,6 +10,7 @@ import jakarta.persistence.criteria.*; import jakarta.persistence.metamodel.EntityType; import org.hibernate.Incubating; import org.hibernate.Session; +import org.hibernate.SessionFactory; import org.hibernate.query.SelectionQuery; import org.hibernate.query.criteria.spi.HibernateCriteriaBuilderDelegate; import org.hibernate.query.sqm.FetchClauseType; @@ -48,256 +49,258 @@ import java.util.function.Function; */ @Incubating public abstract class CriteriaDefinition - extends HibernateCriteriaBuilderDelegate - implements JpaCriteriaQuery { + extends HibernateCriteriaBuilderDelegate + implements JpaCriteriaQuery { - private final Session session; - private final JpaCriteriaQuery query; + private final JpaCriteriaQuery query; - public CriteriaDefinition(Session session, Class resultType) { - super( session.getSessionFactory().getCriteriaBuilder() ); - this.session = session; - query = createQuery( resultType ); - define(); - } + public CriteriaDefinition(SessionFactory factory, Class resultType) { + super( factory.getCriteriaBuilder() ); + query = createQuery( resultType ); + define(); + } - public SelectionQuery createSelectionQuery() { - return session.createSelectionQuery(query); - } + public CriteriaDefinition(Session session, Class resultType) { + this( session.getSessionFactory(), resultType ); + } - public abstract void define(); + public SelectionQuery createSelectionQuery(Session session) { + return session.createSelectionQuery(query); + } - @Override - public JpaCriteriaQuery select(Selection selection) { - return query.select(selection); - } + public abstract void define(); - @Override - public JpaCriteriaQuery multiselect(Selection... selections) { - return query.multiselect(selections); - } + @Override + public JpaCriteriaQuery select(Selection selection) { + return query.select(selection); + } - @Override - public JpaCriteriaQuery multiselect(List> list) { - return query.multiselect(list); - } + @Override + public JpaCriteriaQuery multiselect(Selection... selections) { + return query.multiselect(selections); + } - @Override - public JpaCriteriaQuery where(Expression restriction) { - return query.where(restriction); - } + @Override + public JpaCriteriaQuery multiselect(List> list) { + return query.multiselect(list); + } - @Override - public JpaCriteriaQuery where(Predicate... restrictions) { - return query.where(restrictions); - } + @Override + public JpaCriteriaQuery where(Expression restriction) { + return query.where(restriction); + } - @Override - public JpaCriteriaQuery groupBy(Expression... grouping) { - return query.groupBy(grouping); - } + @Override + public JpaCriteriaQuery where(Predicate... restrictions) { + return query.where(restrictions); + } - @Override - public JpaCriteriaQuery groupBy(List> grouping) { - return query.groupBy(grouping); - } + @Override + public JpaCriteriaQuery groupBy(Expression... grouping) { + return query.groupBy(grouping); + } - @Override - public JpaCriteriaQuery having(Expression restriction) { - return query.having(restriction); - } + @Override + public JpaCriteriaQuery groupBy(List> grouping) { + return query.groupBy(grouping); + } - @Override - public JpaCriteriaQuery having(Predicate... restrictions) { - return query.having(restrictions); - } + @Override + public JpaCriteriaQuery having(Expression restriction) { + return query.having(restriction); + } - @Override - public JpaCriteriaQuery orderBy(Order... o) { - return query.orderBy(o); - } + @Override + public JpaCriteriaQuery having(Predicate... restrictions) { + return query.having(restrictions); + } - @Override - public JpaCriteriaQuery orderBy(List o) { - return query.orderBy(o); - } + @Override + public JpaCriteriaQuery orderBy(Order... o) { + return query.orderBy(o); + } - @Override - public JpaCriteriaQuery distinct(boolean distinct) { - return query.distinct(distinct); - } + @Override + public JpaCriteriaQuery orderBy(List o) { + return query.orderBy(o); + } - @Override - public List getOrderList() { - return query.getOrderList(); - } + @Override + public JpaCriteriaQuery distinct(boolean distinct) { + return query.distinct(distinct); + } - @Override - public Set> getParameters() { - return query.getParameters(); - } + @Override + public List getOrderList() { + return query.getOrderList(); + } - @Override - public JpaRoot from(Class entityClass) { - return query.from(entityClass); - } + @Override + public Set> getParameters() { + return query.getParameters(); + } - @Override - public JpaRoot from(EntityType entity) { - return query.from(entity); - } + @Override + public JpaRoot from(Class entityClass) { + return query.from(entityClass); + } - @Override - public JpaSubQuery subquery(Class type) { - return query.subquery(type); - } + @Override + public JpaRoot from(EntityType entity) { + return query.from(entity); + } - @Override - public Set> getRoots() { - return query.getRoots(); - } + @Override + public JpaSubQuery subquery(Class type) { + return query.subquery(type); + } - @Override - public JpaSelection getSelection() { - return query.getSelection(); - } + @Override + public Set> getRoots() { + return query.getRoots(); + } - @Override - public List> getGroupList() { - return query.getGroupList(); - } + @Override + public JpaSelection getSelection() { + return query.getSelection(); + } - @Override - public JpaPredicate getGroupRestriction() { - return query.getGroupRestriction(); - } + @Override + public List> getGroupList() { + return query.getGroupList(); + } - @Override - public boolean isDistinct() { - return query.isDistinct(); - } + @Override + public JpaPredicate getGroupRestriction() { + return query.getGroupRestriction(); + } - @Override - public Class getResultType() { - return query.getResultType(); - } + @Override + public boolean isDistinct() { + return query.isDistinct(); + } - @Override - public JpaPredicate getRestriction() { - return query.getRestriction(); - } + @Override + public Class getResultType() { + return query.getResultType(); + } - @Override - public JpaExpression getOffset() { - return query.getOffset(); - } + @Override + public JpaPredicate getRestriction() { + return query.getRestriction(); + } - @Override - public JpaCriteriaQuery offset(JpaExpression offset) { - return query.offset(offset); - } + @Override + public JpaExpression getOffset() { + return query.getOffset(); + } - @Override - public JpaCriteriaQuery offset(Number offset) { - return query.offset(offset); - } + @Override + public JpaCriteriaQuery offset(JpaExpression offset) { + return query.offset(offset); + } - @Override - public JpaExpression getFetch() { - return query.getFetch(); - } + @Override + public JpaCriteriaQuery offset(Number offset) { + return query.offset(offset); + } - @Override - public JpaCriteriaQuery fetch(JpaExpression fetch) { - return query.fetch(fetch); - } + @Override + public JpaExpression getFetch() { + return query.getFetch(); + } - @Override - public JpaCriteriaQuery fetch(JpaExpression fetch, FetchClauseType fetchClauseType) { - return query.fetch(fetch, fetchClauseType); - } + @Override + public JpaCriteriaQuery fetch(JpaExpression fetch) { + return query.fetch(fetch); + } - @Override - public JpaCriteriaQuery fetch(Number fetch) { - return query.fetch(fetch); - } + @Override + public JpaCriteriaQuery fetch(JpaExpression fetch, FetchClauseType fetchClauseType) { + return query.fetch(fetch, fetchClauseType); + } - @Override - public JpaCriteriaQuery fetch(Number fetch, FetchClauseType fetchClauseType) { - return query.fetch(fetch, fetchClauseType); - } + @Override + public JpaCriteriaQuery fetch(Number fetch) { + return query.fetch(fetch); + } - @Override - public FetchClauseType getFetchClauseType() { - return query.getFetchClauseType(); - } + @Override + public JpaCriteriaQuery fetch(Number fetch, FetchClauseType fetchClauseType) { + return query.fetch(fetch, fetchClauseType); + } - @Override - public List> getRootList() { - return query.getRootList(); - } + @Override + public FetchClauseType getFetchClauseType() { + return query.getFetchClauseType(); + } - @Override - public Collection> getCteCriterias() { - return query.getCteCriterias(); - } + @Override + public List> getRootList() { + return query.getRootList(); + } - @Override - public JpaCteCriteria getCteCriteria(String cteName) { - return query.getCteCriteria(cteName); - } + @Override + public Collection> getCteCriterias() { + return query.getCteCriterias(); + } - @Override - public JpaCteCriteria with(AbstractQuery criteria) { - return query.with(criteria); - } + @Override + public JpaCteCriteria getCteCriteria(String cteName) { + return query.getCteCriteria(cteName); + } - @Override - public JpaCteCriteria withRecursiveUnionAll(AbstractQuery baseCriteria, Function, AbstractQuery> recursiveCriteriaProducer) { - return query.withRecursiveUnionAll(baseCriteria, recursiveCriteriaProducer); - } + @Override + public JpaCteCriteria with(AbstractQuery criteria) { + return query.with(criteria); + } - @Override - public JpaCteCriteria withRecursiveUnionDistinct(AbstractQuery baseCriteria, Function, AbstractQuery> recursiveCriteriaProducer) { - return query.withRecursiveUnionDistinct(baseCriteria, recursiveCriteriaProducer); - } + @Override + public JpaCteCriteria withRecursiveUnionAll(AbstractQuery baseCriteria, Function, AbstractQuery> recursiveCriteriaProducer) { + return query.withRecursiveUnionAll(baseCriteria, recursiveCriteriaProducer); + } - @Override - public JpaCteCriteria with(String name, AbstractQuery criteria) { - return query.with(name, criteria); - } + @Override + public JpaCteCriteria withRecursiveUnionDistinct(AbstractQuery baseCriteria, Function, AbstractQuery> recursiveCriteriaProducer) { + return query.withRecursiveUnionDistinct(baseCriteria, recursiveCriteriaProducer); + } - @Override - public JpaCteCriteria withRecursiveUnionAll( - String name, AbstractQuery baseCriteria, - Function, AbstractQuery> recursiveCriteriaProducer) { - return query.withRecursiveUnionAll(name, baseCriteria, recursiveCriteriaProducer); - } + @Override + public JpaCteCriteria with(String name, AbstractQuery criteria) { + return query.with(name, criteria); + } - @Override - public JpaCteCriteria withRecursiveUnionDistinct( - String name, AbstractQuery baseCriteria, - Function, AbstractQuery> recursiveCriteriaProducer) { - return query.withRecursiveUnionDistinct(name, baseCriteria, recursiveCriteriaProducer); - } + @Override + public JpaCteCriteria withRecursiveUnionAll( + String name, AbstractQuery baseCriteria, + Function, AbstractQuery> recursiveCriteriaProducer) { + return query.withRecursiveUnionAll(name, baseCriteria, recursiveCriteriaProducer); + } - @Override - public JpaQueryStructure getQuerySpec() { - return query.getQuerySpec(); - } + @Override + public JpaCteCriteria withRecursiveUnionDistinct( + String name, AbstractQuery baseCriteria, + Function, AbstractQuery> recursiveCriteriaProducer) { + return query.withRecursiveUnionDistinct(name, baseCriteria, recursiveCriteriaProducer); + } - @Override - public JpaQueryPart getQueryPart() { - return query.getQueryPart(); - } + @Override + public JpaQueryStructure getQuerySpec() { + return query.getQuerySpec(); + } - @Override - public JpaDerivedRoot from(Subquery subquery) { - return query.from(subquery); - } + @Override + public JpaQueryPart getQueryPart() { + return query.getQueryPart(); + } - @Override - public JpaRoot from(JpaCteCriteria cte) { - return query.from(cte); - } + @Override + public JpaDerivedRoot from(Subquery subquery) { + return query.from(subquery); + } + + @Override + public JpaRoot from(JpaCteCriteria cte) { + return query.from(cte); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/query/CriteriaDefinitionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/query/CriteriaDefinitionTest.java index 61b236df32..11f4b73453 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/query/CriteriaDefinitionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/query/CriteriaDefinitionTest.java @@ -2,6 +2,7 @@ package org.hibernate.orm.test.jpa.criteria.query; import jakarta.persistence.Entity; import jakarta.persistence.Id; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.query.criteria.CriteriaDefinition; import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.SessionFactory; @@ -21,29 +22,33 @@ public class CriteriaDefinitionTest { s.persist( new Message(2L, "bye") ); }); + SessionFactoryImplementor sessionFactory = scope.getSessionFactory(); + + var query1 = new CriteriaDefinition<>(sessionFactory, Object[].class) { + public void define() { + var m = from(Message.class); + select(array(m.get("id"), m.get("text"))); + where(like(m.get("text"), "hell%"), m.get("id").equalTo(1)); + orderBy(asc(m.get("id"))); + } + }; + + var query2 = new CriteriaDefinition<>(sessionFactory, Message.class) { + public void define() { + var m = from(Message.class); + where(like(m.get("text"), "hell%"), m.get("id").equalTo(1)); + orderBy(asc(m.get("id"))); + } + }; + scope.inSession(session -> { - var idAndText = new CriteriaDefinition<>(session, Object[].class) { - public void define() { - var m = from(Message.class); - select(array(m.get("id"), m.get("text"))); - where(like(m.get("text"), "hell%"), m.get("id").equalTo(1)); - orderBy(asc(m.get("id"))); - } - }.createSelectionQuery() - .getSingleResult(); + var idAndText = query1.createSelectionQuery(session).getSingleResult(); assertNotNull(idAndText); assertEquals(1L,idAndText[0]); assertEquals("hello",idAndText[1]); - var message = new CriteriaDefinition<>(session, Message.class) { - public void define() { - var m = from(Message.class); - where(like(m.get("text"), "hell%"), m.get("id").equalTo(1)); - orderBy(asc(m.get("id"))); - } - }.createSelectionQuery() - .getSingleResult(); + var message = query2.createSelectionQuery(session).getSingleResult(); assertNotNull(message); assertEquals(1L,message.id);