Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-6911 - Write DiscriminatorValue to DiscriminatorColumn when combi…
…ned with InheritanceType#JOINED
- Loading branch information
Showing
5 changed files
with
172 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...org/hibernate/test/joinedsubclass/JoinedSubclassWithIgnoredExplicitDiscriminatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
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(); | ||
} | ||
} |