6 - SQM based on JPA type system

This commit is contained in:
Andrea Boriero 2019-07-15 18:20:29 +01:00
parent c50c95a691
commit 0bf8cd1266
8 changed files with 638 additions and 625 deletions

View File

@ -25,7 +25,6 @@ import org.hibernate.Transaction;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.exception.SQLGrammarException;
import org.hibernate.hql.spi.QueryTranslator;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;

View File

@ -11,13 +11,17 @@ import java.util.Date;
import java.util.List;
import java.util.Map;
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.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.dialect.SybaseASE15Dialect;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.mapping.Component;
@ -91,7 +95,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
s = openSession();
t = s.beginTransaction();
u = (User) s.get(User.class, "gavin");
u = s.get(User.class, "gavin");
assertEquals( u.getPerson().getName(), "Gavin King" );
s.delete(u);
t.commit();
@ -113,7 +117,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
s = openSession();
t = s.beginTransaction();
u = (User) s.get(User.class, "gavin");
u = s.get(User.class, "gavin");
assertEquals( u.getPerson().getAddress(), "Phipps Place" );
assertEquals( u.getPerson().getPreviousAddress(), "Karbarook Ave" );
assertEquals( u.getPerson().getYob(), u.getPerson().getDob().getYear()+1900 );
@ -123,7 +127,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
s = openSession();
t = s.beginTransaction();
u = (User) s.get(User.class, "gavin");
u = s.get(User.class, "gavin");
assertEquals( u.getPerson().getAddress(), "Phipps Place" );
assertEquals( u.getPerson().getPreviousAddress(), "Karbarook Ave" );
assertEquals( u.getPassword(), "$ecret" );
@ -204,14 +208,22 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery("from User u where u.person.yob = 1999").list();
s.createCriteria(User.class)
.add( Property.forName("person.yob").between( new Integer(1999), new Integer(2002) ) )
.list();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<User> criteria = criteriaBuilder.createQuery( User.class );
Root<User> root = criteria.from( User.class );
Join<Object, Object> person = root.join( "person", JoinType.INNER );
criteria.where( criteriaBuilder.between( person.get( "yob" ), new Integer(1999), new Integer(2002) ) );
s.createQuery( criteria ).list();
// s.createCriteria(User.class)
// .add( Property.forName("person.yob").between( new Integer(1999), new Integer(2002) ) )
// .list();
if ( getDialect().supportsRowValueConstructorSyntax() ) {
s.createQuery("from User u where u.person = ('gavin', :dob, 'Peachtree Rd', 'Karbarook Ave', 1974, 34, 'Peachtree Rd')")
.setDate("dob", new Date("March 25, 1974")).list();
.setParameter("dob", new Date("March 25, 1974")).list();
s.createQuery("from User where person = ('gavin', :dob, 'Peachtree Rd', 'Karbarook Ave', 1974, 34, 'Peachtree Rd')")
.setDate("dob", new Date("March 25, 1974")).list();
.setParameter("dob", new Date("March 25, 1974")).list();
}
t.commit();
s.close();
@ -232,7 +244,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
// 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 T_USER where T_USER.username='steve'").uniqueResult())
( (Number)s.createNativeQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult())
.doubleValue();
assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d);
@ -241,15 +253,21 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
assertEquals(HEIGHT_INCHES, heightViaHql, 0.01d);
// Test restriction and entity load via criteria
u = (User)s.createCriteria(User.class)
.add(Restrictions.between("person.heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d))
.uniqueResult();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<User> criteria = criteriaBuilder.createQuery( User.class );
Root<User> root = criteria.from( User.class );
Join<Object, Object> person = root.join( "person", JoinType.INNER );
criteria.where( criteriaBuilder.between( person.get( "heightInches" ), HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d) );
u = s.createQuery( criteria ).uniqueResult();
// u = (User)s.createCriteria(User.class)
// .add(Restrictions.between("person.heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d))
// .uniqueResult();
assertEquals(HEIGHT_INCHES, u.getPerson().getHeightInches(), 0.01d);
// Test predicate and entity load via HQL
u = (User)s.createQuery("from User u where u.person.heightInches between ?1 and ?2")
.setDouble(1, HEIGHT_INCHES - 0.01d)
.setDouble(2, HEIGHT_INCHES + 0.01d)
.setParameter(1, HEIGHT_INCHES - 0.01d)
.setParameter(2, HEIGHT_INCHES + 0.01d)
.uniqueResult();
assertEquals(HEIGHT_INCHES, u.getPerson().getHeightInches(), 0.01d);
@ -257,7 +275,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
u.getPerson().setHeightInches(1);
s.flush();
heightViaSql =
( (Number)s.createSQLQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult() )
( (Number)s.createNativeQuery("select height_centimeters from T_USER where T_USER.username='steve'").uniqueResult() )
.doubleValue();
assertEquals(2.54d, heightViaSql, 0.01d);
s.delete(u);
@ -291,7 +309,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
s = openSession();
t = s.beginTransaction();
emp = (Employee)s.get( Employee.class, emp.getId() );
emp = s.get( Employee.class, emp.getId() );
t.commit();
s.close();
@ -308,7 +326,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
s = openSession();
t = s.beginTransaction();
emp = (Employee)s.get( Employee.class, emp.getId() );
emp = s.get( Employee.class, emp.getId() );
t.commit();
s.close();
@ -325,7 +343,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
s = openSession();
t = s.beginTransaction();
emp = (Employee)s.get( Employee.class, emp.getId() );
emp = s.get( Employee.class, emp.getId() );
Hibernate.initialize(emp.getDirectReports());
t.commit();
s.close();
@ -347,7 +365,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
s = openSession();
t = s.beginTransaction();
emp = (Employee)s.get( Employee.class, emp.getId() );
emp = s.get( Employee.class, emp.getId() );
Hibernate.initialize(emp.getDirectReports());
t.commit();
s.close();
@ -367,7 +385,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
s = openSession();
t = s.beginTransaction();
emp = (Employee)s.get( Employee.class, emp.getId() );
emp = s.get( Employee.class, emp.getId() );
Hibernate.initialize(emp.getDirectReports());
t.commit();
s.close();
@ -387,7 +405,7 @@ public class ComponentTest extends BaseNonConfigCoreFunctionalTestCase {
s = openSession();
t = s.beginTransaction();
emp = (Employee)s.get( Employee.class, emp.getId() );
emp = s.get( Employee.class, emp.getId() );
Hibernate.initialize(emp.getDirectReports());
t.commit();
s.close();

View File

@ -8,7 +8,6 @@ package org.hibernate.test.flush;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import org.hibernate.criterion.Restrictions;
import org.hibernate.test.hql.SimpleEntityWithAssociation;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;

View File

@ -16,7 +16,6 @@ import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Projections;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;

View File

@ -14,12 +14,8 @@ import org.hibernate.Session;
import org.hibernate.StaleObjectStateException;
import org.hibernate.StaleStateException;
import org.hibernate.Transaction;
import org.hibernate.TransactionException;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;

View File

@ -18,7 +18,6 @@ import org.hibernate.NonUniqueObjectException;
import org.hibernate.Session;
import org.hibernate.StaleObjectStateException;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.dialect.AbstractHANADialect;
import org.hibernate.testing.DialectChecks;

View File

@ -13,7 +13,6 @@ import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.criterion.Projections;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;