6 - SQM based on JPA type system
This commit is contained in:
parent
fbe622fce6
commit
3d5209d105
|
@ -6,11 +6,15 @@
|
|||
*/
|
||||
package org.hibernate.test.annotations.idmanytoone;
|
||||
|
||||
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.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
@ -23,7 +27,7 @@ import static org.junit.Assert.assertEquals;
|
|||
*/
|
||||
public class IdManyToOneTest extends BaseCoreFunctionalTestCase {
|
||||
@Test
|
||||
public void testFkCreationOrdering() throws Exception {
|
||||
public void testFkCreationOrdering() {
|
||||
//no real test case, the sessionFactory building is tested
|
||||
Session s = openSession();
|
||||
s.close();
|
||||
|
@ -31,47 +35,49 @@ public class IdManyToOneTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testIdClassManyToOne() {
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
Store store = new Store();
|
||||
Customer customer = new Customer();
|
||||
s.persist( store );
|
||||
s.persist( customer );
|
||||
StoreCustomer sc = new StoreCustomer( store, customer );
|
||||
s.persist( sc );
|
||||
s.flush();
|
||||
s.clear();
|
||||
|
||||
store = (Store) s.get(Store.class, store.id );
|
||||
assertEquals( 1, store.customers.size() );
|
||||
assertEquals( customer.id, store.customers.iterator().next().customer.id );
|
||||
tx.rollback();
|
||||
inTransaction( s-> {
|
||||
Store store = new Store();
|
||||
Customer customer = new Customer();
|
||||
s.persist( store );
|
||||
s.persist( customer );
|
||||
StoreCustomer sc = new StoreCustomer( store, customer );
|
||||
s.persist( sc );
|
||||
s.flush();
|
||||
s.clear();
|
||||
|
||||
store = s.get(Store.class, store.id );
|
||||
assertEquals( 1, store.customers.size() );
|
||||
assertEquals( customer.id, store.customers.iterator().next().customer.id );
|
||||
} );
|
||||
//TODO test Customers / ShoppingBaskets / BasketItems testIdClassManyToOneWithReferenceColumn
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-7767" )
|
||||
public void testCriteriaRestrictionOnIdManyToOne() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
inTransaction( s -> {
|
||||
s.createQuery( "from Course c join c.students cs join cs.student s where s.name = 'Foo'" ).list();
|
||||
|
||||
s.createQuery( "from Course c join c.students cs join cs.student s where s.name = 'Foo'" ).list();
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Course> criteria = criteriaBuilder.createQuery( Course.class );
|
||||
Root<Course> root = criteria.from( Course.class );
|
||||
Join<Object, Object> students = root.join( "students", JoinType.INNER );
|
||||
Join<Object, Object> student = students.join( "student", JoinType.INNER );
|
||||
criteria.where( criteriaBuilder.equal( student.get( "name" ), "Foo" ) );
|
||||
session.createQuery( criteria ).list();
|
||||
// Criteria criteria = s.createCriteria( Course.class );
|
||||
// criteria.createCriteria( "students" ).createCriteria( "student" ).add( Restrictions.eq( "name", "Foo" ) );
|
||||
// criteria.list();
|
||||
|
||||
Criteria criteria = s.createCriteria( Course.class );
|
||||
criteria.createCriteria( "students" ).createCriteria( "student" ).add( Restrictions.eq( "name", "Foo" ) );
|
||||
criteria.list();
|
||||
// CriteriaQuery<Course> criteria2 = criteriaBuilder.createQuery( Course.class );
|
||||
|
||||
Criteria criteria2 = s.createCriteria( Course.class );
|
||||
criteria2.createAlias( "students", "cs" );
|
||||
criteria2.add( Restrictions.eq( "cs.value", "Bar" ) );
|
||||
criteria2.createAlias( "cs.student", "s" );
|
||||
criteria2.add( Restrictions.eq( "s.name", "Foo" ) );
|
||||
criteria2.list();
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
// Criteria criteria2 = s.createCriteria( Course.class );
|
||||
// criteria2.createAlias( "students", "cs" );
|
||||
// criteria2.add( Restrictions.eq( "cs.value", "Bar" ) );
|
||||
// criteria2.createAlias( "cs.student", "s" );
|
||||
// criteria2.add( Restrictions.eq( "s.name", "Foo" ) );
|
||||
// criteria2.list();
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -8,8 +8,10 @@ package org.hibernate.test.annotations.onetoone;
|
|||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.id.IdentifierGenerationException;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
|
@ -49,7 +51,7 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testBidirAssignedId() throws Exception {
|
||||
public void testBidirAssignedId() {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
PartyAffiliate affiliate = new PartyAffiliate();
|
||||
affiliate.partyId = "id";
|
||||
|
@ -58,10 +60,16 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase {
|
|||
} );
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
PartyAffiliate affiliate = (PartyAffiliate) session.createCriteria(
|
||||
PartyAffiliate.class )
|
||||
.add( Restrictions.idEq( "id" ) )
|
||||
.uniqueResult();
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<PartyAffiliate> criteria = criteriaBuilder.createQuery( PartyAffiliate.class );
|
||||
Root<PartyAffiliate> root = criteria.from( PartyAffiliate.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get("partyId"), "id" ) );
|
||||
|
||||
PartyAffiliate affiliate = session.createQuery( criteria ).uniqueResult();
|
||||
// PartyAffiliate affiliate = (PartyAffiliate) session.createCriteria(
|
||||
// PartyAffiliate.class )
|
||||
// .add( Restrictions.idEq( "id" ) )
|
||||
// .uniqueResult();
|
||||
assertNotNull( affiliate );
|
||||
assertEquals( "id", affiliate.partyId );
|
||||
assertNull( affiliate.party );
|
||||
|
@ -79,7 +87,7 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testBidirDefaultIdGenerator() throws Exception {
|
||||
public void testBidirDefaultIdGenerator() {
|
||||
PersonAddress _personAddress = doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
|
@ -93,10 +101,15 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase {
|
|||
);
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
PersonAddress personAddress = (PersonAddress) session.createCriteria(
|
||||
PersonAddress.class )
|
||||
.add( Restrictions.idEq( _personAddress.getId() ) )
|
||||
.uniqueResult();
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<PersonAddress> criteria = criteriaBuilder.createQuery( PersonAddress.class );
|
||||
Root<PersonAddress> root = criteria.from( PersonAddress.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get("id"), _personAddress.getId()) );
|
||||
PersonAddress personAddress = session.createQuery( criteria ).uniqueResult();
|
||||
// PersonAddress personAddress = (PersonAddress) session.createCriteria(
|
||||
// PersonAddress.class )
|
||||
// .add( Restrictions.idEq( _personAddress.getId() ) )
|
||||
// .uniqueResult();
|
||||
assertNotNull( personAddress );
|
||||
assertNull( personAddress.getPerson() );
|
||||
} );
|
||||
|
@ -114,7 +127,7 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-5757")
|
||||
public void testBidirQueryEntityProperty() throws Exception {
|
||||
public void testBidirQueryEntityProperty() {
|
||||
|
||||
AtomicReference<Person> personHolder = new AtomicReference<>();
|
||||
|
||||
|
@ -135,11 +148,16 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase {
|
|||
}
|
||||
);
|
||||
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
PersonAddress personAddress = (PersonAddress) session.createCriteria(
|
||||
PersonAddress.class )
|
||||
.add( Restrictions.idEq( _personAddress.getId() ) )
|
||||
.uniqueResult();
|
||||
CriteriaQuery<PersonAddress> criteria = criteriaBuilder.createQuery( PersonAddress.class );
|
||||
Root<PersonAddress> root = criteria.from( PersonAddress.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get("id"), _personAddress.getId()) );
|
||||
PersonAddress personAddress = session.createQuery( criteria ).uniqueResult();
|
||||
// PersonAddress personAddress = (PersonAddress) session.createCriteria(
|
||||
// PersonAddress.class )
|
||||
// .add( Restrictions.idEq( _personAddress.getId() ) )
|
||||
// .uniqueResult();
|
||||
assertNotNull( personAddress );
|
||||
assertNotNull( personAddress.getPerson() );
|
||||
} );
|
||||
|
@ -147,15 +165,20 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase {
|
|||
doInHibernate( this::sessionFactory, session -> {
|
||||
Person person = personHolder.get();
|
||||
// this call throws GenericJDBCException
|
||||
PersonAddress personAddress = (PersonAddress) session.createQuery(
|
||||
PersonAddress personAddress = session.createQuery(
|
||||
"select pa from PersonAddress pa where pa.person = :person", PersonAddress.class )
|
||||
.setParameter( "person", person )
|
||||
.getSingleResult();
|
||||
|
||||
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get("personAddress"), personAddress ) );
|
||||
|
||||
session.createQuery( criteria ).uniqueResult();
|
||||
// the other way should also work
|
||||
person = (Person) session.createCriteria( Person.class )
|
||||
.add( Restrictions.eq( "personAddress", personAddress ) )
|
||||
.uniqueResult();
|
||||
// person = (Person) session.createCriteria( Person.class )
|
||||
// .add( Restrictions.eq( "personAddress", personAddress ) )
|
||||
// .uniqueResult();
|
||||
|
||||
session.delete( personAddress );
|
||||
} );
|
||||
|
|
|
@ -7,10 +7,12 @@
|
|||
package org.hibernate.test.annotations.onetoone;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.id.IdentifierGenerationException;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
|
@ -97,9 +99,15 @@ public class OptionalOneToOnePKJCTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
s = openSession();
|
||||
s.getTransaction().begin();
|
||||
owner = ( Owner ) s.createCriteria( Owner.class )
|
||||
.add( Restrictions.idEq( owner.getId() ) )
|
||||
.uniqueResult();
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Owner> criteria = criteriaBuilder.createQuery( Owner.class );
|
||||
Root<Owner> root = criteria.from( Owner.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get( "id" ), owner.getId() ) );
|
||||
|
||||
owner = s.createQuery( criteria ).uniqueResult();
|
||||
// owner = ( Owner ) s.createCriteria( Owner.class )
|
||||
// .add( Restrictions.idEq( owner.getId() ) )
|
||||
// .uniqueResult();
|
||||
assertNotNull( owner );
|
||||
assertNull( owner.getAddress() );
|
||||
s.delete( owner );
|
||||
|
@ -127,9 +135,16 @@ public class OptionalOneToOnePKJCTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
s = openSession();
|
||||
s.getTransaction().begin();
|
||||
party = ( Party ) s.createCriteria( Party.class )
|
||||
.add( Restrictions.idEq( "id" ) )
|
||||
.uniqueResult();
|
||||
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Party> criteria = criteriaBuilder.createQuery( Party.class );
|
||||
Root<Party> root = criteria.from( Party.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get( "partyId" ), "id" ) );
|
||||
|
||||
party = s.createQuery( criteria ).uniqueResult();
|
||||
// party = ( Party ) s.createCriteria( Party.class )
|
||||
// .add( Restrictions.idEq( "id" ) )
|
||||
// .uniqueResult();
|
||||
assertNotNull( party );
|
||||
assertEquals( "id", party.partyId );
|
||||
assertNull( party.partyAffiliate );
|
||||
|
|
|
@ -9,11 +9,15 @@ package org.hibernate.test.collection.set;
|
|||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.CacheMode;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.collection.internal.PersistentSet;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.query.Query;
|
||||
import org.hibernate.stat.CollectionStatistics;
|
||||
|
||||
import org.hibernate.testing.FailureExpected;
|
||||
|
@ -146,7 +150,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
parent = ( Parent ) session.get( Parent.class, "p1" );
|
||||
parent = session.get( Parent.class, "p1" );
|
||||
assertEquals( 1, parent.getChildren().size() );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
@ -243,7 +247,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
container = ( Container ) session.get( Container.class, container.getId() );
|
||||
container = session.get( Container.class, container.getId() );
|
||||
assertEquals( 1, container.getContents().size() );
|
||||
session.delete( container );
|
||||
session.getTransaction().commit();
|
||||
|
@ -268,7 +272,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
container = ( Container ) session.get( Container.class, container.getId() );
|
||||
container = session.get( Container.class, container.getId() );
|
||||
assertEquals( 1, container.getContents().size() );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
@ -279,7 +283,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
container = ( Container ) session.get( Container.class, container.getId() );
|
||||
container = session.get( Container.class, container.getId() );
|
||||
assertEquals( 1, container.getContents().size() );
|
||||
session.delete( container );
|
||||
session.getTransaction().commit();
|
||||
|
@ -305,7 +309,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
parent = ( Parent ) session.get( Parent.class, parent.getName() );
|
||||
parent = session.get( Parent.class, parent.getName() );
|
||||
assertTrue( parent.getChildren().contains( child ) );
|
||||
assertTrue( parent.getChildren().contains( otherChild ) );
|
||||
session.getTransaction().commit();
|
||||
|
@ -313,22 +317,34 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
session = openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
child = ( Child ) session.get( Child.class, child.getName() );
|
||||
child = session.get( Child.class, child.getName() );
|
||||
assertTrue( child.getParent().getChildren().contains( child ) );
|
||||
session.clear();
|
||||
|
||||
child = ( Child ) session.createCriteria( Child.class, child.getName() )
|
||||
.setCacheable( true )
|
||||
.add( Restrictions.idEq( "c1" ) )
|
||||
.uniqueResult();
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<Child> criteria = criteriaBuilder.createQuery( Child.class );
|
||||
Root<Child> root = criteria.from( Child.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get( "name" ), "c1" ));
|
||||
child = session.createQuery( criteria ).uniqueResult();
|
||||
// child = ( Child ) session.createCriteria( Child.class, child.getName() )
|
||||
// .setCacheable( true )
|
||||
// .add( Restrictions.idEq( "c1" ) )
|
||||
// .uniqueResult();
|
||||
assertTrue( child.getParent().getChildren().contains( child ) );
|
||||
assertTrue( child.getParent().getChildren().contains( otherChild ) );
|
||||
session.clear();
|
||||
|
||||
child = ( Child ) session.createCriteria( Child.class, child.getName() )
|
||||
.setCacheable( true )
|
||||
.add( Restrictions.idEq( "c1" ) )
|
||||
.uniqueResult();
|
||||
CriteriaQuery<Child> criteriaQuery = criteriaBuilder.createQuery( Child.class );
|
||||
Root<Child> childRoot = criteriaQuery.from( Child.class );
|
||||
criteriaQuery.where( criteriaBuilder.equal( childRoot.get( "name" ), "c1" ));
|
||||
Query<Child> query = session.createQuery( criteriaQuery );
|
||||
query.setCacheable( true );
|
||||
child = query.uniqueResult();
|
||||
|
||||
// child = ( Child ) session.createCriteria( Child.class, child.getName() )
|
||||
// .setCacheable( true )
|
||||
// .add( Restrictions.idEq( "c1" ) )
|
||||
// .uniqueResult();
|
||||
assertTrue( child.getParent().getChildren().contains( child ) );
|
||||
assertTrue( child.getParent().getChildren().contains( otherChild ) );
|
||||
session.clear();
|
||||
|
@ -366,7 +382,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
session = openSession();
|
||||
session.beginTransaction();
|
||||
session.setCacheMode( CacheMode.IGNORE );
|
||||
parent = ( Parent ) session.get( Parent.class, parent.getName() );
|
||||
parent = session.get( Parent.class, parent.getName() );
|
||||
assertTrue( parent.getChildren().contains( child ) );
|
||||
assertTrue( parent.getChildren().contains( otherChild ) );
|
||||
session.getTransaction().commit();
|
||||
|
@ -375,13 +391,19 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
session.beginTransaction();
|
||||
session.setCacheMode( CacheMode.IGNORE );
|
||||
|
||||
child = ( Child ) session.get( Child.class, child.getName() );
|
||||
child = session.get( Child.class, child.getName() );
|
||||
assertTrue( child.getParent().getChildren().contains( child ) );
|
||||
session.clear();
|
||||
|
||||
child = ( Child ) session.createCriteria( Child.class, child.getName() )
|
||||
.add( Restrictions.idEq( "c1" ) )
|
||||
.uniqueResult();
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<Child> criteria = criteriaBuilder.createQuery( Child.class );
|
||||
Root<Child> root = criteria.from( Child.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get( "name" ), "c1" ) );
|
||||
|
||||
child = session.createQuery( criteria ).uniqueResult();
|
||||
// child = ( Child ) session.createCriteria( Child.class, child.getName() )
|
||||
// .add( Restrictions.idEq( "c1" ) )
|
||||
// .uniqueResult();
|
||||
assertTrue( child.getParent().getChildren().contains( child ) );
|
||||
assertTrue( child.getParent().getChildren().contains( otherChild ) );
|
||||
session.clear();
|
||||
|
|
|
@ -8,10 +8,13 @@ package org.hibernate.test.compositeelement;
|
|||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.dialect.function.SQLFunction;
|
||||
import org.hibernate.mapping.Collection;
|
||||
import org.hibernate.mapping.Component;
|
||||
|
@ -88,42 +91,51 @@ public class CompositeElementTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testCustomColumnReadAndWrite() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
Child c = new Child( "Child One" );
|
||||
c.setPosition( 1 );
|
||||
Parent p = new Parent( "Parent" );
|
||||
p.getChildren().add( c );
|
||||
c.setParent( p );
|
||||
s.save( p );
|
||||
s.flush();
|
||||
inTransaction( s -> {
|
||||
Child c = new Child( "Child One" );
|
||||
c.setPosition( 1 );
|
||||
Parent p = new Parent( "Parent" );
|
||||
p.getChildren().add( c );
|
||||
c.setParent( p );
|
||||
s.save( p );
|
||||
s.flush();
|
||||
|
||||
// Oracle returns BigDecimaal while other dialects return Integer;
|
||||
// casting to Number so it works on all dialects
|
||||
Number sqlValue = ((Number) s.createSQLQuery("select child_position from ParentChild c where c.name='Child One'")
|
||||
.uniqueResult());
|
||||
assertEquals( 0, sqlValue.intValue() );
|
||||
// Oracle returns BigDecimaal while other dialects return Integer;
|
||||
// casting to Number so it works on all dialects
|
||||
Number sqlValue = ( (Number) s.createNativeQuery(
|
||||
"select child_position from ParentChild c where c.name='Child One'" )
|
||||
.uniqueResult() );
|
||||
assertEquals( 0, sqlValue.intValue() );
|
||||
|
||||
Integer hqlValue = (Integer)s.createQuery("select c.position from Parent p join p.children c where p.name='Parent'")
|
||||
.uniqueResult();
|
||||
assertEquals( 1, hqlValue.intValue() );
|
||||
Integer hqlValue = (Integer) s.createQuery(
|
||||
"select c.position from Parent p join p.children c where p.name='Parent'" )
|
||||
.uniqueResult();
|
||||
assertEquals( 1, hqlValue.intValue() );
|
||||
|
||||
p = (Parent)s.createCriteria(Parent.class).add(Restrictions.eq("name", "Parent")).uniqueResult();
|
||||
c = (Child)p.getChildren().iterator().next();
|
||||
assertEquals( 1, c.getPosition() );
|
||||
// p = (Parent) s.createCriteria( Parent.class ).add( Restrictions.eq( "name", "Parent" ) ).uniqueResult();
|
||||
|
||||
p = (Parent)s.createQuery("from Parent p join p.children c where c.position = 1").uniqueResult();
|
||||
c = (Child)p.getChildren().iterator().next();
|
||||
assertEquals( 1, c.getPosition() );
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Parent> criteria = criteriaBuilder.createQuery( Parent.class );
|
||||
Root<Parent> root = criteria.from( Parent.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get( "name" ),"Parent" ) );
|
||||
|
||||
c.setPosition( 2 );
|
||||
s.flush();
|
||||
sqlValue = ( (Number) s.createSQLQuery("select child_position from ParentChild c where c.name='Child One'")
|
||||
.uniqueResult() );
|
||||
assertEquals( 1, sqlValue.intValue() );
|
||||
s.delete( p );
|
||||
t.commit();
|
||||
s.close();
|
||||
p = s.createQuery( criteria ).uniqueResult();
|
||||
|
||||
c = (Child) p.getChildren().iterator().next();
|
||||
assertEquals( 1, c.getPosition() );
|
||||
|
||||
p = (Parent) s.createQuery( "from Parent p join p.children c where c.position = 1" ).uniqueResult();
|
||||
c = (Child) p.getChildren().iterator().next();
|
||||
assertEquals( 1, c.getPosition() );
|
||||
|
||||
c.setPosition( 2 );
|
||||
s.flush();
|
||||
sqlValue = ( (Number) s.createNativeQuery(
|
||||
"select child_position from ParentChild c where c.name='Child One'" )
|
||||
.uniqueResult() );
|
||||
assertEquals( 1, sqlValue.intValue() );
|
||||
s.delete( p );
|
||||
} );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,8 +6,9 @@
|
|||
*/
|
||||
package org.hibernate.test.fetchprofiles.join.selfReferencing;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -24,13 +25,16 @@ public class JoinSelfReferentialFetchProfileTest extends BaseCoreFunctionalTestC
|
|||
|
||||
@Test
|
||||
public void testEnablingJoinFetchProfileAgainstSelfReferentialAssociation() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
s.enableFetchProfile( Employee.FETCH_PROFILE_TREE );
|
||||
s.createCriteria( Employee.class )
|
||||
.add( Restrictions.isNull( "manager" ) )
|
||||
.list();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction( s-> {
|
||||
s.enableFetchProfile( Employee.FETCH_PROFILE_TREE );
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Employee> criteria = criteriaBuilder.createQuery( Employee.class );
|
||||
Root<Employee> root = criteria.from( Employee.class );
|
||||
criteria.where( criteriaBuilder.isNull( root.get( "manager" ) ) );
|
||||
s.createQuery( criteria ).list();
|
||||
// s.createCriteria( Employee.class )
|
||||
// .add( Restrictions.isNull( "manager" ) )
|
||||
// .list();
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,18 +5,24 @@
|
|||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.joinedsubclass;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.LockMode;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Property;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.query.Query;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -37,77 +43,94 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testJoinedSubclass() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
inTransaction( s -> {
|
||||
|
||||
Employee mark = new Employee();
|
||||
mark.setName("Mark");
|
||||
mark.setTitle("internal sales");
|
||||
mark.setSex('M');
|
||||
mark.setAddress("buckhead");
|
||||
mark.setZip("30305");
|
||||
mark.setCountry("USA");
|
||||
Employee mark = new Employee();
|
||||
mark.setName( "Mark" );
|
||||
mark.setTitle( "internal sales" );
|
||||
mark.setSex( 'M' );
|
||||
mark.setAddress( "buckhead" );
|
||||
mark.setZip( "30305" );
|
||||
mark.setCountry( "USA" );
|
||||
|
||||
Customer joe = new Customer();
|
||||
joe.setName("Joe");
|
||||
joe.setAddress("San Francisco");
|
||||
joe.setZip("XXXXX");
|
||||
joe.setCountry("USA");
|
||||
joe.setComments("Very demanding");
|
||||
joe.setSex('M');
|
||||
joe.setSalesperson(mark);
|
||||
Customer joe = new Customer();
|
||||
joe.setName( "Joe" );
|
||||
joe.setAddress( "San Francisco" );
|
||||
joe.setZip( "XXXXX" );
|
||||
joe.setCountry( "USA" );
|
||||
joe.setComments( "Very demanding" );
|
||||
joe.setSex( 'M' );
|
||||
joe.setSalesperson( mark );
|
||||
|
||||
Person yomomma = new Person();
|
||||
yomomma.setName("mum");
|
||||
yomomma.setSex('F');
|
||||
Person yomomma = new Person();
|
||||
yomomma.setName( "mum" );
|
||||
yomomma.setSex( 'F' );
|
||||
|
||||
s.save(yomomma);
|
||||
s.save(mark);
|
||||
s.save(joe);
|
||||
s.save( yomomma );
|
||||
s.save( mark );
|
||||
s.save( joe );
|
||||
|
||||
assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 );
|
||||
assertEquals( s.createQuery( "from java.io.Serializable" ).list().size(), 0 );
|
||||
|
||||
assertEquals( s.createQuery("from Person").list().size(), 3 );
|
||||
assertEquals( s.createQuery("from Person p where p.class = Customer").list().size(), 1 );
|
||||
assertEquals( s.createQuery("from Person p where p.class = Person").list().size(), 1 );
|
||||
assertEquals( s.createQuery("from Person p where type(p) in :who").setParameter("who", Customer.class).list().size(), 1 );
|
||||
assertEquals( s.createQuery("from Person p where type(p) in :who").setParameterList("who", new Class[] {Customer.class, Person.class}).list().size(), 2 );
|
||||
s.clear();
|
||||
assertEquals( s.createQuery( "from Person" ).list().size(), 3 );
|
||||
assertEquals( s.createQuery( "from Person p where p.class = Customer" ).list().size(), 1 );
|
||||
assertEquals( s.createQuery( "from Person p where p.class = Person" ).list().size(), 1 );
|
||||
assertEquals( s.createQuery( "from Person p where type(p) in :who" )
|
||||
.setParameter( "who", Customer.class )
|
||||
.list()
|
||||
.size(), 1 );
|
||||
assertEquals( s.createQuery( "from Person p where type(p) in :who" ).setParameterList(
|
||||
"who",
|
||||
new Class[] {
|
||||
Customer.class,
|
||||
Person.class
|
||||
}
|
||||
).list().size(), 2 );
|
||||
s.clear();
|
||||
|
||||
List customers = s.createQuery("from Customer c left join fetch c.salesperson").list();
|
||||
for ( Iterator iter = customers.iterator(); iter.hasNext(); ) {
|
||||
Customer c = (Customer) iter.next();
|
||||
assertTrue( Hibernate.isInitialized( c.getSalesperson() ) );
|
||||
assertEquals( c.getSalesperson().getName(), "Mark" );
|
||||
}
|
||||
assertEquals( customers.size(), 1 );
|
||||
s.clear();
|
||||
List customers = s.createQuery( "from Customer c left join fetch c.salesperson" ).list();
|
||||
for ( Iterator iter = customers.iterator(); iter.hasNext(); ) {
|
||||
Customer c = (Customer) iter.next();
|
||||
assertTrue( Hibernate.isInitialized( c.getSalesperson() ) );
|
||||
assertEquals( c.getSalesperson().getName(), "Mark" );
|
||||
}
|
||||
assertEquals( customers.size(), 1 );
|
||||
s.clear();
|
||||
|
||||
customers = s.createQuery("from Customer").list();
|
||||
for ( Iterator iter = customers.iterator(); iter.hasNext(); ) {
|
||||
Customer c = (Customer) iter.next();
|
||||
assertFalse( Hibernate.isInitialized( c.getSalesperson() ) );
|
||||
assertEquals( c.getSalesperson().getName(), "Mark" );
|
||||
}
|
||||
assertEquals( customers.size(), 1 );
|
||||
s.clear();
|
||||
customers = s.createQuery( "from Customer" ).list();
|
||||
for ( Iterator iter = customers.iterator(); iter.hasNext(); ) {
|
||||
Customer c = (Customer) iter.next();
|
||||
assertFalse( Hibernate.isInitialized( c.getSalesperson() ) );
|
||||
assertEquals( c.getSalesperson().getName(), "Mark" );
|
||||
}
|
||||
assertEquals( customers.size(), 1 );
|
||||
s.clear();
|
||||
|
||||
|
||||
mark = (Employee) s.get( Employee.class, new Long( mark.getId() ) );
|
||||
joe = (Customer) s.get( Customer.class, new Long( joe.getId() ) );
|
||||
mark = (Employee) s.get( Employee.class, new Long( mark.getId() ) );
|
||||
joe = (Customer) s.get( Customer.class, new Long( joe.getId() ) );
|
||||
|
||||
mark.setZip("30306");
|
||||
assertEquals( s.createQuery("from Person p where p.address.zip = '30306'" ).list().size(),1 );
|
||||
mark.setZip( "30306" );
|
||||
assertEquals( s.createQuery( "from Person p where p.address.zip = '30306'" ).list().size(), 1 );
|
||||
|
||||
s.createCriteria( Person.class ).add(
|
||||
Restrictions.in( "address", mark.getAddress(), joe.getAddress() ) ).list();
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
CriteriaBuilder.In<Address> in = criteriaBuilder.in( root.get( "address" ) );
|
||||
in.value( mark.getAddress() );
|
||||
in.value( joe.getAddress() );
|
||||
|
||||
s.delete(mark);
|
||||
s.delete(joe);
|
||||
s.delete(yomomma);
|
||||
assertTrue( s.createQuery("from Person").list().isEmpty() );
|
||||
t.commit();
|
||||
s.close();
|
||||
criteria.where( in );
|
||||
Query<Person> query = s.createQuery( criteria );
|
||||
query.list();
|
||||
// s.createCriteria( Person.class ).add(
|
||||
// Restrictions.in( "address", mark.getAddress(), joe.getAddress() ) ).list();
|
||||
|
||||
s.delete( mark );
|
||||
s.delete( joe );
|
||||
s.delete( yomomma );
|
||||
assertTrue( s.createQuery( "from Person" ).list().isEmpty() );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -124,15 +147,15 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
Customer c = ( Customer ) s.get( Customer.class, new Long( e.getId() ) );
|
||||
Customer c = s.get( Customer.class, new Long( e.getId() ) );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
assertNull( c );
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
e = ( Employee ) s.get( Employee.class, new Long( e.getId() ) );
|
||||
c = ( Customer ) s.get( Customer.class, new Long( e.getId() ) );
|
||||
e = s.get( Employee.class, new Long( e.getId() ) );
|
||||
c = s.get( Customer.class, new Long( e.getId() ) );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
assertNotNull( e );
|
||||
|
@ -147,41 +170,47 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testQuerySubclassAttribute() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
Person p = new Person();
|
||||
p.setName("Emmanuel");
|
||||
p.setSex('M');
|
||||
s.persist(p);
|
||||
Employee q = new Employee();
|
||||
q.setName("Steve");
|
||||
q.setSex('M');
|
||||
q.setTitle("Mr");
|
||||
q.setSalary( new BigDecimal(1000) );
|
||||
s.persist(q);
|
||||
inTransaction( s -> {
|
||||
Person p = new Person();
|
||||
p.setName( "Emmanuel" );
|
||||
p.setSex( 'M' );
|
||||
s.persist( p );
|
||||
Employee q = new Employee();
|
||||
q.setName( "Steve" );
|
||||
q.setSex( 'M' );
|
||||
q.setTitle( "Mr" );
|
||||
q.setSalary( new BigDecimal( 1000 ) );
|
||||
s.persist( q );
|
||||
|
||||
List result = s.createQuery("from Person where salary > 100").list();
|
||||
assertEquals( result.size(), 1 );
|
||||
assertSame( result.get(0), q );
|
||||
List result = s.createQuery( "from Person where salary > 100" ).list();
|
||||
assertEquals( result.size(), 1 );
|
||||
assertSame( result.get( 0 ), q );
|
||||
|
||||
result = s.createQuery("from Person where salary > 100 or name like 'E%'").list();
|
||||
assertEquals( result.size(), 2 );
|
||||
result = s.createQuery( "from Person where salary > 100 or name like 'E%'" ).list();
|
||||
assertEquals( result.size(), 2 );
|
||||
|
||||
result = s.createCriteria(Person.class)
|
||||
.add( Property.forName("salary").gt( new BigDecimal(100) ) )
|
||||
.list();
|
||||
assertEquals( result.size(), 1 );
|
||||
assertSame( result.get(0), q );
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
|
||||
|
||||
//TODO: make this work:
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
|
||||
criteria.where( criteriaBuilder.gt(root.get( "salary" ), new BigDecimal( 100 ) ));
|
||||
|
||||
result = s.createQuery( criteria ).list();
|
||||
// result = s.createCriteria( Person.class )
|
||||
// .add( Property.forName( "salary" ).gt( new BigDecimal( 100 ) ) )
|
||||
// .list();
|
||||
assertEquals( result.size(), 1 );
|
||||
assertSame( result.get( 0 ), q );
|
||||
|
||||
//TODO: make this work:
|
||||
/*result = s.createQuery("select salary from Person where salary > 100").list();
|
||||
assertEquals( result.size(), 1 );
|
||||
assertEquals( result.get(0), new BigDecimal(1000) );*/
|
||||
|
||||
s.delete(p);
|
||||
s.delete(q);
|
||||
t.commit();
|
||||
s.close();
|
||||
s.delete( p );
|
||||
s.delete( q );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -191,81 +220,100 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
final double HEIGHT_INCHES = 73;
|
||||
final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d;
|
||||
Person p = new Person();
|
||||
p.setName("Emmanuel");
|
||||
p.setSex('M');
|
||||
p.setHeightInches(HEIGHT_INCHES);
|
||||
s.persist(p);
|
||||
p.setName( "Emmanuel" );
|
||||
p.setSex( 'M' );
|
||||
p.setHeightInches( HEIGHT_INCHES );
|
||||
s.persist( p );
|
||||
final double PASSWORD_EXPIRY_WEEKS = 4;
|
||||
final double PASSWORD_EXPIRY_DAYS = PASSWORD_EXPIRY_WEEKS * 7d;
|
||||
Employee e = new Employee();
|
||||
e.setName("Steve");
|
||||
e.setSex('M');
|
||||
e.setTitle("Mr");
|
||||
e.setPasswordExpiryDays(PASSWORD_EXPIRY_DAYS);
|
||||
s.persist(e);
|
||||
e.setName( "Steve" );
|
||||
e.setSex( 'M' );
|
||||
e.setTitle( "Mr" );
|
||||
e.setPasswordExpiryDays( PASSWORD_EXPIRY_DAYS );
|
||||
s.persist( e );
|
||||
s.flush();
|
||||
|
||||
|
||||
// Test value conversion during insert
|
||||
// Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType;
|
||||
// Cast returned value to Number then call Number.doubleValue() so it works on all dialects.
|
||||
Double heightViaSql =
|
||||
( (Number)s.createSQLQuery("select height_centimeters from JPerson where name='Emmanuel'").uniqueResult() )
|
||||
( (Number) s.createNativeQuery( "select height_centimeters from JPerson where name='Emmanuel'" )
|
||||
.uniqueResult() )
|
||||
.doubleValue();
|
||||
assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d);
|
||||
assertEquals( HEIGHT_CENTIMETERS, heightViaSql, 0.01d );
|
||||
Double expiryViaSql =
|
||||
( (Number)s.createSQLQuery("select pwd_expiry_weeks from JEmployee where person_id=?")
|
||||
.setLong(1, e.getId())
|
||||
( (Number) s.createNativeQuery( "select pwd_expiry_weeks from JEmployee where person_id=?" )
|
||||
.setParameter( 1, e.getId() )
|
||||
.uniqueResult()
|
||||
).doubleValue();
|
||||
assertEquals(PASSWORD_EXPIRY_WEEKS, expiryViaSql, 0.01d);
|
||||
|
||||
assertEquals( PASSWORD_EXPIRY_WEEKS, expiryViaSql, 0.01d );
|
||||
|
||||
// Test projection
|
||||
Double heightViaHql = (Double)s.createQuery("select p.heightInches from Person p where p.name = 'Emmanuel'").uniqueResult();
|
||||
assertEquals(HEIGHT_INCHES, heightViaHql, 0.01d);
|
||||
Double expiryViaHql = (Double)s.createQuery("select e.passwordExpiryDays from Employee e where e.name = 'Steve'").uniqueResult();
|
||||
assertEquals(PASSWORD_EXPIRY_DAYS, expiryViaHql, 0.01d);
|
||||
|
||||
Double heightViaHql = (Double) s.createQuery( "select p.heightInches from Person p where p.name = 'Emmanuel'" )
|
||||
.uniqueResult();
|
||||
assertEquals( HEIGHT_INCHES, heightViaHql, 0.01d );
|
||||
Double expiryViaHql = (Double) s.createQuery(
|
||||
"select e.passwordExpiryDays from Employee e where e.name = 'Steve'" ).uniqueResult();
|
||||
assertEquals( PASSWORD_EXPIRY_DAYS, expiryViaHql, 0.01d );
|
||||
|
||||
// Test restriction and entity load via criteria
|
||||
p = (Person)s.createCriteria(Person.class)
|
||||
.add(Restrictions.between("heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d))
|
||||
.uniqueResult();
|
||||
assertEquals(HEIGHT_INCHES, p.getHeightInches(), 0.01d);
|
||||
e = (Employee)s.createCriteria(Employee.class)
|
||||
.add(Restrictions.between("passwordExpiryDays", PASSWORD_EXPIRY_DAYS - 0.01d, PASSWORD_EXPIRY_DAYS + 0.01d))
|
||||
.uniqueResult();
|
||||
assertEquals(PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d);
|
||||
|
||||
// p = (Person)s.createCriteria(Person.class)
|
||||
// .add(Restrictions.between("heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d))
|
||||
// .uniqueResult();
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
criteria.where( criteriaBuilder.between( root.get("heightInches"),HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d ) ) ;
|
||||
p = s.createQuery( criteria).uniqueResult();
|
||||
|
||||
assertEquals( HEIGHT_INCHES, p.getHeightInches(), 0.01d );
|
||||
CriteriaQuery<Employee> employeeCriteriaQuery = criteriaBuilder.createQuery( Employee.class );
|
||||
Root<Employee> employeeRoot = employeeCriteriaQuery.from( Employee.class );
|
||||
employeeCriteriaQuery.where( criteriaBuilder.between( employeeRoot.get("passwordExpiryDays"), PASSWORD_EXPIRY_DAYS - 0.01d,
|
||||
PASSWORD_EXPIRY_DAYS + 0.01d ) );
|
||||
e = s.createQuery( employeeCriteriaQuery ).uniqueResult();
|
||||
// e = (Employee) s.createCriteria( Employee.class )
|
||||
// .add( Restrictions.between(
|
||||
// "passwordExpiryDays",
|
||||
// PASSWORD_EXPIRY_DAYS - 0.01d,
|
||||
// PASSWORD_EXPIRY_DAYS + 0.01d
|
||||
// ) )
|
||||
// .uniqueResult();
|
||||
assertEquals( PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d );
|
||||
|
||||
// Test predicate and entity load via HQL
|
||||
p = (Person)s.createQuery("from Person p where p.heightInches between ?1 and ?2")
|
||||
.setDouble(1, HEIGHT_INCHES - 0.01d)
|
||||
.setDouble(2, HEIGHT_INCHES + 0.01d)
|
||||
.uniqueResult();
|
||||
assertEquals(HEIGHT_INCHES, p.getHeightInches(), 0.01d);
|
||||
e = (Employee)s.createQuery("from Employee e where e.passwordExpiryDays between ?1 and ?2")
|
||||
.setDouble(1, PASSWORD_EXPIRY_DAYS - 0.01d)
|
||||
.setDouble(2, PASSWORD_EXPIRY_DAYS + 0.01d)
|
||||
.uniqueResult();
|
||||
assertEquals(PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d);
|
||||
|
||||
p = (Person) s.createQuery( "from Person p where p.heightInches between ?1 and ?2" )
|
||||
.setParameter( 1, HEIGHT_INCHES - 0.01d )
|
||||
.setParameter( 2, HEIGHT_INCHES + 0.01d )
|
||||
.uniqueResult();
|
||||
assertEquals( HEIGHT_INCHES, p.getHeightInches(), 0.01d );
|
||||
e = (Employee) s.createQuery( "from Employee e where e.passwordExpiryDays between ?1 and ?2" )
|
||||
.setParameter( 1, PASSWORD_EXPIRY_DAYS - 0.01d )
|
||||
.setParameter( 2, PASSWORD_EXPIRY_DAYS + 0.01d )
|
||||
.uniqueResult();
|
||||
assertEquals( PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d );
|
||||
|
||||
// Test update
|
||||
p.setHeightInches(1);
|
||||
e.setPasswordExpiryDays(7);
|
||||
p.setHeightInches( 1 );
|
||||
e.setPasswordExpiryDays( 7 );
|
||||
s.flush();
|
||||
heightViaSql =
|
||||
( (Number)s.createSQLQuery("select height_centimeters from JPerson where name='Emmanuel'").uniqueResult() )
|
||||
( (Number) s.createNativeQuery( "select height_centimeters from JPerson where name='Emmanuel'" )
|
||||
.uniqueResult() )
|
||||
.doubleValue();
|
||||
assertEquals(2.54d, heightViaSql, 0.01d);
|
||||
assertEquals( 2.54d, heightViaSql, 0.01d );
|
||||
expiryViaSql =
|
||||
( (Number)s.createSQLQuery("select pwd_expiry_weeks from JEmployee where person_id=?")
|
||||
.setLong(1, e.getId())
|
||||
( (Number) s.createNativeQuery( "select pwd_expiry_weeks from JEmployee where person_id=?" )
|
||||
.setParameter( 1, e.getId() )
|
||||
.uniqueResult()
|
||||
).doubleValue();
|
||||
assertEquals(1d, expiryViaSql, 0.01d);
|
||||
s.delete(p);
|
||||
s.delete(e);
|
||||
assertEquals( 1d, expiryViaSql, 0.01d );
|
||||
s.delete( p );
|
||||
s.delete( e );
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -273,15 +321,15 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
Person p = new Person();
|
||||
p.setName("Emmanuel");
|
||||
p.setSex('M');
|
||||
s.persist(p);
|
||||
p.setName( "Emmanuel" );
|
||||
p.setSex( 'M' );
|
||||
s.persist( p );
|
||||
Employee q = new Employee();
|
||||
q.setName("Steve");
|
||||
q.setSex('M');
|
||||
q.setTitle("Mr");
|
||||
q.setSalary( new BigDecimal(1000) );
|
||||
s.persist(q);
|
||||
q.setName( "Steve" );
|
||||
q.setSex( 'M' );
|
||||
q.setTitle( "Mr" );
|
||||
q.setSalary( new BigDecimal( 1000 ) );
|
||||
s.persist( q );
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
|
|
|
@ -7,12 +7,17 @@
|
|||
package org.hibernate.test.keymanytoone.bidir.embedded;
|
||||
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.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
|
@ -35,59 +40,55 @@ public class KeyManyToOneTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testCriteriaRestrictionOnKeyManyToOne() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
s.createQuery( "from Order o where o.customer.name = 'Acme'" ).list();
|
||||
Criteria criteria = s.createCriteria( Order.class );
|
||||
criteria.createCriteria( "customer" ).add( Restrictions.eq( "name", "Acme" ) );
|
||||
criteria.list();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction( s -> {
|
||||
s.createQuery( "from Order o where o.customer.name = 'Acme'" ).list();
|
||||
// Criteria criteria = s.createCriteria( Order.class );
|
||||
// criteria.createCriteria( "customer" ).add( Restrictions.eq( "name", "Acme" ) );
|
||||
// criteria.list();
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Order> criteria = criteriaBuilder.createQuery( Order.class );
|
||||
Root<Order> root = criteria.from( Order.class );
|
||||
Join<Object, Object> customer = root.join( "customer", JoinType.INNER );
|
||||
criteria.where( criteriaBuilder.equal( customer.get( "name" ), "Acme" ) );
|
||||
s.createQuery( criteria ).list();
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveCascadedToKeyManyToOne() {
|
||||
// test cascading a save to an association with a key-many-to-one which refers to a
|
||||
// just saved entity
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Customer cust = new Customer( "Acme, Inc." );
|
||||
Order order = new Order( cust, 1 );
|
||||
cust.getOrders().add( order );
|
||||
sessionFactory().getStatistics().clear();
|
||||
s.save( cust );
|
||||
s.flush();
|
||||
assertEquals( 2, sessionFactory().getStatistics().getEntityInsertCount() );
|
||||
s.delete( cust );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction( s -> {
|
||||
Customer cust = new Customer( "Acme, Inc." );
|
||||
Order order = new Order( cust, 1 );
|
||||
cust.getOrders().add( order );
|
||||
sessionFactory().getStatistics().clear();
|
||||
s.save( cust );
|
||||
s.flush();
|
||||
assertEquals( 2, sessionFactory().getStatistics().getEntityInsertCount() );
|
||||
s.delete( cust );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryingOnMany2One() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Customer cust = new Customer( "Acme, Inc." );
|
||||
Order order = new Order( cust, 1 );
|
||||
cust.getOrders().add( order );
|
||||
s.save( cust );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction( s -> {
|
||||
cust.getOrders().add( order );
|
||||
s.save( cust );
|
||||
} );
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
List results = s.createQuery( "from Order o where o.customer.name = :name" )
|
||||
.setParameter( "name", cust.getName() )
|
||||
.list();
|
||||
assertEquals( 1, results.size() );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction( s -> {
|
||||
List results = s.createQuery( "from Order o where o.customer.name = :name" )
|
||||
.setParameter( "name", cust.getName() )
|
||||
.list();
|
||||
assertEquals( 1, results.size() );
|
||||
} );
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
s.delete( cust );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction( s -> {
|
||||
s.delete( cust );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue