From dcf62801dce11eaa4c992f46bafa60a7a5fa944d Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Tue, 13 Dec 2022 12:50:41 +0100 Subject: [PATCH] HHH-15822 Add test for issue --- .../entity/AbstractTreeableEntityTest.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/persister/entity/AbstractTreeableEntityTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/persister/entity/AbstractTreeableEntityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/persister/entity/AbstractTreeableEntityTest.java new file mode 100644 index 0000000000..0fffd3f546 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/persister/entity/AbstractTreeableEntityTest.java @@ -0,0 +1,105 @@ +/* + * 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 http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.persister.entity; + +import java.util.Collection; +import java.util.List; + +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 jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.MappedSuperclass; +import jakarta.persistence.OneToMany; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DomainModel(annotatedClasses = AbstractTreeableEntityTest.TestEntity.class) +@SessionFactory +@TestForIssue(jiraKey = "HHH-15822") +public class AbstractTreeableEntityTest { + + @Test + public void testGenericParentQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> { + TestEntity entity = new TestEntity(); + entity.setName( "test" ); + session.persist( entity ); + + CriteriaBuilder cb = session.getCriteriaBuilder(); + CriteriaQuery query = cb.createQuery( TestEntity.class ); + Root root = query.from( TestEntity.class ); + + query.select( root ).where( cb.equal( root.get( "parent" ) , entity ) ); + List resultList = session.createQuery( query ).getResultList(); + + assertEquals( 0, resultList.size() ); + } ); + } + + @Entity(name = "TestEntity") + public static class TestEntity extends AbstractTreeableEntity { + } + + @MappedSuperclass + public abstract static class AbstractTreeableEntity> { + @Id + @GeneratedValue + private Long id; + + protected String name; + + @ManyToOne(fetch = FetchType.LAZY) + protected T parent; + + @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "parent") + protected Collection children; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public T getParent() { + return parent; + } + + public void setParent(T parent) { + this.parent = parent; + } + + public Collection getChildren() { + return children; + } + + public void setChildren(Collection children) { + this.children = children; + } + } +}