mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-07 19:58:16 +00:00
6 - SQM based on JPA type system
This commit is contained in:
parent
ff59a1301d
commit
86ccb2115b
@ -6,10 +6,13 @@
|
||||
*/
|
||||
package org.hibernate.test.idprops;
|
||||
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.query.Query;
|
||||
import org.hibernate.Session;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@ -25,149 +28,58 @@ public String[] getMappings() {
|
||||
|
||||
@Test
|
||||
public void testHqlIdPropertyReferences() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Person p = new Person( new Long(1), "steve", 123 );
|
||||
s.save( p );
|
||||
Order o = new Order( new Long(1), p );
|
||||
LineItem l = new LineItem( o, "my-product", 2 );
|
||||
l.setId( "456" );
|
||||
s.save( o );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
Person p = new Person( new Long( 1 ), "steve", 123 );
|
||||
Order o = new Order( new Long( 1 ), p );
|
||||
inTransaction(
|
||||
s -> {
|
||||
s.save( p );
|
||||
LineItem l = new LineItem( o, "my-product", 2 );
|
||||
l.setId( "456" );
|
||||
s.save( o );
|
||||
}
|
||||
);
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
inTransaction(
|
||||
s -> {
|
||||
long count = extractCount( s, "select count(*) from Person p where p.id = 123" );
|
||||
assertEquals( "Person by id prop (non-identifier)", 1, count );
|
||||
count = extractCount( s, "select count(*) from Person p where p.pk = 1" );
|
||||
assertEquals( "Person by pk prop (identifier)", 1, count );
|
||||
|
||||
long count = extractCount( s, "select count(*) from Person p where p.id = 123" );
|
||||
assertEquals( "Person by id prop (non-identifier)", 1, count );
|
||||
count = extractCount( s, "select count(*) from Person p where p.pk = 1" );
|
||||
assertEquals( "Person by pk prop (identifier)", 1, count );
|
||||
count = extractCount( s, "select count(*) from Order o where o.id = 1" );
|
||||
assertEquals( "Order by number prop (named identifier)", 1, count );
|
||||
count = extractCount( s, "select count(*) from Order o where o.number = 1" );
|
||||
assertEquals( "Order by id prop (virtual identifier)", 1, count );
|
||||
|
||||
count = extractCount( s, "select count(*) from Order o where o.id = 1" );
|
||||
assertEquals( "Order by number prop (named identifier)", 1, count );
|
||||
count = extractCount( s, "select count(*) from Order o where o.number = 1" );
|
||||
assertEquals( "Order by id prop (virtual identifier)", 1, count );
|
||||
count = extractCount( s, "select count(*) from LineItem l where l.id = '456'" );
|
||||
assertEquals( "LineItem by id prop (non-identifier", 1, count );
|
||||
|
||||
count = extractCount( s, "select count(*) from LineItem l where l.id = '456'" );
|
||||
assertEquals( "LineItem by id prop (non-identifier", 1, count );
|
||||
if ( getDialect().supportsRowValueConstructorSyntax() ) {
|
||||
Query q = s.createQuery( "select count(*) from LineItem l where l.pk = (:order, :product)" )
|
||||
.setParameter( "order", o )
|
||||
.setParameter( "product", "my-product" );
|
||||
count = extractCount( q );
|
||||
assertEquals( "LineItem by pk prop (named composite identifier", 1, count );
|
||||
}
|
||||
|
||||
if ( getDialect().supportsRowValueConstructorSyntax() ) {
|
||||
Query q = s.createQuery( "select count(*) from LineItem l where l.pk = (:order, :product)" )
|
||||
.setEntity( "order", o )
|
||||
.setString( "product", "my-product" );
|
||||
count = extractCount( q );
|
||||
assertEquals( "LineItem by pk prop (named composite identifier", 1, count );
|
||||
}
|
||||
count = extractCount( s, "select count(*) from Order o where o.orderee.id = 1" );
|
||||
assertEquals( 0, count );
|
||||
count = extractCount( s, "select count(*) from Order o where o.orderee.pk = 1" );
|
||||
assertEquals( 1, count );
|
||||
count = extractCount( s, "select count(*) from Order o where o.orderee.id = 123" );
|
||||
assertEquals( 1, count );
|
||||
|
||||
count = extractCount( s, "select count(*) from Order o where o.orderee.id = 1" );
|
||||
assertEquals( 0, count );
|
||||
count = extractCount( s, "select count(*) from Order o where o.orderee.pk = 1" );
|
||||
assertEquals( 1, count );
|
||||
count = extractCount( s, "select count(*) from Order o where o.orderee.id = 123" );
|
||||
assertEquals( 1, count );
|
||||
count = extractCount( s, "select count(*) from LineItem l where l.pk.order.id = 1" );
|
||||
assertEquals( 1, count );
|
||||
count = extractCount( s, "select count(*) from LineItem l where l.pk.order.number = 1" );
|
||||
assertEquals( 1, count );
|
||||
count = extractCount( s, "select count(*) from LineItem l where l.pk.order.orderee.pk = 1" );
|
||||
assertEquals( 1, count );
|
||||
|
||||
count = extractCount( s, "select count(*) from LineItem l where l.pk.order.id = 1" );
|
||||
assertEquals( 1, count );
|
||||
count = extractCount( s, "select count(*) from LineItem l where l.pk.order.number = 1" );
|
||||
assertEquals( 1, count );
|
||||
count = extractCount( s, "select count(*) from LineItem l where l.pk.order.orderee.pk = 1" );
|
||||
assertEquals( 1, count );
|
||||
|
||||
s.delete( o );
|
||||
s.delete( p );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCriteriaIdPropertyReferences() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Person p = new Person( new Long(1), "steve", 123 );
|
||||
s.save( p );
|
||||
Order o = new Order( new Long(1), p );
|
||||
LineItem l = new LineItem( o, "my-product", 2 );
|
||||
l.setId( "456" );
|
||||
s.save( o );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
|
||||
Criteria crit = s.createCriteria( Person.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "id", new Integer(123) ) );
|
||||
long count = extractCount( crit );
|
||||
assertEquals( "Person by id prop (non-identifier)", 1, count );
|
||||
|
||||
crit = s.createCriteria( Person.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "pk", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( "Person by pk prop (identifier)", 1, count );
|
||||
|
||||
crit = s.createCriteria( Order.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "number", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( "Order by number prop (named identifier)", 1, count );
|
||||
|
||||
crit = s.createCriteria( Order.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "id", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( "Order by id prop (virtual identifier)", 1, count );
|
||||
|
||||
crit = s.createCriteria( LineItem.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "id", "456" ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( "LineItem by id prop (non-identifier", 1, count );
|
||||
|
||||
if ( getDialect().supportsRowValueConstructorSyntax() ) {
|
||||
crit = s.createCriteria( LineItem.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "pk", new LineItemPK( o, "my-product" ) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( "LineItem by pk prop (named composite identifier)", 1, count );
|
||||
}
|
||||
|
||||
crit = s.createCriteria( Order.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.createAlias( "orderee", "p" ).add( Restrictions.eq( "p.id", new Integer(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( 0, count );
|
||||
|
||||
crit = s.createCriteria( Order.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.createAlias( "orderee", "p" ).add( Restrictions.eq( "p.pk", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( 1, count );
|
||||
|
||||
crit = s.createCriteria( Order.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.createAlias( "orderee", "p" ).add( Restrictions.eq( "p.id", new Integer(123) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( 1, count );
|
||||
|
||||
crit = s.createCriteria( LineItem.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "pk.order.id", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( 1, count );
|
||||
|
||||
crit = s.createCriteria( LineItem.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "pk.order.number", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( 1, count );
|
||||
|
||||
s.delete( o );
|
||||
s.delete( p );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
s.delete( o );
|
||||
s.delete( p );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private long extractCount(Session s, String hql) {
|
||||
@ -175,10 +87,11 @@ private long extractCount(Session s, String hql) {
|
||||
}
|
||||
|
||||
private long extractCount(Query query) {
|
||||
return ( ( Long ) query.list().get( 0 ) ).longValue();
|
||||
return ( (Long) query.list().get( 0 ) ).longValue();
|
||||
}
|
||||
|
||||
private long extractCount(Criteria crit) {
|
||||
return ( ( Long ) crit.list().get( 0 ) ).longValue();
|
||||
private long extractCount(Session s, CriteriaQuery crit) {
|
||||
Query query = s.createQuery( crit );
|
||||
return ( (Long) query.list().get( 0 ) ).longValue();
|
||||
}
|
||||
}
|
||||
|
@ -12,12 +12,16 @@
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import javax.jws.soap.SOAPBinding;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.jdbc.AbstractWork;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
@ -40,111 +44,109 @@ public String[] getMappings() {
|
||||
@Override
|
||||
protected void configure(Configuration configuration) {
|
||||
super.afterConfigurationBuilt( configuration );
|
||||
configuration.setProperty( AvailableSettings.JDBC_TYLE_PARAMS_ZERO_BASE, "true" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSequentialSelects() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
|
||||
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);
|
||||
|
||||
Person yomomma = new Person();
|
||||
yomomma.setName("mum");
|
||||
yomomma.setSex('F');
|
||||
|
||||
s.save(yomomma);
|
||||
s.save(mark);
|
||||
s.save(joe);
|
||||
|
||||
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 is null").list().size(), 1 );
|
||||
assertEquals( s.createQuery("from Person p where p.class = Customer").list().size(), 1 );
|
||||
assertTrue(s.createQuery("from Customer c").list().size()==1);
|
||||
s.clear();
|
||||
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");
|
||||
|
||||
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();
|
||||
|
||||
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);
|
||||
|
||||
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.zip = '30306'").list().size(), 1 );
|
||||
s.delete(mark);
|
||||
s.delete(joe);
|
||||
s.delete(yomomma);
|
||||
assertTrue( s.createQuery("from Person").list().isEmpty() );
|
||||
t.commit();
|
||||
s.close();
|
||||
Person yomomma = new Person();
|
||||
yomomma.setName("mum");
|
||||
yomomma.setSex('F');
|
||||
|
||||
s.save(yomomma);
|
||||
s.save(mark);
|
||||
s.save(joe);
|
||||
|
||||
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 is null").list().size(), 1 );
|
||||
assertEquals( s.createQuery("from Person p where p.class = Customer").list().size(), 1 );
|
||||
assertTrue(s.createQuery("from Customer c").list().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();
|
||||
|
||||
|
||||
mark = s.get( Employee.class, new Long( mark.getId() ) );
|
||||
joe = s.get( Customer.class, new Long( joe.getId() ) );
|
||||
|
||||
mark.setZip("30306");
|
||||
assertEquals( s.createQuery("from Person p where p.zip = '30306'").list().size(), 1 );
|
||||
s.delete(mark);
|
||||
s.delete(joe);
|
||||
s.delete(yomomma);
|
||||
assertTrue( s.createQuery("from Person").list().isEmpty() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSequentialSelectsOptionalData() throws Exception {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
public void testSequentialSelectsOptionalData() {
|
||||
inTransaction(
|
||||
s -> {
|
||||
User jesus = new User();
|
||||
jesus.setName("Jesus Olvera y Martinez");
|
||||
jesus.setSex('M');
|
||||
|
||||
User jesus = new User();
|
||||
jesus.setName("Jesus Olvera y Martinez");
|
||||
jesus.setSex('M');
|
||||
s.save(jesus);
|
||||
|
||||
s.save(jesus);
|
||||
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(), 1 );
|
||||
assertEquals( s.createQuery("from Person p where p.class is null").list().size(), 0 );
|
||||
assertEquals( s.createQuery("from Person p where p.class = User").list().size(), 1 );
|
||||
assertTrue(s.createQuery("from User u").list().size()==1);
|
||||
s.clear();
|
||||
assertEquals( s.createQuery("from Person").list().size(), 1 );
|
||||
assertEquals( s.createQuery("from Person p where p.class is null").list().size(), 0 );
|
||||
assertEquals( s.createQuery("from Person p where p.class = User").list().size(), 1 );
|
||||
assertTrue(s.createQuery("from User u").list().size()==1);
|
||||
s.clear();
|
||||
|
||||
// Remove the optional row from the join table and requery the User obj
|
||||
doWork(s);
|
||||
s.clear();
|
||||
// Remove the optional row from the join table and requery the User obj
|
||||
doWork(s);
|
||||
s.clear();
|
||||
|
||||
jesus = (User) s.get( Person.class, new Long( jesus.getId() ) );
|
||||
s.clear();
|
||||
jesus = (User) s.get( Person.class, new Long( jesus.getId() ) );
|
||||
s.clear();
|
||||
|
||||
// Cleanup the test data
|
||||
s.delete(jesus);
|
||||
// Cleanup the test data
|
||||
s.delete(jesus);
|
||||
|
||||
assertTrue( s.createQuery("from Person").list().isEmpty() );
|
||||
t.commit();
|
||||
s.close();
|
||||
assertTrue( s.createQuery("from Person").list().isEmpty() );
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void doWork(final Session s) {
|
||||
@ -161,83 +163,88 @@ public void execute(Connection connection) throws SQLException {
|
||||
|
||||
@Test
|
||||
public void testCustomColumnReadAndWrite() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
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);
|
||||
final double PASSWORD_EXPIRY_WEEKS = 4;
|
||||
final double PASSWORD_EXPIRY_DAYS = PASSWORD_EXPIRY_WEEKS * 7d;
|
||||
User u = new User();
|
||||
u.setName("Steve");
|
||||
u.setSex('M');
|
||||
u.setPasswordExpiryDays(PASSWORD_EXPIRY_DAYS);
|
||||
s.persist(u);
|
||||
s.flush();
|
||||
|
||||
// Test value conversion during insert
|
||||
// Oracle returns BigDecimaal while other dialects return Double;
|
||||
// casting to Number so it works on all dialects
|
||||
Number heightViaSql = (Number)s.createSQLQuery("select height_centimeters from person where name='Emmanuel'").uniqueResult();
|
||||
assertEquals(HEIGHT_CENTIMETERS, heightViaSql.doubleValue(), 0.01d);
|
||||
Number expiryViaSql = (Number)s.createSQLQuery("select pwd_expiry_weeks from t_user where person_id=?")
|
||||
.setLong(0, u.getId())
|
||||
.uniqueResult();
|
||||
assertEquals(PASSWORD_EXPIRY_WEEKS, expiryViaSql.doubleValue(), 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 u.passwordExpiryDays from User u where u.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);
|
||||
u = (User)s.createCriteria(User.class)
|
||||
.add(Restrictions.between("passwordExpiryDays", PASSWORD_EXPIRY_DAYS - 0.01d, PASSWORD_EXPIRY_DAYS + 0.01d))
|
||||
.uniqueResult();
|
||||
assertEquals(PASSWORD_EXPIRY_DAYS, u.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();
|
||||
assertNotNull( p );
|
||||
assertEquals(HEIGHT_INCHES, p.getHeightInches(), 0.01d);
|
||||
u = (User)s.createQuery("from User u where u.passwordExpiryDays between ?1 and ?2")
|
||||
.setDouble(1, PASSWORD_EXPIRY_DAYS - 0.01d)
|
||||
.setDouble(2, PASSWORD_EXPIRY_DAYS + 0.01d)
|
||||
.uniqueResult();
|
||||
assertEquals(PASSWORD_EXPIRY_DAYS, u.getPasswordExpiryDays(), 0.01d);
|
||||
|
||||
// Test update
|
||||
p.setHeightInches(1);
|
||||
u.setPasswordExpiryDays(7d);
|
||||
s.flush();
|
||||
heightViaSql = (Number)s.createSQLQuery("select height_centimeters from person where name='Emmanuel'").uniqueResult();
|
||||
assertEquals(2.54d, heightViaSql.doubleValue(), 0.01d);
|
||||
expiryViaSql = (Number)s.createSQLQuery("select pwd_expiry_weeks from t_user where person_id=?")
|
||||
.setLong(0, u.getId())
|
||||
.uniqueResult();
|
||||
assertEquals(1d, expiryViaSql.doubleValue(), 0.01d);
|
||||
|
||||
s.delete(p);
|
||||
s.delete(u);
|
||||
assertTrue( s.createQuery("from Person").list().isEmpty() );
|
||||
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
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);
|
||||
final double PASSWORD_EXPIRY_WEEKS = 4;
|
||||
final double PASSWORD_EXPIRY_DAYS = PASSWORD_EXPIRY_WEEKS * 7d;
|
||||
User u = new User();
|
||||
u.setName("Steve");
|
||||
u.setSex('M');
|
||||
u.setPasswordExpiryDays(PASSWORD_EXPIRY_DAYS);
|
||||
s.persist(u);
|
||||
s.flush();
|
||||
|
||||
// Test value conversion during insert
|
||||
// Oracle returns BigDecimaal while other dialects return Double;
|
||||
// casting to Number so it works on all dialects
|
||||
Number heightViaSql = (Number)s.createNativeQuery("select height_centimeters from person where name='Emmanuel'").uniqueResult();
|
||||
assertEquals(HEIGHT_CENTIMETERS, heightViaSql.doubleValue(), 0.01d);
|
||||
Number expiryViaSql = (Number)s.createNativeQuery("select pwd_expiry_weeks from t_user where person_id=?")
|
||||
.setParameter(1, u.getId())
|
||||
.uniqueResult();
|
||||
assertEquals(PASSWORD_EXPIRY_WEEKS, expiryViaSql.doubleValue(), 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 u.passwordExpiryDays from User u where u.name = 'Steve'").uniqueResult();
|
||||
assertEquals(PASSWORD_EXPIRY_DAYS, expiryViaHql, 0.01d);
|
||||
|
||||
// Test restriction and entity load via criteria
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Person> personCriteria = criteriaBuilder.createQuery( Person.class );
|
||||
Root<Person> rootPerson = personCriteria.from( Person.class );
|
||||
personCriteria.where( criteriaBuilder.between( rootPerson.get( "heightInches" ),HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d ) );
|
||||
p = s.createQuery( personCriteria ).uniqueResult();
|
||||
// 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);
|
||||
CriteriaQuery<User> userCriteria = criteriaBuilder.createQuery( User.class );
|
||||
Root<User> userRoot = userCriteria.from( User.class );
|
||||
userCriteria.where( criteriaBuilder.between( userRoot.get( "passwordExpiryDays" ), PASSWORD_EXPIRY_DAYS - 0.01d, PASSWORD_EXPIRY_DAYS + 0.01d ) );
|
||||
u = s.createQuery( userCriteria ).uniqueResult();
|
||||
// u = (User)s.createCriteria(User.class)
|
||||
// .add(Restrictions.between("passwordExpiryDays", PASSWORD_EXPIRY_DAYS - 0.01d, PASSWORD_EXPIRY_DAYS + 0.01d))
|
||||
// .uniqueResult();
|
||||
assertEquals(PASSWORD_EXPIRY_DAYS, u.getPasswordExpiryDays(), 0.01d);
|
||||
|
||||
// Test predicate and entity load via HQL
|
||||
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();
|
||||
assertNotNull( p );
|
||||
assertEquals(HEIGHT_INCHES, p.getHeightInches(), 0.01d);
|
||||
u = (User)s.createQuery("from User u where u.passwordExpiryDays between ?1 and ?2")
|
||||
.setParameter(1, PASSWORD_EXPIRY_DAYS - 0.01d)
|
||||
.setParameter(2, PASSWORD_EXPIRY_DAYS + 0.01d)
|
||||
.uniqueResult();
|
||||
assertEquals(PASSWORD_EXPIRY_DAYS, u.getPasswordExpiryDays(), 0.01d);
|
||||
|
||||
// Test update
|
||||
p.setHeightInches(1);
|
||||
u.setPasswordExpiryDays(7d);
|
||||
s.flush();
|
||||
heightViaSql = (Number)s.createNativeQuery("select height_centimeters from person where name='Emmanuel'").uniqueResult();
|
||||
assertEquals(2.54d, heightViaSql.doubleValue(), 0.01d);
|
||||
expiryViaSql = (Number)s.createNativeQuery("select pwd_expiry_weeks from t_user where person_id=?")
|
||||
.setParameter(1, u.getId())
|
||||
.uniqueResult();
|
||||
assertEquals(1d, expiryViaSql.doubleValue(), 0.01d);
|
||||
|
||||
s.delete(p);
|
||||
s.delete(u);
|
||||
assertTrue( s.createQuery("from Person").list().isEmpty() );
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -6,11 +6,15 @@
|
||||
*/
|
||||
package org.hibernate.test.join;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
@ -24,7 +28,7 @@
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
@TestForIssue( jiraKey = "HHH-11241" )
|
||||
@TestForIssue(jiraKey = "HHH-11241")
|
||||
public class SubclassesWithSamePropertyNameTest extends BaseCoreFunctionalTestCase {
|
||||
private Long blogEntryId;
|
||||
|
||||
@ -35,94 +39,105 @@ public String[] getMappings() {
|
||||
|
||||
@Override
|
||||
protected void prepareTest() {
|
||||
Session s = openSession();
|
||||
s.getTransaction().begin();
|
||||
BlogEntry blogEntry = new BlogEntry();
|
||||
blogEntry.setDetail( "detail" );
|
||||
blogEntry.setReportedBy( "John Doe" );
|
||||
s.persist( blogEntry );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
blogEntry.setDetail( "detail" );
|
||||
blogEntry.setReportedBy( "John Doe" );
|
||||
s.persist( blogEntry );
|
||||
}
|
||||
);
|
||||
blogEntryId = blogEntry.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cleanupTest() {
|
||||
Session s = openSession();
|
||||
s.getTransaction().begin();
|
||||
s.createQuery( "delete from BlogEntry" ).executeUpdate();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> s.createQuery( "delete from BlogEntry" ).executeUpdate()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-11241" )
|
||||
@TestForIssue(jiraKey = "HHH-11241")
|
||||
public void testGetSuperclass() {
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
Reportable reportable = s.get( Reportable.class, blogEntryId );
|
||||
assertEquals( "John Doe", reportable.getReportedBy() );
|
||||
assertEquals( "detail", ( (BlogEntry) reportable ).getDetail() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Reportable reportable = s.get( Reportable.class, blogEntryId );
|
||||
assertEquals( "John Doe", reportable.getReportedBy() );
|
||||
assertEquals( "detail", ( (BlogEntry) reportable ).getDetail() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-11241" )
|
||||
@TestForIssue(jiraKey = "HHH-11241")
|
||||
public void testQuerySuperclass() {
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
Reportable reportable = (Reportable) s.createQuery(
|
||||
"from Reportable where reportedBy='John Doe'"
|
||||
).uniqueResult();
|
||||
assertEquals( "John Doe", reportable.getReportedBy() );
|
||||
assertEquals( "detail", ( (BlogEntry) reportable ).getDetail() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Reportable reportable = (Reportable) s.createQuery(
|
||||
"from Reportable where reportedBy='John Doe'"
|
||||
).uniqueResult();
|
||||
assertEquals( "John Doe", reportable.getReportedBy() );
|
||||
assertEquals( "detail", ( (BlogEntry) reportable ).getDetail() );
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-11241" )
|
||||
@TestForIssue(jiraKey = "HHH-11241")
|
||||
public void testCriteriaSuperclass() {
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
Reportable reportable =
|
||||
(Reportable) s.createCriteria( Reportable.class, "r" )
|
||||
.add( Restrictions.eq( "r.reportedBy", "John Doe" ) )
|
||||
.uniqueResult();
|
||||
assertEquals( "John Doe", reportable.getReportedBy() );
|
||||
assertEquals( "detail", ( (BlogEntry) reportable ).getDetail() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Reportable> criteria = criteriaBuilder.createQuery( Reportable.class );
|
||||
Root<Reportable> root = criteria.from( Reportable.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get( "reportedBy" ),"John Doe" ) );
|
||||
Reportable reportable = s.createQuery( criteria ).uniqueResult();
|
||||
// Reportable reportable =
|
||||
// (Reportable) s.createCriteria( Reportable.class, "r" )
|
||||
// .add( Restrictions.eq( "r.reportedBy", "John Doe" ) )
|
||||
// .uniqueResult();
|
||||
assertEquals( "John Doe", reportable.getReportedBy() );
|
||||
assertEquals( "detail", ( (BlogEntry) reportable ).getDetail() );
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-11241" )
|
||||
@TestForIssue(jiraKey = "HHH-11241")
|
||||
public void testQuerySubclass() {
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
BlogEntry blogEntry = (BlogEntry) s.createQuery(
|
||||
"from BlogEntry where reportedBy='John Doe'"
|
||||
).uniqueResult();
|
||||
assertEquals( "John Doe", blogEntry.getReportedBy() );
|
||||
assertEquals( "detail", ( blogEntry ).getDetail() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
BlogEntry blogEntry = (BlogEntry) s.createQuery(
|
||||
"from BlogEntry where reportedBy='John Doe'"
|
||||
).uniqueResult();
|
||||
assertEquals( "John Doe", blogEntry.getReportedBy() );
|
||||
assertEquals( "detail", ( blogEntry ).getDetail() );
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-11241" )
|
||||
@TestForIssue(jiraKey = "HHH-11241")
|
||||
public void testCriteriaSubclass() {
|
||||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
BlogEntry blogEntry =
|
||||
(BlogEntry) s.createCriteria( BlogEntry.class, "r" )
|
||||
.add( Restrictions.eq( "r.reportedBy", "John Doe" ) )
|
||||
.uniqueResult();
|
||||
assertEquals( "John Doe", blogEntry.getReportedBy() );
|
||||
assertEquals( "detail", ( blogEntry ).getDetail() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<BlogEntry> criteria = criteriaBuilder.createQuery( BlogEntry.class );
|
||||
Root<BlogEntry> root = criteria.from( BlogEntry.class );
|
||||
criteria.where( criteriaBuilder.equal( root.get( "reportedBy" ),"John Doe" ) );
|
||||
BlogEntry blogEntry = s.createQuery( criteria ).uniqueResult();
|
||||
// BlogEntry blogEntry =
|
||||
// (BlogEntry) s.createCriteria( BlogEntry.class, "r" )
|
||||
// .add( Restrictions.eq( "r.reportedBy", "John Doe" ) )
|
||||
// .uniqueResult();
|
||||
assertEquals( "John Doe", blogEntry.getReportedBy() );
|
||||
assertEquals( "detail", ( blogEntry ).getDetail() );
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.idprops;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.query.Query;
|
||||
import org.hibernate.Session;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class CriteriaIdPropertyReferencesTest extends BaseCoreFunctionalTestCase {
|
||||
@Override
|
||||
public String[] getMappings() {
|
||||
return new String[] { "idprops/Mapping.hbm.xml" };
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCriteriaIdPropertyReferences() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Person p = new Person( new Long(1), "steve", 123 );
|
||||
s.save( p );
|
||||
Order o = new Order( new Long(1), p );
|
||||
LineItem l = new LineItem( o, "my-product", 2 );
|
||||
l.setId( "456" );
|
||||
s.save( o );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
|
||||
Criteria crit = s.createCriteria( Person.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "id", new Integer(123) ) );
|
||||
long count = extractCount( crit );
|
||||
assertEquals( "Person by id prop (non-identifier)", 1, count );
|
||||
|
||||
crit = s.createCriteria( Person.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "pk", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( "Person by pk prop (identifier)", 1, count );
|
||||
|
||||
crit = s.createCriteria( Order.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "number", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( "Order by number prop (named identifier)", 1, count );
|
||||
|
||||
crit = s.createCriteria( Order.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "id", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( "Order by id prop (virtual identifier)", 1, count );
|
||||
|
||||
crit = s.createCriteria( LineItem.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "id", "456" ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( "LineItem by id prop (non-identifier", 1, count );
|
||||
|
||||
if ( getDialect().supportsRowValueConstructorSyntax() ) {
|
||||
crit = s.createCriteria( LineItem.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "pk", new LineItemPK( o, "my-product" ) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( "LineItem by pk prop (named composite identifier)", 1, count );
|
||||
}
|
||||
|
||||
crit = s.createCriteria( Order.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.createAlias( "orderee", "p" ).add( Restrictions.eq( "p.id", new Integer(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( 0, count );
|
||||
|
||||
crit = s.createCriteria( Order.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.createAlias( "orderee", "p" ).add( Restrictions.eq( "p.pk", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( 1, count );
|
||||
|
||||
crit = s.createCriteria( Order.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.createAlias( "orderee", "p" ).add( Restrictions.eq( "p.id", new Integer(123) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( 1, count );
|
||||
|
||||
crit = s.createCriteria( LineItem.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "pk.order.id", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( 1, count );
|
||||
|
||||
crit = s.createCriteria( LineItem.class );
|
||||
crit.setProjection( Projections.rowCount() );
|
||||
crit.add( Restrictions.eq( "pk.order.number", new Long(1) ) );
|
||||
count = extractCount( crit );
|
||||
assertEquals( 1, count );
|
||||
|
||||
s.delete( o );
|
||||
s.delete( p );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
private long extractCount(Session s, String hql) {
|
||||
return extractCount( s.createQuery( hql ) );
|
||||
}
|
||||
|
||||
private long extractCount(Query query) {
|
||||
return ( (Long) query.list().get( 0 ) ).longValue();
|
||||
}
|
||||
|
||||
private long extractCount(Session s, CriteriaQuery crit) {
|
||||
Query query = s.createQuery( crit );
|
||||
return ( (Long) query.list().get( 0 ) ).longValue();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user