6 - SQM based on JPA type system

This commit is contained in:
Andrea Boriero 2019-07-15 15:38:15 +01:00
parent 40fb8dc254
commit c50c95a691
17 changed files with 90 additions and 61 deletions

View File

@ -17,7 +17,8 @@ import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory; import org.hibernate.query.hql.SemanticQueryProducer;
import org.hibernate.testing.FailureExpected; import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -54,7 +55,7 @@ public class ASTParserLoadingOrderByTest extends BaseCoreFunctionalTestCase {
super.configure( cfg ); super.configure( cfg );
cfg.setProperty( Environment.USE_QUERY_CACHE, "false" ); cfg.setProperty( Environment.USE_QUERY_CACHE, "false" );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
cfg.setProperty( Environment.QUERY_TRANSLATOR, ASTQueryTranslatorFactory.class.getName() ); cfg.setProperty( Environment.SEMANTIC_QUERY_PRODUCER, SemanticQueryProducer.class.getName() );
} }
private void createData() { private void createData() {

View File

@ -14,14 +14,11 @@ import javax.persistence.Table;
import org.junit.Test; import org.junit.Test;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.hql.internal.ast.QuerySyntaxException;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/** /**
* @author Gail Badner * @author Gail Badner

View File

@ -15,6 +15,7 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalId;
import org.hibernate.dialect.SybaseDialect; import org.hibernate.dialect.SybaseDialect;
@ -154,24 +155,25 @@ public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase {
@Test @Test
@TestForIssue(jiraKey = "HHH-11538") @TestForIssue(jiraKey = "HHH-11538")
public void testNoImpliedJoinGeneratedForEqualityComparison() { public void testNoImpliedJoinGeneratedForEqualityComparison() {
doInHibernate( this::sessionFactory, session -> { // doInHibernate( this::sessionFactory, session -> {
final HQLQueryPlan plan = sessionFactory().getQueryInterpretationCache().getHQLQueryPlan( // final HQLQueryPlan plan = sessionFactory().getQueryInterpretationCache().getHQLQueryPlan(
"select r.id, cust.name " + // "select r.id, cust.name " +
"from FinancialRecord r " + // "from FinancialRecord r " +
" join Customer cust on r.customer = cust" + // " join Customer cust on r.customer = cust" +
" order by r.id", // " order by r.id",
false, // false,
Collections.EMPTY_MAP // Collections.EMPTY_MAP
); // );
assertEquals( 1, plan.getTranslators().length ); // assertEquals( 1, plan.getTranslators().length );
final QueryTranslator translator = plan.getTranslators()[0]; // final QueryTranslator translator = plan.getTranslators()[0];
final String generatedSql = translator.getSQLString(); // final String generatedSql = translator.getSQLString();
//
int tableReferenceIndex = generatedSql.indexOf( " customer " ); // int tableReferenceIndex = generatedSql.indexOf( " customer " );
assertNotEquals("Generated SQL doesn't contain a table reference for customer", -1, tableReferenceIndex ); // assertNotEquals("Generated SQL doesn't contain a table reference for customer", -1, tableReferenceIndex );
int nextTableReferenceIndex = generatedSql.indexOf( " customer ", tableReferenceIndex + 1 ); // int nextTableReferenceIndex = generatedSql.indexOf( " customer ", tableReferenceIndex + 1 );
assertEquals("Generated SQL wrongly joined customer twice", -1, nextTableReferenceIndex ); // assertEquals("Generated SQL wrongly joined customer twice", -1, nextTableReferenceIndex );
} ); // } );
throw new NotYetImplementedFor6Exception( getClass() );
} }
@Test @Test

View File

@ -8,13 +8,17 @@ package org.hibernate.test.hql;
import java.util.List; import java.util.List;
import org.hibernate.FetchMode; 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.Hibernate;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Restrictions;
import org.hibernate.dialect.PostgresPlusDialect; import org.hibernate.dialect.PostgresPlusDialect;
import org.hibernate.dialect.SybaseASE15Dialect; import org.hibernate.dialect.SybaseASE15Dialect;
import org.hibernate.dialect.function.SQLFunction; import org.hibernate.dialect.function.SQLFunction;
@ -84,7 +88,13 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
t = s.beginTransaction(); t = s.beginTransaction();
e = ( EntityWithArgFunctionAsColumn ) s.createCriteria( EntityWithArgFunctionAsColumn.class ).uniqueResult(); CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<EntityWithArgFunctionAsColumn> criteria = criteriaBuilder.createQuery( EntityWithArgFunctionAsColumn.class );
criteria.from( EntityWithArgFunctionAsColumn.class );
e = s.createQuery( criteria ).uniqueResult();
// e = ( EntityWithArgFunctionAsColumn ) s.createCriteria( EntityWithArgFunctionAsColumn.class ).uniqueResult();
assertEquals( 3, e.getLower() ); assertEquals( 3, e.getLower() );
assertEquals( " abc ", e.getUpper() ); assertEquals( " abc ", e.getUpper() );
t.commit(); t.commit();
@ -157,12 +167,23 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
t = s.beginTransaction(); t = s.beginTransaction();
holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class ) CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
.add( Restrictions.isNotNull( "nextHolder" )) CriteriaQuery<EntityWithFunctionAsColumnHolder> criteria = criteriaBuilder.createQuery(
.setFetchMode( "entityWithArgFunctionAsColumns", FetchMode.JOIN ) EntityWithFunctionAsColumnHolder.class );
.setFetchMode( "nextHolder", FetchMode.JOIN ) Root<EntityWithFunctionAsColumnHolder> root = criteria.from( EntityWithFunctionAsColumnHolder.class );
.setFetchMode( "nextHolder.entityWithArgFunctionAsColumns", FetchMode.JOIN ) root.join( "entityWithArgFunctionAsColumns", JoinType.LEFT );
.uniqueResult(); Join<Object, Object> nextHolder = root.join( "nextHolder", JoinType.LEFT );
nextHolder.join( "entityWithArgFunctionAsColumns", JoinType.LEFT );
criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) );
holder1 = s.createQuery( criteria ).uniqueResult();
// holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class )
// .add( Restrictions.isNotNull( "nextHolder" ))
// .setFetchMode( "entityWithArgFunctionAsColumns", FetchMode.JOIN )
// .setFetchMode( "nextHolder", FetchMode.JOIN )
// .setFetchMode( "nextHolder.entityWithArgFunctionAsColumns", FetchMode.JOIN )
// .uniqueResult();
assertTrue( Hibernate.isInitialized( holder1.getEntityWithArgFunctionAsColumns() ) ); assertTrue( Hibernate.isInitialized( holder1.getEntityWithArgFunctionAsColumns() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) );
@ -181,7 +202,7 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testGetMultiColumnSameNameAsNoArgFunctionHQL() throws Exception { public void testGetMultiColumnSameNameAsNoArgFunctionHQL() {
SQLFunction function = sessionFactory().getSqlFunctionRegistry().findSQLFunction( "current_date" ); SQLFunction function = sessionFactory().getSqlFunctionRegistry().findSQLFunction( "current_date" );
if ( function == null || function.hasParenthesesIfNoArguments() ) { if ( function == null || function.hasParenthesesIfNoArguments() ) {
SkipLog.reportSkip( "current_date reuires ()", "tests noarg function that does not require ()" ); SkipLog.reportSkip( "current_date reuires ()", "tests noarg function that does not require ()" );
@ -251,12 +272,22 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
s = openSession(); s = openSession();
t = s.beginTransaction(); t = s.beginTransaction();
holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class )
.add( Restrictions.isNotNull( "nextHolder" )) CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
.setFetchMode( "entityWithNoArgFunctionAsColumns", FetchMode.JOIN ) CriteriaQuery<EntityWithFunctionAsColumnHolder> criteria = criteriaBuilder.createQuery(
.setFetchMode( "nextHolder", FetchMode.JOIN ) EntityWithFunctionAsColumnHolder.class );
.setFetchMode( "nextHolder.entityWithNoArgFunctionAsColumns", FetchMode.JOIN ) Root<EntityWithFunctionAsColumnHolder> root = criteria.from( EntityWithFunctionAsColumnHolder.class );
.uniqueResult(); root.join( "entityWithArgFunctionAsColumns", JoinType.LEFT );
Join<Object, Object> nextHolder = root.join( "nextHolder", JoinType.LEFT );
nextHolder.join( "entityWithArgFunctionAsColumns", JoinType.LEFT );
criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) );
// holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class )
// .add( Restrictions.isNotNull( "nextHolder" ))
// .setFetchMode( "entityWithNoArgFunctionAsColumns", FetchMode.JOIN )
// .setFetchMode( "nextHolder", FetchMode.JOIN )
// .setFetchMode( "nextHolder.entityWithNoArgFunctionAsColumns", FetchMode.JOIN )
// .uniqueResult();
assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) ); assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns() ) ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns() ) );

View File

@ -17,7 +17,6 @@ import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.After; import org.junit.After;
@ -33,7 +32,6 @@ import static org.junit.Assert.assertEquals;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class MapFunctionExpressionsTest extends BaseNonConfigCoreFunctionalTestCase { public class MapFunctionExpressionsTest extends BaseNonConfigCoreFunctionalTestCase {
private final ASTQueryTranslatorFactory queryTranslatorFactory = new ASTQueryTranslatorFactory();
@Before @Before
public void prepareTestData() { public void prepareTestData() {

View File

@ -7,8 +7,8 @@
package org.hibernate.test.hql; package org.hibernate.test.hql;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalId;
import org.hibernate.hql.spi.FilterTranslator;
import org.hibernate.query.Query; import org.hibernate.query.Query;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -281,17 +281,18 @@ public class NaturalIdDereferenceTest extends BaseCoreFunctionalTestCase {
} }
private String getSQLQuery(Query query) { private String getSQLQuery(Query query) {
FilterTranslator naturalIdJoinGenerationTest1 = this.sessionFactory() // FilterTranslator naturalIdJoinGenerationTest1 = this.sessionFactory()
.getSettings() // .getSettings()
.getQueryTranslatorFactory() // .getQueryTranslatorFactory()
.createFilterTranslator( // .createFilterTranslator(
"nid", // "nid",
query.getQueryString(), // query.getQueryString(),
Collections.emptyMap(), // Collections.emptyMap(),
this.sessionFactory() // this.sessionFactory()
); // );
naturalIdJoinGenerationTest1.compile( Collections.emptyMap(), false ); // naturalIdJoinGenerationTest1.compile( Collections.emptyMap(), false );
return naturalIdJoinGenerationTest1.getSQLString(); // return naturalIdJoinGenerationTest1.getSQLString();
throw new NotYetImplementedFor6Exception( getClass() );
} }
@Override @Override

View File

@ -8,7 +8,7 @@ package org.hibernate.test.hql;
import java.util.List; import java.util.List;
import org.hibernate.Query; import org.hibernate.query.Query;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;

View File

@ -8,11 +8,9 @@ package org.hibernate.test.hql;
import java.util.List; import java.util.List;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import org.hibernate.Query; import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
@ -44,10 +42,10 @@ public class HibernateFirstResultMaxResultsTest extends BaseNonConfigCoreFunctio
assertNull( query.getHibernateFirstResult() ); assertNull( query.getHibernateFirstResult() );
// the following is special case; when initialized to -1, getHibernateFirstResult returns 0 // the following is special case; when initialized to -1, getHibernateFirstResult returns 0
assertEquals( Integer.valueOf( 0 ), query.setHibernateFirstResult( -1 ).getHibernateFirstResult() ); assertEquals( 0 , query.setFirstResult( -1 ).getFirstResult() );
assertEquals( Integer.valueOf( 0 ), query.setHibernateFirstResult( 0 ).getHibernateFirstResult() ); assertEquals( Integer.valueOf( 0 ), query.setFirstResult( 0 ).getHibernateFirstResult() );
assertEquals( Integer.valueOf( 1 ), query.setHibernateFirstResult( 1 ).getHibernateFirstResult() ); assertEquals( Integer.valueOf( 1 ), query.setFirstResult( 1 ).getHibernateFirstResult() );
assertEquals( Integer.valueOf( 10 ), query.setFirstResult( 10 ).getHibernateFirstResult() ); assertEquals( Integer.valueOf( 10 ), query.setFirstResult( 10 ).getHibernateFirstResult() );
} }

View File

@ -10,7 +10,8 @@ import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import org.hibernate.hql.internal.QuerySplitter;
import org.hibernate.query.hql.internal.QuerySplitter;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;