diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/manytomany/ManyToManyOrderByJoinedInheritanceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/manytomany/ManyToManyOrderByJoinedInheritanceTest.java new file mode 100644 index 0000000000..abf2337d0f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/manytomany/ManyToManyOrderByJoinedInheritanceTest.java @@ -0,0 +1,106 @@ +/* + * 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.mapping.manytomany; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.MappedSuperclass; +import jakarta.persistence.OrderBy; +import jakarta.persistence.Table; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Marco Belladelli + */ +@SessionFactory +@DomainModel( annotatedClasses = { + ManyToManyOrderByJoinedInheritanceTest.BaseEntity.class, + ManyToManyOrderByJoinedInheritanceTest.AnimalBase.class, + ManyToManyOrderByJoinedInheritanceTest.Animal.class, + ManyToManyOrderByJoinedInheritanceTest.Dog.class, + ManyToManyOrderByJoinedInheritanceTest.Human.class, +} ) +@Jira( "https://hibernate.atlassian.net/browse/HHH-16837" ) +public class ManyToManyOrderByJoinedInheritanceTest { + @Test + public void testLeftJoinFetch(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final List resultList = session.createQuery( + "select h from Human h left join fetch h.pets", + Human.class + ).getResultList(); + assertThat( resultList ).isNotNull(); + } ); + } + + @Test + public void testLeftJoin(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final List resultList = session.createQuery( + "select h from Human h left join h.pets", + Human.class + ).getResultList(); + assertThat( resultList ).isNotNull(); + } ); + } + + @MappedSuperclass + public abstract static class BaseEntity { + @Id + @GeneratedValue + protected Long id; + } + + @MappedSuperclass + public abstract static class AnimalBase extends BaseEntity { + protected String name; + protected String species; + } + + @Entity( name = "Animal" ) + @Inheritance( strategy = InheritanceType.JOINED ) + @Table( name = "animals" ) + public static class Animal extends AnimalBase { + private transient String unrelatedThing; + } + + @Entity( name = "Dog" ) + @Table( name = "dogs" ) + public static class Dog extends Animal { + private int barkIntensity; + } + + @Entity( name = "Human" ) + @Table( name = "humans" ) + public static class Human extends BaseEntity { + private String humanName; + + @ManyToMany + @OrderBy( "name" ) + @JoinTable( name = "human_pet", + inverseJoinColumns = @JoinColumn( name = "pet_id", referencedColumnName = "id" ), + joinColumns = @JoinColumn( name = "human_id", referencedColumnName = "id" ) ) + private Set pets = new HashSet<>(); + } +}