6 - SQM based on JPA type system
This commit is contained in:
parent
21cd25f520
commit
40fb8dc254
|
@ -15,7 +15,7 @@ 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.engine.spi.SessionFactoryImplementor;
|
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||||
import org.hibernate.hql.internal.ast.QuerySyntaxException;
|
import org.hibernate.query.SemanticException;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ public class ConfigurationTest {
|
||||||
fail( "Boat should not be mapped" );
|
fail( "Boat should not be mapped" );
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException expected) {
|
catch (IllegalArgumentException expected) {
|
||||||
assertTyping( QuerySyntaxException.class, expected.getCause() );
|
assertTyping( SemanticException.class, expected.getCause() );
|
||||||
// expected outcome
|
// expected outcome
|
||||||
|
|
||||||
// see org.hibernate.test.jpa.compliance.tck2_2.QueryApiTest#testInvalidQueryMarksTxnForRollback
|
// see org.hibernate.test.jpa.compliance.tck2_2.QueryApiTest#testInvalidQueryMarksTxnForRollback
|
||||||
|
|
|
@ -12,9 +12,14 @@ import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Join;
|
||||||
|
import javax.persistence.criteria.JoinType;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import org.hibernate.query.Query;
|
import org.hibernate.query.Query;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.criterion.Restrictions;
|
|
||||||
import org.hibernate.dialect.AbstractHANADialect;
|
import org.hibernate.dialect.AbstractHANADialect;
|
||||||
import org.hibernate.dialect.Oracle8iDialect;
|
import org.hibernate.dialect.Oracle8iDialect;
|
||||||
import org.hibernate.engine.spi.SessionImplementor;
|
import org.hibernate.engine.spi.SessionImplementor;
|
||||||
|
@ -98,7 +103,7 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
assertEquals( id, entityEnum.getId() );
|
assertEquals( id, entityEnum.getId() );
|
||||||
assertEquals( Common.A2, entityEnum.getOrdinal() );
|
assertEquals( Common.A2, entityEnum.getOrdinal() );
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum where ordinal=1" ).executeUpdate() );
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum where ordinal=1" ).executeUpdate() );
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
@ -128,7 +133,7 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
assertEquals( id, entityEnum.getId() );
|
assertEquals( id, entityEnum.getId() );
|
||||||
assertEquals( Common.B1, entityEnum.getString() );
|
assertEquals( Common.B1, entityEnum.getString() );
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum where string='B1'" ).executeUpdate() );
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum where string='B1'" ).executeUpdate() );
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
|
||||||
|
@ -157,7 +162,7 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
assertEquals( id, entityEnum.getId() );
|
assertEquals( id, entityEnum.getId() );
|
||||||
assertEquals( FirstLetter.C_LETTER, entityEnum.getFirstLetter() );
|
assertEquals( FirstLetter.C_LETTER, entityEnum.getFirstLetter() );
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum where firstLetter='C'" ).executeUpdate() );
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum where firstLetter='C'" ).executeUpdate() );
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
@ -187,7 +192,7 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
assertEquals( id, entityEnum.getId() );
|
assertEquals( id, entityEnum.getId() );
|
||||||
assertEquals( LastNumber.NUMBER_1, entityEnum.getLastNumber() );
|
assertEquals( LastNumber.NUMBER_1, entityEnum.getLastNumber() );
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum where lastNumber='1'" ).executeUpdate() );
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum where lastNumber='1'" ).executeUpdate() );
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
@ -219,7 +224,7 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
assertEquals( id, entityEnum.getId() );
|
assertEquals( id, entityEnum.getId() );
|
||||||
assertEquals( LastNumber.NUMBER_2, entityEnum.getExplicitOverridingImplicit() );
|
assertEquals( LastNumber.NUMBER_2, entityEnum.getExplicitOverridingImplicit() );
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum where explicitOverridingImplicit='NUMBER_2'" )
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum where explicitOverridingImplicit='NUMBER_2'" )
|
||||||
.executeUpdate() );
|
.executeUpdate() );
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
|
@ -242,12 +247,20 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
session.getTransaction().begin();
|
session.getTransaction().begin();
|
||||||
|
|
||||||
// find
|
// find
|
||||||
entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
|
|
||||||
.add( Restrictions.eq( "ordinal", Common.A1 ) ).uniqueResult();
|
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<EntityEnum> criteria = criteriaBuilder.createQuery( EntityEnum.class );
|
||||||
|
Root<EntityEnum> root = criteria.from( EntityEnum.class );
|
||||||
|
Join<Object, Object> ordinal = root.join( "ordinal", JoinType.INNER );
|
||||||
|
criteria.where( criteriaBuilder.equal( ordinal, Common.A1 ) );
|
||||||
|
entityEnum = session.createQuery( criteria ).uniqueResult();
|
||||||
|
|
||||||
|
// entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
|
||||||
|
// .add( Restrictions.eq( "ordinal", Common.A1 ) ).uniqueResult();
|
||||||
assertEquals( id, entityEnum.getId() );
|
assertEquals( id, entityEnum.getId() );
|
||||||
assertEquals( Common.A1, entityEnum.getOrdinal() );
|
assertEquals( Common.A1, entityEnum.getOrdinal() );
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum where ordinal=0" ).executeUpdate() );
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum where ordinal=0" ).executeUpdate() );
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
@ -267,12 +280,16 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
session.getTransaction().begin();
|
session.getTransaction().begin();
|
||||||
|
|
||||||
// find
|
// find
|
||||||
entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
|
criteria = criteriaBuilder.createQuery( EntityEnum.class );
|
||||||
.add( Restrictions.eq( "string", Common.B2 ) ).uniqueResult();
|
root = criteria.from( EntityEnum.class );
|
||||||
|
criteria.where( criteriaBuilder.equal( root.get( "string" ), Common.B2 ) );
|
||||||
|
entityEnum = session.createQuery( criteria ).uniqueResult();
|
||||||
|
// entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
|
||||||
|
// .add( Restrictions.eq( "string", Common.B2 ) ).uniqueResult();
|
||||||
assertEquals( id, entityEnum.getId() );
|
assertEquals( id, entityEnum.getId() );
|
||||||
assertEquals( Common.B2, entityEnum.getString() );
|
assertEquals( Common.B2, entityEnum.getString() );
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum where string='B2'" ).executeUpdate() );
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum where string='B2'" ).executeUpdate() );
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
@ -292,12 +309,17 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
session.getTransaction().begin();
|
session.getTransaction().begin();
|
||||||
|
|
||||||
// find
|
// find
|
||||||
entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
|
|
||||||
.add( Restrictions.eq( "firstLetter", FirstLetter.A_LETTER ) ).uniqueResult();
|
criteria = criteriaBuilder.createQuery( EntityEnum.class );
|
||||||
|
root = criteria.from( EntityEnum.class );
|
||||||
|
criteria.where( criteriaBuilder.equal( root.get( "firstLetter" ), FirstLetter.A_LETTER));
|
||||||
|
entityEnum = session.createQuery( criteria).uniqueResult();
|
||||||
|
// entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
|
||||||
|
// .add( Restrictions.eq( "firstLetter", FirstLetter.A_LETTER ) ).uniqueResult();
|
||||||
assertEquals( id, entityEnum.getId() );
|
assertEquals( id, entityEnum.getId() );
|
||||||
assertEquals( FirstLetter.A_LETTER, entityEnum.getFirstLetter() );
|
assertEquals( FirstLetter.A_LETTER, entityEnum.getFirstLetter() );
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum where firstLetter='A'" ).executeUpdate() );
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum where firstLetter='A'" ).executeUpdate() );
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
@ -317,12 +339,16 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
session.getTransaction().begin();
|
session.getTransaction().begin();
|
||||||
|
|
||||||
// find
|
// find
|
||||||
entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
|
criteria = criteriaBuilder.createQuery( EntityEnum.class );
|
||||||
.add( Restrictions.eq( "lastNumber", LastNumber.NUMBER_3 ) ).uniqueResult();
|
root = criteria.from( EntityEnum.class );
|
||||||
|
criteria.where( criteriaBuilder.equal( root.get( "lastNumber" ), LastNumber.NUMBER_3 ) );
|
||||||
|
entityEnum = session.createQuery( criteria ).uniqueResult();
|
||||||
|
// entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
|
||||||
|
// .add( Restrictions.eq( "lastNumber", LastNumber.NUMBER_3 ) ).uniqueResult();
|
||||||
assertEquals( id, entityEnum.getId() );
|
assertEquals( id, entityEnum.getId() );
|
||||||
assertEquals( LastNumber.NUMBER_3, entityEnum.getLastNumber() );
|
assertEquals( LastNumber.NUMBER_3, entityEnum.getLastNumber() );
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum where lastNumber='3'" ).executeUpdate() );
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum where lastNumber='3'" ).executeUpdate() );
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
@ -342,12 +368,16 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
session.getTransaction().begin();
|
session.getTransaction().begin();
|
||||||
|
|
||||||
// find
|
// find
|
||||||
entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
|
criteria = criteriaBuilder.createQuery( EntityEnum.class );
|
||||||
.add( Restrictions.eq( "explicitOverridingImplicit", LastNumber.NUMBER_2 ) ).uniqueResult();
|
root = criteria.from( EntityEnum.class );
|
||||||
|
criteria.where( criteriaBuilder.equal( root.get( "explicitOverridingImplicit" ), LastNumber.NUMBER_2 ) );
|
||||||
|
entityEnum = session.createQuery( criteria ).uniqueResult();
|
||||||
|
// entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
|
||||||
|
// .add( Restrictions.eq( "explicitOverridingImplicit", LastNumber.NUMBER_2 ) ).uniqueResult();
|
||||||
assertEquals( id, entityEnum.getId() );
|
assertEquals( id, entityEnum.getId() );
|
||||||
assertEquals( LastNumber.NUMBER_2, entityEnum.getExplicitOverridingImplicit() );
|
assertEquals( LastNumber.NUMBER_2, entityEnum.getExplicitOverridingImplicit() );
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum where explicitOverridingImplicit='NUMBER_2'" )
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum where explicitOverridingImplicit='NUMBER_2'" )
|
||||||
.executeUpdate() );
|
.executeUpdate() );
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
|
@ -443,8 +473,8 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
assertEquals( Common.B2, entityEnum.getSet().iterator().next() );
|
assertEquals( Common.B2, entityEnum.getSet().iterator().next() );
|
||||||
|
|
||||||
// delete
|
// delete
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM set_enum" ).executeUpdate() );
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM set_enum" ).executeUpdate() );
|
||||||
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum" ).executeUpdate() );
|
assertEquals( 1, session.createNativeQuery( "DELETE FROM EntityEnum" ).executeUpdate() );
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
|
|
@ -6,17 +6,20 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.annotations.naturalid;
|
package org.hibernate.test.annotations.naturalid;
|
||||||
|
|
||||||
|
import javax.persistence.FlushModeType;
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.JoinType;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.hibernate.FetchMode;
|
|
||||||
import org.hibernate.FlushMode;
|
|
||||||
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.query.Query;
|
||||||
import org.hibernate.sql.JoinType;
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
|
||||||
|
@ -38,7 +41,7 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
newTx.commit();
|
newTx.commit();
|
||||||
|
|
||||||
newTx = s.beginTransaction();
|
newTx = s.beginTransaction();
|
||||||
getCriteria( s ).uniqueResult(); // put query-result into cache
|
getQuery( s ).uniqueResult(); // put query-result into cache
|
||||||
A a2 = new A();
|
A a2 = new A();
|
||||||
a2.setName( "xxxxxx" );
|
a2.setName( "xxxxxx" );
|
||||||
s.persist( a2 );
|
s.persist( a2 );
|
||||||
|
@ -53,7 +56,7 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
Assert.assertTrue( s.getSessionFactory().getStatistics().isStatisticsEnabled() );
|
Assert.assertTrue( s.getSessionFactory().getStatistics().isStatisticsEnabled() );
|
||||||
s.getSessionFactory().getStatistics().clear();
|
s.getSessionFactory().getStatistics().clear();
|
||||||
|
|
||||||
getCriteria( s ).uniqueResult(); // should produce a hit in StandardQuery cache region
|
getQuery( s ).uniqueResult(); // should produce a hit in StandardQuery cache region
|
||||||
|
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
"query is not considered as isImmutableNaturalKeyLookup, despite fullfilling all conditions",
|
"query is not considered as isImmutableNaturalKeyLookup, despite fullfilling all conditions",
|
||||||
|
@ -79,7 +82,17 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
newTx.commit();
|
newTx.commit();
|
||||||
|
|
||||||
newTx = s.beginTransaction();
|
newTx = s.beginTransaction();
|
||||||
getCriteria( s ).add( Restrictions.isNull( "singleD" ) ).uniqueResult(); // put query-result into cache
|
|
||||||
|
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<A> criteria = criteriaBuilder.createQuery( A.class );
|
||||||
|
Root<A> root = criteria.from( A.class );
|
||||||
|
criteria.where( criteriaBuilder.and( criteriaBuilder.equal( root.get( "name" ), "name1" ), criteriaBuilder.isNull( root.get( "singleD" ) )) );
|
||||||
|
|
||||||
|
Query<A> query = session.createQuery( criteria );
|
||||||
|
query.setFlushMode( FlushModeType.COMMIT );
|
||||||
|
query.setCacheable( true );
|
||||||
|
query.uniqueResult();
|
||||||
|
// getCriteria( s ).add( Restrictions.isNull( "singleD" ) ).uniqueResult(); // put query-result into cache
|
||||||
A a2 = new A();
|
A a2 = new A();
|
||||||
a2.setName( "xxxxxx" );
|
a2.setName( "xxxxxx" );
|
||||||
s.persist( a2 );
|
s.persist( a2 );
|
||||||
|
@ -91,7 +104,15 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
s.getSessionFactory().getStatistics().clear();
|
s.getSessionFactory().getStatistics().clear();
|
||||||
|
|
||||||
// should not produce a hit in StandardQuery cache region because there is a constraint
|
// should not produce a hit in StandardQuery cache region because there is a constraint
|
||||||
getCriteria( s ).add( Restrictions.isNull( "singleD" ) ).uniqueResult();
|
criteria = criteriaBuilder.createQuery( A.class );
|
||||||
|
root = criteria.from( A.class );
|
||||||
|
criteria.where( criteriaBuilder.and( criteriaBuilder.equal( root.get( "name" ), "name1" ), criteriaBuilder.isNull( root.get( "singleD" ) )) );
|
||||||
|
|
||||||
|
query = session.createQuery( criteria );
|
||||||
|
query.setFlushMode( FlushModeType.COMMIT );
|
||||||
|
query.setCacheable( true );
|
||||||
|
query.uniqueResult();
|
||||||
|
// getCriteria( s ).add( Restrictions.isNull( "singleD" ) ).uniqueResult();
|
||||||
|
|
||||||
Assert.assertEquals( 0, s.getSessionFactory().getStatistics().getQueryCacheHitCount() );
|
Assert.assertEquals( 0, s.getSessionFactory().getStatistics().getQueryCacheHitCount() );
|
||||||
|
|
||||||
|
@ -118,7 +139,9 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
newTx.commit();
|
newTx.commit();
|
||||||
|
|
||||||
newTx = s.beginTransaction();
|
newTx = s.beginTransaction();
|
||||||
getCriteria( s ).setFetchMode( "ds", FetchMode.JOIN ).uniqueResult(); // put query-result into cache
|
|
||||||
|
getQueryFecth( s, "ds", JoinType.LEFT ).uniqueResult();
|
||||||
|
// getQuery( s ).setFetchMode( "ds", FetchMode.JOIN ).uniqueResult(); // put query-result into cache
|
||||||
A a2 = new A();
|
A a2 = new A();
|
||||||
a2.setName( "xxxxxx" );
|
a2.setName( "xxxxxx" );
|
||||||
s.persist( a2 );
|
s.persist( a2 );
|
||||||
|
@ -139,7 +162,8 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
s.getSessionFactory().getStatistics().clear();
|
s.getSessionFactory().getStatistics().clear();
|
||||||
|
|
||||||
// should produce a hit in StandardQuery cache region
|
// should produce a hit in StandardQuery cache region
|
||||||
getCriteria( s ).setFetchMode( "ds", FetchMode.JOIN ).uniqueResult();
|
getQueryFecth( s, "ds", JoinType.LEFT ).uniqueResult();
|
||||||
|
// getCriteria( s ).setFetchMode( "ds", FetchMode.JOIN ).uniqueResult();
|
||||||
|
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
"query is not considered as isImmutableNaturalKeyLookup, despite fullfilling all conditions",
|
"query is not considered as isImmutableNaturalKeyLookup, despite fullfilling all conditions",
|
||||||
|
@ -170,7 +194,8 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
newTx.commit();
|
newTx.commit();
|
||||||
|
|
||||||
newTx = s.beginTransaction();
|
newTx = s.beginTransaction();
|
||||||
getCriteria( s ).setFetchMode( "singleD", FetchMode.JOIN ).uniqueResult(); // put query-result into cache
|
getQueryFecth( s, "singleD" , JoinType.LEFT).uniqueResult();
|
||||||
|
// getCriteria( s ).setFetchMode( "singleD", FetchMode.JOIN ).uniqueResult(); // put query-result into cache
|
||||||
A a2 = new A();
|
A a2 = new A();
|
||||||
a2.setName( "xxxxxx" );
|
a2.setName( "xxxxxx" );
|
||||||
s.persist( a2 );
|
s.persist( a2 );
|
||||||
|
@ -186,7 +211,8 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
s.getSessionFactory().getStatistics().clear();
|
s.getSessionFactory().getStatistics().clear();
|
||||||
|
|
||||||
// should produce a hit in StandardQuery cache region
|
// should produce a hit in StandardQuery cache region
|
||||||
getCriteria( s ).setFetchMode( "singleD", FetchMode.JOIN ).uniqueResult();
|
getQueryFecth( s, "singleD", JoinType.LEFT ).uniqueResult();
|
||||||
|
// getCriteria( s ).setFetchMode( "singleD", FetchMode.JOIN ).uniqueResult();
|
||||||
|
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
"query is not considered as isImmutableNaturalKeyLookup, despite fullfilling all conditions",
|
"query is not considered as isImmutableNaturalKeyLookup, despite fullfilling all conditions",
|
||||||
|
@ -217,8 +243,9 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
newTx.commit();
|
newTx.commit();
|
||||||
|
|
||||||
newTx = s.beginTransaction();
|
newTx = s.beginTransaction();
|
||||||
getCriteria( s ).createAlias( "singleD", "d", JoinType.LEFT_OUTER_JOIN )
|
getQueryFecth( s, "singleD", JoinType.LEFT).uniqueResult();
|
||||||
.uniqueResult(); // put query-result into cache
|
// getCriteria( s ).createAlias( "singleD", "d", JoinType.LEFT_OUTER_JOIN )
|
||||||
|
// .uniqueResult(); // put query-result into cache
|
||||||
A a2 = new A();
|
A a2 = new A();
|
||||||
a2.setName( "xxxxxx" );
|
a2.setName( "xxxxxx" );
|
||||||
s.persist( a2 );
|
s.persist( a2 );
|
||||||
|
@ -235,7 +262,8 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
s.getSessionFactory().getStatistics().clear();
|
s.getSessionFactory().getStatistics().clear();
|
||||||
|
|
||||||
// should not produce a hit in StandardQuery cache region because createAlias() creates a subcriteria
|
// should not produce a hit in StandardQuery cache region because createAlias() creates a subcriteria
|
||||||
getCriteria( s ).createAlias( "singleD", "d", JoinType.LEFT_OUTER_JOIN ).uniqueResult();
|
getQueryFecth( s, "singleD", JoinType.LEFT).uniqueResult();
|
||||||
|
// getCriteria( s ).createAlias( "singleD", "d", JoinType.LEFT_OUTER_JOIN ).uniqueResult();
|
||||||
|
|
||||||
Assert.assertEquals( 0, s.getSessionFactory().getStatistics().getQueryCacheHitCount() );
|
Assert.assertEquals( 0, s.getSessionFactory().getStatistics().getQueryCacheHitCount() );
|
||||||
s.createQuery( "delete from A" ).executeUpdate();
|
s.createQuery( "delete from A" ).executeUpdate();
|
||||||
|
@ -263,8 +291,10 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
newTx.commit();
|
newTx.commit();
|
||||||
|
|
||||||
newTx = s.beginTransaction();
|
newTx = s.beginTransaction();
|
||||||
getCriteria( s ).createCriteria( "singleD", "d", JoinType.LEFT_OUTER_JOIN )
|
getQueryFecth( s, "singleD", JoinType.LEFT).uniqueResult();
|
||||||
.uniqueResult(); // put query-result into cache
|
|
||||||
|
// getCriteria( s ).createCriteria( "singleD", "d", JoinType.LEFT_OUTER_JOIN )
|
||||||
|
// .uniqueResult(); // put query-result into cache
|
||||||
A a2 = new A();
|
A a2 = new A();
|
||||||
a2.setName( "xxxxxx" );
|
a2.setName( "xxxxxx" );
|
||||||
s.persist( a2 );
|
s.persist( a2 );
|
||||||
|
@ -281,7 +311,9 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
s.getSessionFactory().getStatistics().clear();
|
s.getSessionFactory().getStatistics().clear();
|
||||||
|
|
||||||
// should not produce a hit in StandardQuery cache region because createCriteria() creates a subcriteria
|
// should not produce a hit in StandardQuery cache region because createCriteria() creates a subcriteria
|
||||||
getCriteria( s ).createCriteria( "singleD", "d", JoinType.LEFT_OUTER_JOIN ).uniqueResult();
|
getQueryFecth( s, "singleD", JoinType.LEFT).uniqueResult();
|
||||||
|
|
||||||
|
// getCriteria( s ).createCriteria( "singleD", "d", JoinType.LEFT_OUTER_JOIN ).uniqueResult();
|
||||||
|
|
||||||
Assert.assertEquals( 0, s.getSessionFactory().getStatistics().getQueryCacheHitCount() );
|
Assert.assertEquals( 0, s.getSessionFactory().getStatistics().getQueryCacheHitCount() );
|
||||||
s.createQuery( "delete from A" ).executeUpdate();
|
s.createQuery( "delete from A" ).executeUpdate();
|
||||||
|
@ -292,14 +324,40 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
|
||||||
s.close();
|
s.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Criteria getCriteria(Session s) {
|
private Query<A> getQuery(Session s) {
|
||||||
Criteria crit = s.createCriteria( A.class, "anAlias" );
|
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||||
crit.add( Restrictions.naturalId().set( "name", "name1" ) );
|
CriteriaQuery<A> criteria = criteriaBuilder.createQuery( A.class );
|
||||||
crit.setFlushMode( FlushMode.COMMIT );
|
Root<A> root = criteria.from( A.class );
|
||||||
crit.setCacheable( true );
|
criteria.where( criteriaBuilder.equal( root.get( "name" ), "name1" ) );
|
||||||
return crit;
|
|
||||||
|
Query<A> query = session.createQuery( criteria );
|
||||||
|
query.setFlushMode( FlushModeType.COMMIT );
|
||||||
|
query.setCacheable( true );
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Query<A> getQueryFecth(Session s, String fecth, JoinType joinType) {
|
||||||
|
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<A> criteria = criteriaBuilder.createQuery( A.class );
|
||||||
|
Root<A> root = criteria.from( A.class );
|
||||||
|
root.join( fecth, joinType );
|
||||||
|
|
||||||
|
criteria.where( criteriaBuilder.equal( root.get( "name" ), "name1" ) );
|
||||||
|
|
||||||
|
Query<A> query = session.createQuery( criteria );
|
||||||
|
query.setFlushMode( FlushModeType.COMMIT );
|
||||||
|
query.setCacheable( true );
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
// private CriteriaQuery getCriteria(Session s) {
|
||||||
|
// Criteria crit = s.createCriteria( A.class, "anAlias" );
|
||||||
|
// crit.add( Restrictions.naturalId().set( "name", "name1" ) );
|
||||||
|
// crit.setFlushMode( FlushMode.COMMIT );
|
||||||
|
// crit.setCacheable( true );
|
||||||
|
// return crit;
|
||||||
|
// }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class[] getAnnotatedClasses() {
|
protected Class[] getAnnotatedClasses() {
|
||||||
return new Class[] {
|
return new Class[] {
|
||||||
|
|
|
@ -6,86 +6,82 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.annotations.naturalid.cid;
|
package org.hibernate.test.annotations.naturalid.cid;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.criterion.Restrictions;
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Donnchadh O Donnabhain
|
* @author Donnchadh O Donnabhain
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public class EmbeddedAndNaturalIdTest extends BaseCoreFunctionalTestCase {
|
public class EmbeddedAndNaturalIdTest extends BaseCoreFunctionalTestCase {
|
||||||
@TestForIssue(jiraKey = "HHH-9333")
|
@TestForIssue(jiraKey = "HHH-9333")
|
||||||
@Test
|
@Test
|
||||||
public void testSave() {
|
public void testSave() {
|
||||||
// prepare some test data...
|
A account = new A( new AId( 1 ), "testCode" );
|
||||||
Session session = openSession();
|
inTransaction(
|
||||||
session.beginTransaction();
|
session ->
|
||||||
A account = new A(new AId(1), "testCode");
|
session.save( account )
|
||||||
session.save( account );
|
);
|
||||||
session.getTransaction().commit();
|
|
||||||
session.close();
|
|
||||||
|
|
||||||
// clean up
|
|
||||||
session = openSession();
|
|
||||||
session.beginTransaction();
|
|
||||||
session.delete( account );
|
|
||||||
session.getTransaction().commit();
|
|
||||||
session.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestForIssue(jiraKey = "HHH-9333")
|
@TestForIssue(jiraKey = "HHH-9333")
|
||||||
@Test
|
@Test
|
||||||
public void testNaturalIdCriteria() {
|
public void testNaturalIdCriteria() {
|
||||||
Session s = openSession();
|
inTransaction(
|
||||||
s.beginTransaction();
|
s -> {
|
||||||
A u = new A(new AId(1), "testCode" );
|
A u = new A( new AId( 1 ), "testCode" );
|
||||||
s.persist( u );
|
s.persist( u );
|
||||||
s.getTransaction().commit();
|
}
|
||||||
s.close();
|
);
|
||||||
|
|
||||||
s = openSession();
|
inTransaction(
|
||||||
s.beginTransaction();
|
s -> {
|
||||||
u = ( A ) s.createCriteria( A.class )
|
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||||
.add( Restrictions.naturalId().set( "shortCode", "testCode" ) )
|
CriteriaQuery<A> criteria = criteriaBuilder.createQuery( A.class );
|
||||||
.uniqueResult();
|
Root<A> root = criteria.from( A.class );
|
||||||
|
criteria.where( criteriaBuilder.equal( root.get( "shortCode" ), "testCode" ) );
|
||||||
|
A u = s.createQuery( criteria ).uniqueResult();
|
||||||
|
// u = ( A ) s.createCriteria( A.class )
|
||||||
|
// .add( Restrictions.naturalId().set( "shortCode", "testCode" ) )
|
||||||
|
// .uniqueResult();
|
||||||
assertNotNull( u );
|
assertNotNull( u );
|
||||||
s.getTransaction().commit();
|
}
|
||||||
s.close();
|
);
|
||||||
|
|
||||||
s = openSession();
|
|
||||||
s.beginTransaction();
|
|
||||||
s.createQuery( "delete A" ).executeUpdate();
|
|
||||||
s.getTransaction().commit();
|
|
||||||
s.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestForIssue(jiraKey = "HHH-9333")
|
@TestForIssue(jiraKey = "HHH-9333")
|
||||||
@Test
|
@Test
|
||||||
public void testByNaturalId() {
|
public void testByNaturalId() {
|
||||||
Session s = openSession();
|
inTransaction(
|
||||||
s.beginTransaction();
|
s -> {
|
||||||
A u = new A(new AId(1), "testCode" );
|
A u = new A( new AId( 1 ), "testCode" );
|
||||||
s.persist( u );
|
s.persist( u );
|
||||||
s.getTransaction().commit();
|
}
|
||||||
s.close();
|
);
|
||||||
|
|
||||||
s = openSession();
|
inTransaction(
|
||||||
s.beginTransaction();
|
s -> {
|
||||||
u = ( A ) s.byNaturalId(A.class).using("shortCode", "testCode").load();
|
A u = s.byNaturalId( A.class ).using( "shortCode", "testCode" ).load();
|
||||||
assertNotNull( u );
|
assertNotNull( u );
|
||||||
s.getTransaction().commit();
|
}
|
||||||
s.close();
|
);
|
||||||
|
}
|
||||||
|
|
||||||
s = openSession();
|
@After
|
||||||
s.beginTransaction();
|
public void tearDown() {
|
||||||
s.createQuery( "delete A" ).executeUpdate();
|
// clean up
|
||||||
s.getTransaction().commit();
|
inTransaction(
|
||||||
s.close();
|
session ->
|
||||||
|
session.createQuery( "delete A" ).executeUpdate()
|
||||||
|
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -6,9 +6,10 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.annotations.various.readwriteexpression;
|
package org.hibernate.test.annotations.various.readwriteexpression;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
import org.hibernate.Transaction;
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
import org.hibernate.criterion.Restrictions;
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import org.hibernate.dialect.H2Dialect;
|
import org.hibernate.dialect.H2Dialect;
|
||||||
|
|
||||||
import org.hibernate.testing.RequiresDialect;
|
import org.hibernate.testing.RequiresDialect;
|
||||||
|
@ -29,14 +30,13 @@ public class ColumnTransformerTest extends BaseCoreFunctionalTestCase {
|
||||||
* column references in the expression.
|
* column references in the expression.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
@RequiresDialect(H2Dialect.class )
|
@RequiresDialect(H2Dialect.class)
|
||||||
public void testCustomColumnReadAndWrite() throws Exception{
|
public void testCustomColumnReadAndWrite() {
|
||||||
Session s = openSession();
|
inTransaction( s -> {
|
||||||
Transaction t = s.beginTransaction();
|
|
||||||
final double HEIGHT_INCHES = 73;
|
final double HEIGHT_INCHES = 73;
|
||||||
final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d;
|
final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d;
|
||||||
|
|
||||||
Staff staff = new Staff(HEIGHT_INCHES, HEIGHT_INCHES, HEIGHT_INCHES*2, 1);
|
Staff staff = new Staff( HEIGHT_INCHES, HEIGHT_INCHES, HEIGHT_INCHES * 2, 1 );
|
||||||
s.persist( staff );
|
s.persist( staff );
|
||||||
s.flush();
|
s.flush();
|
||||||
|
|
||||||
|
@ -44,47 +44,61 @@ public class ColumnTransformerTest extends BaseCoreFunctionalTestCase {
|
||||||
// Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType;
|
// 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.
|
// Cast returned value to Number then call Number.doubleValue() so it works on all dialects.
|
||||||
double heightViaSql =
|
double heightViaSql =
|
||||||
( (Number)s.createSQLQuery("select size_in_cm from t_staff where t_staff.id=1").uniqueResult() )
|
( (Number) s.createNativeQuery( "select size_in_cm from t_staff where t_staff.id=1" )
|
||||||
|
.uniqueResult() )
|
||||||
.doubleValue();
|
.doubleValue();
|
||||||
assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d);
|
assertEquals( HEIGHT_CENTIMETERS, heightViaSql, 0.01d );
|
||||||
|
|
||||||
heightViaSql =
|
heightViaSql =
|
||||||
( (Number)s.createSQLQuery("select radiusS from t_staff where t_staff.id=1").uniqueResult() )
|
( (Number) s.createNativeQuery( "select radiusS from t_staff where t_staff.id=1" ).uniqueResult() )
|
||||||
.doubleValue();
|
.doubleValue();
|
||||||
assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d);
|
assertEquals( HEIGHT_CENTIMETERS, heightViaSql, 0.01d );
|
||||||
|
|
||||||
heightViaSql =
|
heightViaSql =
|
||||||
( (Number)s.createSQLQuery("select diamet from t_staff where t_staff.id=1").uniqueResult() )
|
( (Number) s.createNativeQuery( "select diamet from t_staff where t_staff.id=1" ).uniqueResult() )
|
||||||
.doubleValue();
|
.doubleValue();
|
||||||
assertEquals(HEIGHT_CENTIMETERS*2, heightViaSql, 0.01d);
|
assertEquals( HEIGHT_CENTIMETERS * 2, heightViaSql, 0.01d );
|
||||||
|
|
||||||
// Test projection
|
// Test projection
|
||||||
Double heightViaHql = (Double)s.createQuery("select s.sizeInInches from Staff s where s.id = 1").uniqueResult();
|
Double heightViaHql = (Double) s.createQuery( "select s.sizeInInches from Staff s where s.id = 1" )
|
||||||
assertEquals(HEIGHT_INCHES, heightViaHql, 0.01d);
|
.uniqueResult();
|
||||||
|
assertEquals( HEIGHT_INCHES, heightViaHql, 0.01d );
|
||||||
|
|
||||||
// Test restriction and entity load via criteria
|
// Test restriction and entity load via criteria
|
||||||
staff = (Staff)s.createCriteria(Staff.class)
|
|
||||||
.add( Restrictions.between("sizeInInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d))
|
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||||
.uniqueResult();
|
CriteriaQuery<Staff> criteria = criteriaBuilder.createQuery( Staff.class );
|
||||||
assertEquals(HEIGHT_INCHES, staff.getSizeInInches(), 0.01d);
|
Root<Staff> root = criteria.from( Staff.class );
|
||||||
|
criteria.where( criteriaBuilder.between(
|
||||||
|
root.get( "sizeInInches" ),
|
||||||
|
HEIGHT_INCHES - 0.01d,
|
||||||
|
HEIGHT_INCHES + 0.01d
|
||||||
|
) );
|
||||||
|
staff = s.createQuery( criteria ).uniqueResult();
|
||||||
|
|
||||||
|
|
||||||
|
// staff = (Staff)s.createCriteria(Staff.class)
|
||||||
|
// .add( Restrictions.between("sizeInInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d))
|
||||||
|
// .uniqueResult();
|
||||||
|
assertEquals( HEIGHT_INCHES, staff.getSizeInInches(), 0.01d );
|
||||||
|
|
||||||
// Test predicate and entity load via HQL
|
// Test predicate and entity load via HQL
|
||||||
staff = (Staff)s.createQuery("from Staff s where s.sizeInInches between ?1 and ?2")
|
staff = (Staff) s.createQuery( "from Staff s where s.sizeInInches between ?1 and ?2" )
|
||||||
.setDouble(1, HEIGHT_INCHES - 0.01d)
|
.setParameter( 1, HEIGHT_INCHES - 0.01d )
|
||||||
.setDouble(2, HEIGHT_INCHES + 0.01d)
|
.setParameter( 2, HEIGHT_INCHES + 0.01d )
|
||||||
.uniqueResult();
|
.uniqueResult();
|
||||||
assertEquals(HEIGHT_INCHES, staff.getSizeInInches(), 0.01d);
|
assertEquals( HEIGHT_INCHES, staff.getSizeInInches(), 0.01d );
|
||||||
|
|
||||||
// Test update
|
// Test update
|
||||||
staff.setSizeInInches(1);
|
staff.setSizeInInches( 1 );
|
||||||
s.flush();
|
s.flush();
|
||||||
heightViaSql =
|
heightViaSql =
|
||||||
( (Number)s.createSQLQuery("select size_in_cm from t_staff where t_staff.id=1").uniqueResult() )
|
( (Number) s.createNativeQuery( "select size_in_cm from t_staff where t_staff.id=1" )
|
||||||
|
.uniqueResult() )
|
||||||
.doubleValue();
|
.doubleValue();
|
||||||
assertEquals(2.54d, heightViaSql, 0.01d);
|
assertEquals( 2.54d, heightViaSql, 0.01d );
|
||||||
s.delete(staff);
|
s.delete( staff );
|
||||||
t.commit();
|
} );
|
||||||
s.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue