From 4d5c66c80badd214287b1280b262d0623df6c25a Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Tue, 10 Jan 2017 16:22:22 +0100 Subject: [PATCH] HHH-11375 - Add test --- .../SingleTableRelationsTest.java | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/inheritance/discriminator/SingleTableRelationsTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/inheritance/discriminator/SingleTableRelationsTest.java b/hibernate-core/src/test/java/org/hibernate/test/inheritance/discriminator/SingleTableRelationsTest.java new file mode 100644 index 0000000000..b91269698a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/inheritance/discriminator/SingleTableRelationsTest.java @@ -0,0 +1,147 @@ +/* + * 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.inheritance.discriminator; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Assert; +import org.junit.Test; + +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorType; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import java.util.List; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; + +/** + * @author Christian Beikov + */ +public class SingleTableRelationsTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { PostTable.class, Category.class, Post.class }; + } + + private void createTestData() { + doInHibernate( this::sessionFactory, session -> { + Category category7; + session.persist( new Category( 1 ) ); + session.persist( new Category( 2 ) ); + session.persist( new Category( 3 ) ); + session.persist( new Category( 4 ) ); + session.persist( new Category( 5 ) ); + session.persist( new Category( 6 ) ); + session.persist( category7 = new Category( 7 ) ); + session.persist( new Post( 8, category7 ) ); + } ); + } + + @Test + @TestForIssue(jiraKey = "HHH-11375") + public void testLazyInitialization() { + createTestData(); + doInHibernate( this::sessionFactory, session -> { + Category category7 = session.find( Category.class, 7 ); + // Must be empty because although Post and Category share the same column for their category relations, + // the children must be based on entities that are of type Category + Assert.assertTrue( category7.children.isEmpty() ); + } ); + } + + @Override + protected boolean isCleanupTestDataRequired() { + return true; + } + + @Test + @TestForIssue(jiraKey = "HHH-11375") + public void testJoinFetch() { + createTestData(); + doInHibernate( this::sessionFactory, session -> { + Category category7 = session.createQuery( "SELECT c FROM " + Category.class.getName() + " c LEFT JOIN FETCH c.children WHERE c.id = :id", Category.class ) + .setParameter( "id", 7 ) + .getSingleResult(); + // Must be empty because although Post and Category share the same column for their category relations, + // the children must be based on entities that are of type Category + Assert.assertTrue( category7.children.isEmpty() ); + } ); + } + + @Entity + @Table(name = "cp_post") + @Inheritance(strategy = InheritanceType.SINGLE_TABLE) + @DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER) + public static class PostTable { + + @Id + protected Integer id; + + public PostTable() { + } + + public PostTable(Integer id) { + this.id = id; + } + } + + @Entity + @DiscriminatorValue("1") + public static class Category extends PostTable { + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn + protected Category category; + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "category") + protected List children; + + public Category() { + } + + public Category(Integer id) { + super(id); + } + + public Category(Integer id, Category category) { + super(id); + this.category = category; + } + } + + @Entity + @DiscriminatorValue("2") + public static class Post extends PostTable { + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn + protected Category category; + + public Post() { + } + + public Post(Integer id) { + super(id); + } + + public Post(Integer id, Category category) { + super(id); + this.category = category; + } + } + +}