6 - SQM based on JPA type system
This commit is contained in:
parent
ff1650c66c
commit
9b586aec45
|
@ -6,25 +6,21 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.jpa.test.criteria.components;
|
package org.hibernate.jpa.test.criteria.components;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.Query;
|
|
||||||
import javax.persistence.TypedQuery;
|
import javax.persistence.TypedQuery;
|
||||||
import javax.persistence.criteria.CriteriaBuilder;
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
import javax.persistence.criteria.CriteriaQuery;
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
import javax.persistence.criteria.Fetch;
|
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import org.hibernate.Hibernate;
|
import org.hibernate.Hibernate;
|
||||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,107 +34,90 @@ public class ComponentCriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmbeddableInPath() {
|
public void testEmbeddableInPath() {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
|
||||||
em.getTransaction().begin();
|
|
||||||
Client client = new Client( 111, "steve", "ebersole" );
|
Client client = new Client( 111, "steve", "ebersole" );
|
||||||
|
doInJPA( this::entityManagerFactory, em -> {
|
||||||
em.persist( client );
|
em.persist( client );
|
||||||
em.getTransaction().commit();
|
} );
|
||||||
em.close();
|
|
||||||
|
|
||||||
em = getOrCreateEntityManager();
|
doInJPA( this::entityManagerFactory, em -> {
|
||||||
em.getTransaction().begin();
|
|
||||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
CriteriaQuery<Client> cq = cb.createQuery( Client.class );
|
CriteriaQuery<Client> cq = cb.createQuery( Client.class );
|
||||||
Root<Client> root = cq.from( Client.class );
|
Root<Client> root = cq.from( Client.class );
|
||||||
cq.where( cb.equal( root.get( "name" ).get( "firstName" ), client.getName().getFirstName() ) );
|
cq.where( cb.equal( root.get( "name" ).get( "firstName" ), client.getName().getFirstName() ) );
|
||||||
List<Client> list = em.createQuery( cq ).getResultList();
|
List<Client> list = em.createQuery( cq ).getResultList();
|
||||||
Assert.assertEquals( 1, list.size() );
|
Assert.assertEquals( 1, list.size() );
|
||||||
em.getTransaction().commit();
|
} );
|
||||||
em.close();
|
|
||||||
|
|
||||||
// HHH-5792
|
// HHH-5792
|
||||||
em = getOrCreateEntityManager();
|
doInJPA( this::entityManagerFactory, em -> {
|
||||||
em.getTransaction().begin();
|
|
||||||
TypedQuery<Client> q = em.createQuery(
|
TypedQuery<Client> q = em.createQuery(
|
||||||
"SELECT c FROM Client c JOIN c.name n WHERE n.firstName = '"
|
"SELECT c FROM Client c JOIN c.name n WHERE n.firstName = '"
|
||||||
+ client.getName().getFirstName() + "'",
|
+ client.getName().getFirstName() + "'",
|
||||||
Client.class );
|
Client.class
|
||||||
|
);
|
||||||
Assert.assertEquals( 1, q.getResultList().size() );
|
Assert.assertEquals( 1, q.getResultList().size() );
|
||||||
em.getTransaction().commit();
|
} );
|
||||||
em.close();
|
|
||||||
|
|
||||||
em = getOrCreateEntityManager();
|
doInJPA( this::entityManagerFactory, em -> {
|
||||||
em.getTransaction().begin();
|
|
||||||
em.createQuery( "delete Client" ).executeUpdate();
|
em.createQuery( "delete Client" ).executeUpdate();
|
||||||
em.getTransaction().commit();
|
} );
|
||||||
em.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue(jiraKey = "HHH-9642")
|
@TestForIssue(jiraKey = "HHH-9642")
|
||||||
public void testOneToManyJoinFetchedInEmbeddable() {
|
public void testOneToManyJoinFetchedInEmbeddable() {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
|
||||||
em.getTransaction().begin();
|
|
||||||
Client client = new Client( 111, "steve", "ebersole" );
|
Client client = new Client( 111, "steve", "ebersole" );
|
||||||
Alias alias = new Alias( "a", "guy", "work" );
|
Alias alias = new Alias( "a", "guy", "work" );
|
||||||
client.getName().getAliases().add( alias );
|
client.getName().getAliases().add( alias );
|
||||||
|
doInJPA( this::entityManagerFactory, em -> {
|
||||||
em.persist( client );
|
em.persist( client );
|
||||||
em.getTransaction().commit();
|
} );
|
||||||
em.close();
|
|
||||||
|
|
||||||
em = getOrCreateEntityManager();
|
List<Client> list = new ArrayList<>();
|
||||||
em.getTransaction().begin();
|
doInJPA( this::entityManagerFactory, em -> {
|
||||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
CriteriaQuery<Client> cq = cb.createQuery( Client.class );
|
CriteriaQuery<Client> cq = cb.createQuery( Client.class );
|
||||||
Root<Client> root = cq.from( Client.class );
|
Root<Client> root = cq.from( Client.class );
|
||||||
root.fetch( Client_.name ).fetch( Name_.aliases );
|
root.fetch( Client_.name ).fetch( Name_.aliases );
|
||||||
cq.where( cb.equal( root.get( "name" ).get( "firstName" ), client.getName().getFirstName() ) );
|
cq.where( cb.equal( root.get( "name" ).get( "firstName" ), client.getName().getFirstName() ) );
|
||||||
List<Client> list = em.createQuery(cq).getResultList();
|
list.addAll( em.createQuery( cq ).getResultList() );
|
||||||
Assert.assertEquals( 1, list.size() );
|
Assert.assertEquals( 1, list.size() );
|
||||||
client = list.get( 0 );
|
Client c = list.get( 0 );
|
||||||
assertTrue( Hibernate.isInitialized( client.getName().getAliases() ) );
|
assertTrue( Hibernate.isInitialized( c.getName().getAliases() ) );
|
||||||
em.getTransaction().commit();
|
} );
|
||||||
em.close();
|
|
||||||
|
|
||||||
em = getOrCreateEntityManager();
|
doInJPA( this::entityManagerFactory, em -> {
|
||||||
em.getTransaction().begin();
|
|
||||||
TypedQuery<Client> q = em.createQuery(
|
TypedQuery<Client> q = em.createQuery(
|
||||||
"SELECT c FROM Client c JOIN FETCH c.name.aliases WHERE c.name.firstName = '"
|
"SELECT c FROM Client c JOIN FETCH c.name.aliases WHERE c.name.firstName = '"
|
||||||
+ client.getName().getFirstName() + "'",
|
+ client.getName().getFirstName() + "'",
|
||||||
Client.class
|
Client.class
|
||||||
);
|
);
|
||||||
Assert.assertEquals( 1, q.getResultList().size() );
|
Assert.assertEquals( 1, q.getResultList().size() );
|
||||||
client = list.get( 0 );
|
Client c = list.get( 0 );
|
||||||
assertTrue( Hibernate.isInitialized( client.getName().getAliases() ) );
|
assertTrue( Hibernate.isInitialized( c.getName().getAliases() ) );
|
||||||
em.getTransaction().commit();
|
} );
|
||||||
em.close();
|
|
||||||
|
|
||||||
em = getOrCreateEntityManager();
|
doInJPA( this::entityManagerFactory, em -> {
|
||||||
em.getTransaction().begin();
|
TypedQuery<Client> q = em.createQuery(
|
||||||
q = em.createQuery(
|
|
||||||
"SELECT c FROM Client c JOIN c.name n join FETCH n.aliases WHERE c.name.firstName = '"
|
"SELECT c FROM Client c JOIN c.name n join FETCH n.aliases WHERE c.name.firstName = '"
|
||||||
+ client.getName().getFirstName() + "'",
|
+ client.getName().getFirstName() + "'",
|
||||||
Client.class
|
Client.class
|
||||||
);
|
);
|
||||||
Assert.assertEquals( 1, q.getResultList().size() );
|
Assert.assertEquals( 1, q.getResultList().size() );
|
||||||
client = list.get( 0 );
|
Client c = list.get( 0 );
|
||||||
assertTrue( Hibernate.isInitialized( client.getName().getAliases() ) );
|
assertTrue( Hibernate.isInitialized( c.getName().getAliases() ) );
|
||||||
em.getTransaction().commit();
|
} );
|
||||||
em.close();
|
|
||||||
|
|
||||||
em = getOrCreateEntityManager();
|
doInJPA( this::entityManagerFactory, em -> {
|
||||||
em.getTransaction().begin();
|
Client c = em.merge( client );
|
||||||
client = em.merge( client );
|
em.remove( c );
|
||||||
em.remove( client );
|
} );
|
||||||
em.getTransaction().commit();
|
|
||||||
em.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue(jiraKey = "HHH-4586")
|
@TestForIssue(jiraKey = "HHH-4586")
|
||||||
public void testParameterizedFunctions() {
|
public void testParameterizedFunctions() {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
doInJPA( this::entityManagerFactory, em -> {
|
||||||
em.getTransaction().begin();
|
|
||||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
// lower
|
// lower
|
||||||
CriteriaQuery<Client> cq = cb.createQuery( Client.class );
|
CriteriaQuery<Client> cq = cb.createQuery( Client.class );
|
||||||
|
@ -150,7 +129,7 @@ public class ComponentCriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
||||||
root = cq.from( Client.class );
|
root = cq.from( Client.class );
|
||||||
cq.where( cb.equal( cb.upper( root.get( Client_.name ).get( Name_.lastName ) ), "test" ) );
|
cq.where( cb.equal( cb.upper( root.get( Client_.name ).get( Name_.lastName ) ), "test" ) );
|
||||||
em.createQuery( cq ).getResultList();
|
em.createQuery( cq ).getResultList();
|
||||||
em.getTransaction().commit();
|
}
|
||||||
em.close();
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,11 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.enums;
|
package org.hibernate.test.enums;
|
||||||
|
|
||||||
import org.hibernate.criterion.Restrictions;
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
import org.hibernate.internal.CoreMessageLogger;
|
import org.hibernate.internal.CoreMessageLogger;
|
||||||
import org.hibernate.type.descriptor.sql.BasicBinder;
|
import org.hibernate.type.descriptor.sql.BasicBinder;
|
||||||
import org.hibernate.type.descriptor.sql.BasicExtractor;
|
import org.hibernate.type.descriptor.sql.BasicExtractor;
|
||||||
|
@ -73,22 +77,34 @@ public class EnumTypeTest extends BaseCoreFunctionalTestCase {
|
||||||
@Test
|
@Test
|
||||||
@TestForIssue(jiraKey = "HHH-8153")
|
@TestForIssue(jiraKey = "HHH-8153")
|
||||||
public void hbmEnumTypeTest() {
|
public void hbmEnumTypeTest() {
|
||||||
doInHibernate( this::sessionFactory, s -> {
|
doInHibernate(
|
||||||
assertEquals( s.createCriteria( Person.class )
|
this::sessionFactory,
|
||||||
.add( Restrictions.eq( "gender", Gender.MALE ) )
|
s -> {
|
||||||
.list().size(), 2 );
|
assertEquals( getNumberOfPersonByGender( s, Gender.MALE ), 2 );
|
||||||
assertEquals( s.createCriteria( Person.class )
|
assertEquals( getNumberOfPersonByGenderAndHairColor( s, Gender.MALE, HairColor.BROWN ), 1 );
|
||||||
.add( Restrictions.eq( "gender", Gender.MALE ) )
|
assertEquals( getNumberOfPersonByGender( s, Gender.FEMALE ), 2 );
|
||||||
.add( Restrictions.eq( "hairColor", HairColor.BROWN ) )
|
assertEquals( getNumberOfPersonByGenderAndHairColor( s, Gender.FEMALE, HairColor.BROWN ), 1 );
|
||||||
.list().size(), 1 );
|
}
|
||||||
assertEquals( s.createCriteria( Person.class )
|
);
|
||||||
.add( Restrictions.eq( "gender", Gender.FEMALE ) )
|
}
|
||||||
.list().size(), 2 );
|
|
||||||
assertEquals( s.createCriteria( Person.class )
|
private int getNumberOfPersonByGender(Session session, Gender value) {
|
||||||
.add( Restrictions.eq( "gender", Gender.FEMALE ) )
|
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||||
.add( Restrictions.eq( "hairColor", HairColor.BROWN ) )
|
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
|
||||||
.list().size(), 1 );
|
Root<Person> root = criteria.from( Person.class );
|
||||||
} );
|
criteria.where( criteriaBuilder.equal( root.get( "gender" ), value ) );
|
||||||
|
return session.createQuery( criteria ).list().size();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getNumberOfPersonByGenderAndHairColor(Session session, Gender gender, HairColor hairColor) {
|
||||||
|
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
|
||||||
|
Root<Person> root = criteria.from( Person.class );
|
||||||
|
criteria.where( criteriaBuilder.and(
|
||||||
|
criteriaBuilder.equal( root.get( "gender" ), gender ),
|
||||||
|
criteriaBuilder.equal( root.get( "hairColor" ), hairColor )
|
||||||
|
) );
|
||||||
|
return session.createQuery( criteria ).list().size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -12,7 +12,6 @@ import javax.persistence.Enumerated;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
|
||||||
import org.hibernate.criterion.Restrictions;
|
|
||||||
import org.hibernate.internal.CoreMessageLogger;
|
import org.hibernate.internal.CoreMessageLogger;
|
||||||
import org.hibernate.type.descriptor.sql.BasicBinder;
|
import org.hibernate.type.descriptor.sql.BasicBinder;
|
||||||
import org.hibernate.type.descriptor.sql.BasicExtractor;
|
import org.hibernate.type.descriptor.sql.BasicExtractor;
|
||||||
|
@ -27,7 +26,6 @@ import org.junit.Test;
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
|
|
||||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -288,24 +288,24 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
|
||||||
.fetch( "nextHolder", JoinType.LEFT )
|
.fetch( "nextHolder", JoinType.LEFT )
|
||||||
.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT );
|
.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT );
|
||||||
criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) );
|
criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) );
|
||||||
|
EntityWithFunctionAsColumnHolder holder = s.createQuery( criteria ).uniqueResult();
|
||||||
// holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class )
|
// holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class )
|
||||||
// .add( Restrictions.isNotNull( "nextHolder" ))
|
// .add( Restrictions.isNotNull( "nextHolder" ))
|
||||||
// .setFetchMode( "entityWithNoArgFunctionAsColumns", FetchMode.JOIN )
|
// .setFetchMode( "entityWithNoArgFunctionAsColumns", FetchMode.JOIN )
|
||||||
// .setFetchMode( "nextHolder", FetchMode.JOIN )
|
// .setFetchMode( "nextHolder", FetchMode.JOIN )
|
||||||
// .setFetchMode( "nextHolder.entityWithNoArgFunctionAsColumns", FetchMode.JOIN )
|
// .setFetchMode( "nextHolder.entityWithNoArgFunctionAsColumns", FetchMode.JOIN )
|
||||||
// .uniqueResult();
|
// .uniqueResult();
|
||||||
assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) );
|
assertTrue( Hibernate.isInitialized( holder.getEntityWithNoArgFunctionAsColumns() ) );
|
||||||
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
|
assertTrue( Hibernate.isInitialized( holder.getNextHolder() ) );
|
||||||
assertTrue( Hibernate.isInitialized( holder1.getNextHolder()
|
assertTrue( Hibernate.isInitialized( holder.getNextHolder()
|
||||||
.getEntityWithNoArgFunctionAsColumns() ) );
|
.getEntityWithNoArgFunctionAsColumns() ) );
|
||||||
assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() );
|
assertEquals( 1, holder.getEntityWithNoArgFunctionAsColumns().size() );
|
||||||
EntityWithNoArgFunctionAsColumn e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns()
|
EntityWithNoArgFunctionAsColumn e1 = (EntityWithNoArgFunctionAsColumn) holder.getEntityWithNoArgFunctionAsColumns()
|
||||||
.iterator()
|
.iterator()
|
||||||
.next();
|
.next();
|
||||||
assertEquals( "blah blah blah", e1.getCurrentDate() );
|
assertEquals( "blah blah blah", e1.getCurrentDate() );
|
||||||
assertEquals( 1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() );
|
assertEquals( 1, holder.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() );
|
||||||
EntityWithNoArgFunctionAsColumn e2 = (EntityWithNoArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns()
|
EntityWithNoArgFunctionAsColumn e2 = (EntityWithNoArgFunctionAsColumn) ( holder.getNextHolder() ).getEntityWithNoArgFunctionAsColumns()
|
||||||
.iterator()
|
.iterator()
|
||||||
.next();
|
.next();
|
||||||
assertEquals( "yadda yadda yadda", e2.getCurrentDate() );
|
assertEquals( "yadda yadda yadda", e2.getCurrentDate() );
|
||||||
|
|
|
@ -296,32 +296,25 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
|
||||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||||
CriteriaQuery<Parent> criteria = criteriaBuilder.createQuery( Parent.class );
|
CriteriaQuery<Parent> criteria = criteriaBuilder.createQuery( Parent.class );
|
||||||
Root<Parent> root = criteria.from( Parent.class );
|
Root<Parent> root = criteria.from( Parent.class );
|
||||||
Join<Object, Object> friends = root.join( "moreChildren", JoinType.INNER )
|
root.join( "moreChildren", JoinType.INNER )
|
||||||
.join( "friends", JoinType.INNER );
|
.join( "friends", JoinType.INNER );
|
||||||
criteria.orderBy( criteriaBuilder.desc( friends.get( "name" ) ) );
|
criteria.orderBy( criteriaBuilder.desc( root.get( "name" ) ) );
|
||||||
|
|
||||||
s.createQuery( criteria ).list();
|
s.createQuery( criteria ).list();
|
||||||
|
|
||||||
|
|
||||||
criteria = criteriaBuilder.createQuery( Parent.class );
|
|
||||||
root = criteria.from( Parent.class );
|
|
||||||
Join<Object, Object> moreChildren = root.join( "moreChildren", JoinType.LEFT );
|
|
||||||
friends = moreChildren.join( "friends", JoinType.LEFT );
|
|
||||||
|
|
||||||
root.fetch( "moreChildren", JoinType.LEFT ).fetch(
|
|
||||||
"friends",
|
|
||||||
JoinType.LEFT
|
|
||||||
);
|
|
||||||
criteria.orderBy( criteriaBuilder.desc( friends.get( "name" ) ) );
|
|
||||||
|
|
||||||
List parents = s.createQuery( criteria ).list();
|
|
||||||
|
|
||||||
// List parents = s.createCriteria( Parent.class )
|
// List parents = s.createCriteria( Parent.class )
|
||||||
// .createCriteria( "moreChildren" )
|
// .createCriteria( "moreChildren" )
|
||||||
// .createCriteria( "friends" )
|
// .createCriteria( "friends" )
|
||||||
// .addOrder( Order.desc( "name" ) )
|
// .addOrder( Order.desc( "name" ) )
|
||||||
// .list();
|
// .list();
|
||||||
//
|
|
||||||
|
|
||||||
|
criteria = criteriaBuilder.createQuery( Parent.class );
|
||||||
|
root = criteria.from( Parent.class );
|
||||||
|
root.fetch( "moreChildren", JoinType.LEFT ).fetch( "friends", JoinType.LEFT );
|
||||||
|
criteria.orderBy( criteriaBuilder.desc( root.get( "name" ) ) );
|
||||||
|
|
||||||
|
List parents = s.createQuery( criteria ).list();
|
||||||
|
|
||||||
// parents = s.createCriteria( Parent.class )
|
// parents = s.createCriteria( Parent.class )
|
||||||
// .setFetchMode( "moreChildren", FetchMode.JOIN )
|
// .setFetchMode( "moreChildren", FetchMode.JOIN )
|
||||||
// .setFetchMode( "moreChildren.friends", FetchMode.JOIN )
|
// .setFetchMode( "moreChildren.friends", FetchMode.JOIN )
|
||||||
|
|
|
@ -9,18 +9,20 @@ package org.hibernate.test.where.hbm;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Join;
|
||||||
|
import javax.persistence.criteria.JoinType;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.hibernate.FetchMode;
|
|
||||||
import org.hibernate.criterion.Restrictions;
|
|
||||||
import org.hibernate.query.NativeQuery;
|
import org.hibernate.query.NativeQuery;
|
||||||
|
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
|
||||||
import org.hibernate.test.where.hbm.File;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
@ -86,7 +88,7 @@ public class WhereTest extends BaseCoreFunctionalTestCase {
|
||||||
final HashSet<File> filesSet = new HashSet<>( files );
|
final HashSet<File> filesSet = new HashSet<>( files );
|
||||||
assertEquals( 1, filesSet.size() );
|
assertEquals( 1, filesSet.size() );
|
||||||
File parent = files.get( 0 );
|
File parent = files.get( 0 );
|
||||||
assertEquals( parent.getChildren().size(), 1 );
|
assertEquals( 1, parent.getChildren().size() );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -95,11 +97,19 @@ public class WhereTest extends BaseCoreFunctionalTestCase {
|
||||||
public void testCriteria() {
|
public void testCriteria() {
|
||||||
inTransaction(
|
inTransaction(
|
||||||
s -> {
|
s -> {
|
||||||
File parent = (File) s.createCriteria( File.class )
|
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||||
.setFetchMode( "children", FetchMode.JOIN )
|
CriteriaQuery<File> criteria = criteriaBuilder.createQuery( File.class );
|
||||||
.add( Restrictions.isNull( "parent" ) )
|
Root<File> root = criteria.from( File.class );
|
||||||
.uniqueResult();
|
root.fetch( "children", JoinType.LEFT );
|
||||||
|
criteria.where( criteriaBuilder.isNull( root.get("parent") ));
|
||||||
|
File parent = s.createQuery( criteria ).uniqueResult();
|
||||||
assertEquals( parent.getChildren().size(), 1 );
|
assertEquals( parent.getChildren().size(), 1 );
|
||||||
|
|
||||||
|
// File parent = (File) s.createCriteria( File.class )
|
||||||
|
// .setFetchMode( "children", FetchMode.JOIN )
|
||||||
|
// .add( Restrictions.isNull( "parent" ) )
|
||||||
|
// .uniqueResult();
|
||||||
|
assertEquals( 1, parent.getChildren().size() );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -115,7 +125,7 @@ public class WhereTest extends BaseCoreFunctionalTestCase {
|
||||||
|
|
||||||
File parent = (File) ( (Object[]) query.list().get( 0 ) )[0];
|
File parent = (File) ( (Object[]) query.list().get( 0 ) )[0];
|
||||||
// @Where should not be applied
|
// @Where should not be applied
|
||||||
assertEquals( parent.getChildren().size(), 2 );
|
assertEquals( 2, parent.getChildren().size() );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue