diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/SingleTableEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/SingleTableEntityPersister.java index 33d93f7607..d05021f6a0 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/SingleTableEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/SingleTableEntityPersister.java @@ -626,7 +626,7 @@ public class SingleTableEntityPersister extends AbstractEntityPersister { frag.addValues( fullDiscriminatorValues() ); } - return " and " + frag.toFragmentString(); + return frag.toFragmentString(); } private boolean needsDiscriminator() { diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstWalker.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstWalker.java index 600afed570..7f9cb599fe 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstWalker.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstWalker.java @@ -197,12 +197,13 @@ public abstract class AbstractSqlAstWalker visitFromClause( fromClause ); } - if ( querySpec.getWhereClauseRestrictions() != null && !querySpec.getWhereClauseRestrictions().isEmpty() ) { + final Predicate whereClauseRestrictions = querySpec.getWhereClauseRestrictions(); + if ( whereClauseRestrictions != null && !whereClauseRestrictions.isEmpty() ) { appendSql( " where " ); clauseStack.push( Clause.WHERE ); try { - querySpec.getWhereClauseRestrictions().accept( this ); + whereClauseRestrictions.accept( this ); } finally { clauseStack.pop(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/SingleTableRelationsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/SingleTableRelationsTest.java new file mode 100644 index 0000000000..521be1083d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/SingleTableRelationsTest.java @@ -0,0 +1,167 @@ +/* + * 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.inheritance.discriminator; + +import java.util.List; +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 org.hibernate.cfg.AvailableSettings; + +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.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Christian Beikov + */ +@DomainModel( + annotatedClasses = { + SingleTableRelationsTest.PostTable.class, + SingleTableRelationsTest.Category.class, + SingleTableRelationsTest.Post.class + } +) +@SessionFactory +@ServiceRegistry(settings = @ServiceRegistry.Setting(name = AvailableSettings.FORCE_DISCRIMINATOR_IN_SELECTS_BY_DEFAULT, value = "true")) +public class SingleTableRelationsTest { + + @BeforeEach + public void createTestData(SessionFactoryScope scope) { + scope.inTransaction( + 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 ) ); + } ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery( "delete from Post" ).executeUpdate(); + session.createQuery( "delete from Category" ).executeUpdate(); + } + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-11375") + public void testLazyInitialization(SessionFactoryScope scope) { + scope.inTransaction( + 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 + assertTrue( category7.children.isEmpty() ); + } ); + } + + @Test + @TestForIssue(jiraKey = "HHH-11375") + public void testJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( + 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 + assertTrue( category7.children.isEmpty() ); + } ); + } + + @Entity(name = "PostTable") + @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(name = "Category") + @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(name = "Post") + @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; + } + } + +} 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 deleted file mode 100644 index 9f20354dfe..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/inheritance/discriminator/SingleTableRelationsTest.java +++ /dev/null @@ -1,159 +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.inheritance.discriminator; - -import java.util.List; -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 org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Assert; -import org.junit.Test; - -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}; - } - - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.FORCE_DISCRIMINATOR_IN_SELECTS_BY_DEFAULT, "true" ); - } - - 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; - } - } - -}