diff --git a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithEmbeddableTest.java similarity index 73% rename from hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithEmbeddableTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithEmbeddableTest.java index 35d012b997..206afdaf06 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithEmbeddableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithEmbeddableTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.joinedsubclass; +package org.hibernate.orm.test.joinedsubclass; import javax.persistence.Embeddable; import javax.persistence.Entity; @@ -13,27 +13,31 @@ import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertThat; /** * @author Andrea Boriero */ -public class JoinedSubclassWithEmbeddableTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + JoinedSubclassWithEmbeddableTest.BaseEntity.class, + JoinedSubclassWithEmbeddableTest.ConcreteEntity.class + } +) +@SessionFactory +public class JoinedSubclassWithEmbeddableTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] {BaseEntity.class, ConcreteEntity.class}; - } - - @Before - public void setUp() { - doInHibernate( this::sessionFactory, session -> { + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { ConcreteEntity entity = new ConcreteEntity(); entity.setId( 1L ); entity.setField( "field_base" ); @@ -47,14 +51,15 @@ public class JoinedSubclassWithEmbeddableTest extends BaseCoreFunctionalTestCase @Test @TestForIssue(jiraKey = "HHH-10920") - public void testEmbeddedFieldIsNotNull() { - doInHibernate( this::sessionFactory, session -> { + public void testEmbeddedFieldIsNotNull(SessionFactoryScope scope) { + scope.inTransaction( session -> { final ConcreteEntity entity = session.get( ConcreteEntity.class, 1L ); assertThat( entity.getEmbedded().getField(), is( "field_embedded" ) ); assertThat( entity.getField(), is( "field_base" ) ); entity.getEmbedded().setField( "field_subclass" ); } ); - doInHibernate( this::sessionFactory, session -> { + + scope.inTransaction( session -> { final ConcreteEntity entity = session.get( ConcreteEntity.class, 1L ); assertThat( entity.getEmbedded().getField(), is( "field_subclass" ) ); assertThat( entity.getField(), is( "field_base" ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithExplicitDiscriminatorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithExplicitDiscriminatorTest.java new file mode 100644 index 0000000000..c013dfeb0e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithExplicitDiscriminatorTest.java @@ -0,0 +1,138 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.joinedsubclass; + +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorType; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.persister.entity.JoinedSubclassEntityPersister; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + + +/** + * @author Steve Ebersole + */ +@TestForIssue(jiraKey = "HHH-6911") +@DomainModel( + annotatedClasses = { + JoinedSubclassWithExplicitDiscriminatorTest.Animal.class, + JoinedSubclassWithExplicitDiscriminatorTest.Cat.class, + JoinedSubclassWithExplicitDiscriminatorTest.Dog.class + } +) +@SessionFactory +public class JoinedSubclassWithExplicitDiscriminatorTest { + + @Test + public void metadataAssertions(SessionFactoryScope scope) { + EntityPersister p = scope.getSessionFactory().getEntityPersister( Dog.class.getName() ); + assertNotNull( p ); + final JoinedSubclassEntityPersister dogPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); + assertEquals( "string", dogPersister.getDiscriminatorType().getName() ); + assertEquals( "type", dogPersister.getDiscriminatorColumnName() ); + assertEquals( "dog", dogPersister.getDiscriminatorValue() ); + + p = scope.getSessionFactory().getEntityPersister( Cat.class.getName() ); + assertNotNull( p ); + final JoinedSubclassEntityPersister catPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); + assertEquals( "string", catPersister.getDiscriminatorType().getName() ); + assertEquals( "type", catPersister.getDiscriminatorColumnName() ); + assertEquals( "cat", catPersister.getDiscriminatorValue() ); + } + + @Test + public void basicUsageTest(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.save( new Cat( 1 ) ); + session.save( new Dog( 2 ) ); + } + ); + + scope.inTransaction( + session -> { + session.createQuery( "from Animal" ).list(); + Cat cat = session.get( Cat.class, 1 ); + assertNotNull( cat ); + session.delete( cat ); + Dog dog = session.get( Dog.class, 2 ); + assertNotNull( dog ); + session.delete( dog ); + } + ); + } + + public T assertTyping(Class expectedType, Object value) { + if ( ! expectedType.isInstance( value ) ) { + fail( + String.format( + "Expecting value of type [%s], but found [%s]", + expectedType.getName(), + value == null ? "" : value + ) + ); + } + return (T) value; + } + + @Entity(name = "Animal") + @Table(name = "animal") + @Inheritance(strategy = InheritanceType.JOINED) + @DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING) + @DiscriminatorValue(value = "???animal???") + public static abstract class Animal { + @Id + public Integer id; + + protected Animal() { + } + + protected Animal(Integer id) { + this.id = id; + } + } + + @Entity(name = "Cat") + @DiscriminatorValue(value = "cat") + public static class Cat extends Animal { + public Cat() { + super(); + } + + public Cat(Integer id) { + super( id ); + } + } + + @Entity(name = "Dog") + @DiscriminatorValue(value = "dog") + public static class Dog extends Animal { + public Dog() { + super(); + } + + public Dog(Integer id) { + super( id ); + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithIgnoredExplicitDiscriminatorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithIgnoredExplicitDiscriminatorTest.java new file mode 100644 index 0000000000..aaddc50b88 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithIgnoredExplicitDiscriminatorTest.java @@ -0,0 +1,146 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.joinedsubclass; + +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorType; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.persister.entity.JoinedSubclassEntityPersister; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author Steve Ebersole + */ +@TestForIssue(jiraKey = "HHH-6911") +@DomainModel( + annotatedClasses = { + JoinedSubclassWithIgnoredExplicitDiscriminatorTest.Animal.class, + JoinedSubclassWithIgnoredExplicitDiscriminatorTest.Cat.class, + JoinedSubclassWithIgnoredExplicitDiscriminatorTest.Dog.class + } +) +@SessionFactory +@ServiceRegistry( + settings = + @ServiceRegistry.Setting( + name = AvailableSettings.IGNORE_EXPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS, + value = "true")) +public class JoinedSubclassWithIgnoredExplicitDiscriminatorTest { + + + @Test + public void metadataAssertions(SessionFactoryScope scope) { + EntityPersister p = scope.getSessionFactory().getEntityPersister( Dog.class.getName() ); + assertNotNull( p ); + final JoinedSubclassEntityPersister dogPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); + assertEquals( "integer", dogPersister.getDiscriminatorType().getName() ); + assertEquals( "clazz_", dogPersister.getDiscriminatorColumnName() ); + assertTrue( Integer.class.isInstance( dogPersister.getDiscriminatorValue() ) ); + + p = scope.getSessionFactory().getEntityPersister( Cat.class.getName() ); + assertNotNull( p ); + final JoinedSubclassEntityPersister catPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); + assertEquals( "integer", catPersister.getDiscriminatorType().getName() ); + assertEquals( "clazz_", catPersister.getDiscriminatorColumnName() ); + assertTrue( Integer.class.isInstance( catPersister.getDiscriminatorValue() ) ); + } + + @Test + public void basicUsageTest(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.save( new Cat( 1 ) ); + session.save( new Dog( 2 ) ); + } + ); + + scope.inTransaction( + session -> { + session.createQuery( "from Animal" ).list(); + Cat cat = session.get( Cat.class, 1 ); + assertNotNull( cat ); + session.delete( cat ); + Dog dog = session.get( Dog.class, 2 ); + assertNotNull( dog ); + session.delete( dog ); + } + ); + } + + public T assertTyping(Class expectedType, Object value) { + if ( !expectedType.isInstance( value ) ) { + fail( + String.format( + "Expecting value of type [%s], but found [%s]", + expectedType.getName(), + value == null ? "" : value + ) + ); + } + return (T) value; + } + + @Entity(name = "Animal") + @Table(name = "animal") + @Inheritance(strategy = InheritanceType.JOINED) + @DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING) + @DiscriminatorValue(value = "???animal???") + public static abstract class Animal { + @Id + public Integer id; + + protected Animal() { + } + + protected Animal(Integer id) { + this.id = id; + } + } + + @Entity(name = "Cat") + @DiscriminatorValue(value = "cat") + public static class Cat extends Animal { + public Cat() { + super(); + } + + public Cat(Integer id) { + super( id ); + } + } + + @Entity(name = "Dog") + @DiscriminatorValue(value = "dog") + public static class Dog extends Animal { + public Dog() { + super(); + } + + public Dog(Integer id) { + super( id ); + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithImplicitDiscriminatorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithImplicitDiscriminatorTest.java new file mode 100644 index 0000000000..77d85a6f0b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/joinedsubclass/JoinedSubclassWithImplicitDiscriminatorTest.java @@ -0,0 +1,150 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.joinedsubclass; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Ejb3DiscriminatorColumn; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.persister.entity.JoinedSubclassEntityPersister; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author Steve Ebersole + */ +@TestForIssue(jiraKey = "HHH-6911") +@DomainModel( + annotatedClasses = { + JoinedSubclassWithImplicitDiscriminatorTest.Animal.class, + JoinedSubclassWithImplicitDiscriminatorTest.Cat.class, + JoinedSubclassWithImplicitDiscriminatorTest.Dog.class + } +) +@SessionFactory +@ServiceRegistry( + settings = + @ServiceRegistry.Setting( + name = AvailableSettings.IMPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS, + value = "true")) +public class JoinedSubclassWithImplicitDiscriminatorTest { + + @Test + public void metadataAssertions(SessionFactoryScope scope) { + EntityPersister p = scope.getSessionFactory().getEntityPersister( Dog.class.getName() ); + assertNotNull( p ); + final JoinedSubclassEntityPersister dogPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); + assertEquals( + Ejb3DiscriminatorColumn.DEFAULT_DISCRIMINATOR_TYPE, + dogPersister.getDiscriminatorType().getName() + ); + assertEquals( + Ejb3DiscriminatorColumn.DEFAULT_DISCRIMINATOR_COLUMN_NAME, + dogPersister.getDiscriminatorColumnName() + ); + assertEquals( "Dog", dogPersister.getDiscriminatorValue() ); + + p = scope.getSessionFactory().getEntityPersister( Cat.class.getName() ); + assertNotNull( p ); + final JoinedSubclassEntityPersister catPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); + assertEquals( + Ejb3DiscriminatorColumn.DEFAULT_DISCRIMINATOR_TYPE, + catPersister.getDiscriminatorType().getName() + ); + assertEquals( + Ejb3DiscriminatorColumn.DEFAULT_DISCRIMINATOR_COLUMN_NAME, + catPersister.getDiscriminatorColumnName() + ); + assertEquals( "Cat", catPersister.getDiscriminatorValue() ); + } + + @Test + public void basicUsageTest(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.save( new Cat( 1 ) ); + session.save( new Dog( 2 ) ); + } + ); + + scope.inTransaction( + session -> { + session.createQuery( "from Animal" ).list(); + Cat cat = (Cat) session.get( Cat.class, 1 ); + assertNotNull( cat ); + session.delete( cat ); + Dog dog = (Dog) session.get( Dog.class, 2 ); + assertNotNull( dog ); + session.delete( dog ); + } + ); + } + + public T assertTyping(Class expectedType, Object value) { + if ( !expectedType.isInstance( value ) ) { + fail( + String.format( + "Expecting value of type [%s], but found [%s]", + expectedType.getName(), + value == null ? "" : value + ) + ); + } + return (T) value; + } + + @Entity(name = "Animal") + @Table(name = "animal") + @Inheritance(strategy = InheritanceType.JOINED) + public static abstract class Animal { + @Id + public Integer id; + + protected Animal() { + } + + protected Animal(Integer id) { + this.id = id; + } + } + + @Entity(name = "Cat") + public static class Cat extends Animal { + public Cat() { + super(); + } + + public Cat(Integer id) { + super( id ); + } + } + + @Entity(name = "Dog") + public static class Dog extends Animal { + public Dog() { + super(); + } + + public Dog(Integer id) { + super( id ); + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithExplicitDiscriminatorTest.java b/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithExplicitDiscriminatorTest.java deleted file mode 100644 index cadb218189..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithExplicitDiscriminatorTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.joinedsubclass; - -import javax.persistence.DiscriminatorColumn; -import javax.persistence.DiscriminatorType; -import javax.persistence.DiscriminatorValue; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.Table; - -import org.hibernate.Session; -import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.persister.entity.JoinedSubclassEntityPersister; -import org.hibernate.persister.entity.Loadable; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.hibernate.testing.junit4.ExtraAssertions; -import org.junit.Test; - -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -/** - * @author Steve Ebersole - */ -@TestForIssue( jiraKey = "HHH-6911" ) -public class JoinedSubclassWithExplicitDiscriminatorTest extends BaseCoreFunctionalTestCase { - @Entity( name = "Animal" ) - @Table( name = "animal" ) - @Inheritance( strategy = InheritanceType.JOINED ) - @DiscriminatorColumn( name = "type", discriminatorType = DiscriminatorType.STRING ) - @DiscriminatorValue( value = "???animal???" ) - public static abstract class Animal { - @Id - public Integer id; - - protected Animal() { - } - - protected Animal(Integer id) { - this.id = id; - } - } - - @Entity( name = "Cat" ) - @DiscriminatorValue( value = "cat" ) - public static class Cat extends Animal { - public Cat() { - super(); - } - - public Cat(Integer id) { - super( id ); - } - } - - @Entity( name = "Dog" ) - @DiscriminatorValue( value = "dog" ) - public static class Dog extends Animal { - public Dog() { - super(); - } - - public Dog(Integer id) { - super( id ); - } - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Animal.class, Cat.class, Dog.class }; - } - - @Test - public void metadataAssertions() { - EntityPersister p = sessionFactory().getEntityPersister( Dog.class.getName() ); - assertNotNull( p ); - final JoinedSubclassEntityPersister dogPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); - assertEquals( "string", dogPersister.getDiscriminatorType().getName() ); - assertEquals( "type", dogPersister.getDiscriminatorColumnName() ); - assertEquals( "dog", dogPersister.getDiscriminatorValue() ); - - p = sessionFactory().getEntityPersister( Cat.class.getName() ); - assertNotNull( p ); - final JoinedSubclassEntityPersister catPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); - assertEquals( "string", catPersister.getDiscriminatorType().getName() ); - assertEquals( "type", catPersister.getDiscriminatorColumnName() ); - assertEquals( "cat", catPersister.getDiscriminatorValue() ); - } - - @Test - public void basicUsageTest() { - Session session = openSession(); - session.beginTransaction(); - session.save( new Cat( 1 ) ); - session.save( new Dog( 2 ) ); - session.getTransaction().commit(); - session.close(); - - session = openSession(); - session.beginTransaction(); - session.createQuery( "from Animal" ).list(); - Cat cat = (Cat) session.get( Cat.class, 1 ); - assertNotNull( cat ); - session.delete( cat ); - Dog dog = (Dog) session.get( Dog.class, 2 ); - assertNotNull( dog ); - session.delete( dog ); - session.getTransaction().commit(); - session.close(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithIgnoredExplicitDiscriminatorTest.java b/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithIgnoredExplicitDiscriminatorTest.java deleted file mode 100644 index a97206d5c4..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithIgnoredExplicitDiscriminatorTest.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.joinedsubclass; - -import javax.persistence.DiscriminatorColumn; -import javax.persistence.DiscriminatorType; -import javax.persistence.DiscriminatorValue; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.Table; - -import org.hibernate.Session; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.persister.entity.JoinedSubclassEntityPersister; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * @author Steve Ebersole - */ -@TestForIssue( jiraKey = "HHH-6911" ) -public class JoinedSubclassWithIgnoredExplicitDiscriminatorTest extends BaseCoreFunctionalTestCase { - @Entity( name = "Animal" ) - @Table( name = "animal" ) - @Inheritance( strategy = InheritanceType.JOINED ) - @DiscriminatorColumn( name = "type", discriminatorType = DiscriminatorType.STRING ) - @DiscriminatorValue( value = "???animal???" ) - public static abstract class Animal { - @Id - public Integer id; - - protected Animal() { - } - - protected Animal(Integer id) { - this.id = id; - } - } - - @Entity( name = "Cat" ) - @DiscriminatorValue( value = "cat" ) - public static class Cat extends Animal { - public Cat() { - super(); - } - - public Cat(Integer id) { - super( id ); - } - } - - @Entity( name = "Dog" ) - @DiscriminatorValue( value = "dog" ) - public static class Dog extends Animal { - public Dog() { - super(); - } - - public Dog(Integer id) { - super( id ); - } - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Animal.class, Cat.class, Dog.class }; - } - - @Override - protected void configure(Configuration configuration) { - super.configure( configuration ); - configuration.setProperty( AvailableSettings.IGNORE_EXPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS, "true" ); - } - - @Test - public void metadataAssertions() { - EntityPersister p = sessionFactory().getEntityPersister( Dog.class.getName() ); - assertNotNull( p ); - final JoinedSubclassEntityPersister dogPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); - assertEquals( "integer", dogPersister.getDiscriminatorType().getName() ); - assertEquals( "clazz_", dogPersister.getDiscriminatorColumnName() ); - assertTrue( Integer.class.isInstance( dogPersister.getDiscriminatorValue() ) ); - - p = sessionFactory().getEntityPersister( Cat.class.getName() ); - assertNotNull( p ); - final JoinedSubclassEntityPersister catPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); - assertEquals( "integer", catPersister.getDiscriminatorType().getName() ); - assertEquals( "clazz_", catPersister.getDiscriminatorColumnName() ); - assertTrue( Integer.class.isInstance( catPersister.getDiscriminatorValue() ) ); - } - - @Test - public void basicUsageTest() { - Session session = openSession(); - session.beginTransaction(); - session.save( new Cat( 1 ) ); - session.save( new Dog( 2 ) ); - session.getTransaction().commit(); - session.close(); - - session = openSession(); - session.beginTransaction(); - session.createQuery( "from Animal" ).list(); - Cat cat = (Cat) session.get( Cat.class, 1 ); - assertNotNull( cat ); - session.delete( cat ); - Dog dog = (Dog) session.get( Dog.class, 2 ); - assertNotNull( dog ); - session.delete( dog ); - session.getTransaction().commit(); - session.close(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithImplicitDiscriminatorTest.java b/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithImplicitDiscriminatorTest.java deleted file mode 100644 index 56f58c9b2a..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassWithImplicitDiscriminatorTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.joinedsubclass; - -import javax.persistence.DiscriminatorColumn; -import javax.persistence.DiscriminatorType; -import javax.persistence.DiscriminatorValue; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.Table; - -import org.hibernate.Session; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Ejb3DiscriminatorColumn; -import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.persister.entity.JoinedSubclassEntityPersister; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -/** - * @author Steve Ebersole - */ -@TestForIssue( jiraKey = "HHH-6911" ) -public class JoinedSubclassWithImplicitDiscriminatorTest extends BaseCoreFunctionalTestCase { - @Entity( name = "Animal" ) - @Table( name = "animal" ) - @Inheritance( strategy = InheritanceType.JOINED ) - public static abstract class Animal { - @Id - public Integer id; - - protected Animal() { - } - - protected Animal(Integer id) { - this.id = id; - } - } - - @Entity( name = "Cat" ) - public static class Cat extends Animal { - public Cat() { - super(); - } - - public Cat(Integer id) { - super( id ); - } - } - - @Entity( name = "Dog" ) - public static class Dog extends Animal { - public Dog() { - super(); - } - - public Dog(Integer id) { - super( id ); - } - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Animal.class, Cat.class, Dog.class }; - } - - @Override - protected void configure(Configuration configuration) { - super.configure( configuration ); - configuration.setProperty( AvailableSettings.IMPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS, "true" ); - } - - @Test - public void metadataAssertions() { - EntityPersister p = sessionFactory().getEntityPersister( Dog.class.getName() ); - assertNotNull( p ); - final JoinedSubclassEntityPersister dogPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); - assertEquals( Ejb3DiscriminatorColumn.DEFAULT_DISCRIMINATOR_TYPE, dogPersister.getDiscriminatorType().getName() ); - assertEquals( Ejb3DiscriminatorColumn.DEFAULT_DISCRIMINATOR_COLUMN_NAME, dogPersister.getDiscriminatorColumnName() ); - assertEquals( "Dog", dogPersister.getDiscriminatorValue() ); - - p = sessionFactory().getEntityPersister( Cat.class.getName() ); - assertNotNull( p ); - final JoinedSubclassEntityPersister catPersister = assertTyping( JoinedSubclassEntityPersister.class, p ); - assertEquals( Ejb3DiscriminatorColumn.DEFAULT_DISCRIMINATOR_TYPE, catPersister.getDiscriminatorType().getName() ); - assertEquals( Ejb3DiscriminatorColumn.DEFAULT_DISCRIMINATOR_COLUMN_NAME, catPersister.getDiscriminatorColumnName() ); - assertEquals( "Cat", catPersister.getDiscriminatorValue() ); - } - - @Test - public void basicUsageTest() { - Session session = openSession(); - session.beginTransaction(); - session.save( new Cat( 1 ) ); - session.save( new Dog( 2 ) ); - session.getTransaction().commit(); - session.close(); - - session = openSession(); - session.beginTransaction(); - session.createQuery( "from Animal" ).list(); - Cat cat = (Cat) session.get( Cat.class, 1 ); - assertNotNull( cat ); - session.delete( cat ); - Dog dog = (Dog) session.get( Dog.class, 2 ); - assertNotNull( dog ); - session.delete( dog ); - session.getTransaction().commit(); - session.close(); - } -}