Re-enabled OneToOneFormulaTest

This commit is contained in:
Andrea Boriero 2021-05-07 09:57:56 +02:00
parent e122ac33dc
commit 05ff370488
4 changed files with 82 additions and 74 deletions

View File

@ -13,60 +13,77 @@ import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.Oracle8iDialect; import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.orm.test.immutable.ImmutableTest;
import org.hibernate.type.AbstractSingleColumnStandardBasicType; import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.TextType; import org.hibernate.type.TextType;
import org.hibernate.type.descriptor.jdbc.ClobTypeDescriptor; import org.hibernate.type.descriptor.jdbc.ClobTypeDescriptor;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.junit.Before; import org.hibernate.testing.orm.junit.DialectContext;
import org.junit.Test; import org.hibernate.testing.orm.junit.DomainModel;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.notNullValue; import static org.hamcrest.core.IsNull.notNullValue;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.Assert.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/** /**
* @author Gavin King * @author Gavin King
*/ */
public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase { public class OneToOneFormulaTest extends BaseSessionFactoryFunctionalTest {
private static class TextAsMaterializedClobType extends AbstractSingleColumnStandardBasicType<String> { private static class TextAsMaterializedClobType extends AbstractSingleColumnStandardBasicType<String> {
public final static TextAsMaterializedClobType INSTANCE = new TextAsMaterializedClobType(); public final static TextAsMaterializedClobType INSTANCE = new TextAsMaterializedClobType();
public TextAsMaterializedClobType() { public TextAsMaterializedClobType() {
super( ClobTypeDescriptor.DEFAULT, TextType.INSTANCE.getJavaTypeDescriptor() ); super( ClobTypeDescriptor.DEFAULT, TextType.INSTANCE.getJavaTypeDescriptor() );
} }
public String getName() { public String getName() {
return TextType.INSTANCE.getName(); return TextType.INSTANCE.getName();
} }
} }
public String[] getMappings() { @Override
return new String[] { "onetoone/formula/Person.hbm.xml" }; protected void applySettings(StandardServiceRegistryBuilder builder) {
builder.applySetting( Environment.USE_SECOND_LEVEL_CACHE, "false" );
builder.applySetting( Environment.GENERATE_STATISTICS, "true" );
builder.applySetting( Environment.DEFAULT_BATCH_FETCH_SIZE, "2" );
} }
public void configure(Configuration cfg) { @Override
if ( Oracle8iDialect.class.isInstance( getDialect() ) ) { protected void applyMetadataBuilder(MetadataBuilder metadataBuilder) {
cfg.registerTypeOverride( TextAsMaterializedClobType.INSTANCE ); if ( OracleDialect.class.isInstance( getDialect() ) ) {
metadataBuilder.applyBasicType( TextAsMaterializedClobType.INSTANCE );
} }
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
cfg.setProperty(Environment.DEFAULT_BATCH_FETCH_SIZE, "2");
} }
@Override
public String[] getOrmXmlFiles() {
return new String[] { "org/hibernate/test/onetoone/formula/Person.hbm.xml" };
}
private Person person; private Person person;
private Address address; private Address address;
@Before @BeforeEach
public void setUp(){ public void setUp() {
person = new Person(); person = new Person();
person.setName( "Gavin King" ); person.setName( "Gavin King" );
address = new Address(); address = new Address();
@ -77,14 +94,15 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
address.setStreet( "Karbarook Ave" ); address.setStreet( "Karbarook Ave" );
person.setAddress( address ); person.setAddress( address );
doInHibernate( this::sessionFactory, session -> { inTransaction(
session.persist( person ); session ->
} ); session.persist( person )
);
} }
@Override @AfterEach
protected void cleanupTest() { protected void cleanupTest() {
doInHibernate( this::sessionFactory, session -> { inTransaction( session -> {
session.delete( person ); session.delete( person );
} ); } );
} }
@ -92,16 +110,16 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testOneToOneFormula() { public void testOneToOneFormula() {
doInHibernate( this::sessionFactory, s -> { inTransaction( session -> {
Person p = (Person) s.createQuery( "from Person" ).uniqueResult(); Person p = (Person) session.createQuery( "from Person" ).uniqueResult();
assertNotNull( p.getAddress() ); assertNotNull( p.getAddress() );
assertTrue( Hibernate.isInitialized( p.getAddress() ) ); assertTrue( Hibernate.isInitialized( p.getAddress() ) );
assertNull( p.getMailingAddress() ); assertNull( p.getMailingAddress() );
} ); } );
doInHibernate( this::sessionFactory, s -> { inTransaction( session -> {
Person p = (Person) s.createQuery( Person p = (Person) session.createQuery(
"from Person p left join fetch p.mailingAddress left join fetch p.address" ).uniqueResult(); "from Person p left join fetch p.mailingAddress left join fetch p.address" ).uniqueResult();
assertNotNull( p.getAddress() ); assertNotNull( p.getAddress() );
@ -109,53 +127,43 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
assertNull( p.getMailingAddress() ); assertNull( p.getMailingAddress() );
} ); } );
doInHibernate( this::sessionFactory, s -> { inTransaction( session -> {
Person p = (Person) s.createQuery( "from Person p left join fetch p.address" ).uniqueResult(); Person p = (Person) session.createQuery( "from Person p left join fetch p.address" ).uniqueResult();
assertNotNull( p.getAddress() ); assertNotNull( p.getAddress() );
assertTrue( Hibernate.isInitialized( p.getAddress() ) ); assertTrue( Hibernate.isInitialized( p.getAddress() ) );
assertNull( p.getMailingAddress() ); assertNull( p.getMailingAddress() );
} ); } );
doInHibernate( this::sessionFactory, s -> { inTransaction( session -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class ); CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class ); Root<Person> root = criteria.from( Person.class );
Join<Object, Object> address = root.join( "address", JoinType.INNER ); Join<Object, Object> address = root.join( "address", JoinType.INNER );
criteria.where( criteriaBuilder.equal( address.get( "zip" ), "3181" ) ); criteria.where( criteriaBuilder.equal( address.get( "zip" ), "3181" ) );
Person p = s.createQuery( criteria ).uniqueResult(); Person p = session.createQuery( criteria ).uniqueResult();
// Person p = (Person) s.createCriteria( Person.class )
// .createCriteria( "address" )
// .add( Property.forName( "zip" ).eq( "3181" ) )
// .uniqueResult();
assertNotNull( p ); assertNotNull( p );
} ); } );
doInHibernate( this::sessionFactory, s -> { inTransaction( session -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class ); CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class ); Root<Person> root = criteria.from( Person.class );
root.fetch( "address", JoinType.LEFT ); root.fetch( "address", JoinType.LEFT );
Person p = s.createQuery( criteria ).uniqueResult(); Person p = session.createQuery( criteria ).uniqueResult();
// Person p = (Person) s.createCriteria( Person.class )
// .setFetchMode( "address", FetchMode.JOIN )
// .uniqueResult();
assertNotNull( p.getAddress() ); assertNotNull( p.getAddress() );
assertTrue( Hibernate.isInitialized( p.getAddress() ) ); assertTrue( Hibernate.isInitialized( p.getAddress() ) );
assertNull( p.getMailingAddress() ); assertNull( p.getMailingAddress() );
} ); } );
doInHibernate( this::sessionFactory, s -> { inTransaction( session -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class ); CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class ); Root<Person> root = criteria.from( Person.class );
root.fetch( "address", JoinType.LEFT ); root.fetch( "address", JoinType.LEFT );
Person p = s.createQuery( criteria ).uniqueResult(); Person p = session.createQuery( criteria ).uniqueResult();
// Person p = (Person) s.createCriteria( Person.class )
// .setFetchMode( "mailingAddress", FetchMode.JOIN )
// .uniqueResult();
assertNotNull( p.getAddress() ); assertNotNull( p.getAddress() );
assertTrue( Hibernate.isInitialized( p.getAddress() ) ); assertTrue( Hibernate.isInitialized( p.getAddress() ) );
@ -168,17 +176,19 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue(jiraKey = "HHH-5757") @TestForIssue(jiraKey = "HHH-5757")
public void testQuery() { public void testQuery() {
doInHibernate( this::sessionFactory, session -> { inTransaction( session -> {
Person p = (Person) session.createQuery( "from Person p where p.address = :address" ).setParameter( Person p = (Person) session.createQuery( "from Person p where p.address = :address" ).setParameter(
"address", "address",
address ).uniqueResult(); address
).uniqueResult();
assertThat( p, notNullValue() ); assertThat( p, notNullValue() );
} ); } );
doInHibernate( this::sessionFactory, session -> { inTransaction( session -> {
Address a = (Address) session.createQuery( "from Address a where a.person = :person" ).setParameter( Address a = (Address) session.createQuery( "from Address a where a.person = :person" ).setParameter(
"person", "person",
person ).uniqueResult(); person
).uniqueResult();
assertThat( a, notNullValue() ); assertThat( a, notNullValue() );
} ); } );
@ -186,32 +196,30 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testOneToOneEmbeddedCompositeKey() { public void testOneToOneEmbeddedCompositeKey() {
doInHibernate( this::sessionFactory, session -> { inTransaction( session -> {
Address a = new Address(); Address a = new Address();
a.setType("HOME"); a.setType( "HOME" );
a.setPerson(person); a.setPerson( person );
a = session.load(Address.class, a); a = session.load( Address.class, a );
assertFalse( Hibernate.isInitialized(a) ); assertFalse( Hibernate.isInitialized( a ) );
a.getPerson(); a.getPerson();
a.getType(); a.getType();
assertFalse( Hibernate.isInitialized(a) ); assertFalse( Hibernate.isInitialized( a ) );
assertEquals(a.getZip(), "3181"); assertEquals( "3181", a.getZip() );
} ); } );
doInHibernate( this::sessionFactory, session -> { inTransaction( session -> {
Address a = new Address(); Address a = new Address();
a.setType("HOME"); a.setType( "HOME" );
a.setPerson(person); a.setPerson( person );
Address a2 = session.get(Address.class, a); Address a2 = session.get( Address.class, a );
assertTrue( Hibernate.isInitialized(a) ); assertTrue( Hibernate.isInitialized( a ) );
assertSame(a2, a); assertSame( a, a2 );
assertSame(a2.getPerson(), person); //this is a little bit desirable assertSame( person, a2.getPerson() ); //this is a little bit desirable
assertEquals(a.getZip(), "3181"); assertEquals( "3181", a.getZip() );
} ); } );
// s.delete(a2); // s.delete(a2);
// s.delete( s.get( Person.class, p.getName() ) ); //this is certainly undesirable! oh well... // s.delete( s.get( Person.class, p.getName() ) ); //this is certainly undesirable! oh well...
// //