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