diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/Department.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/Department.java new file mode 100644 index 0000000000..7049476457 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/Department.java @@ -0,0 +1,37 @@ +package org.hibernate.orm.test.annotations.onetomany.orderby; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.IdClass; +import jakarta.persistence.ManyToOne; + +@Entity(name = "Department") +@IdClass(DepartmentId.class) +public class Department { + + @Id + @ManyToOne + private ECompany company; + + + @Id + private String departmentCode; + + private String name; + + public String getName() { + return name; + } + + public void setCompany(ECompany company) { + this.company = company; + } + + public void setName(String name) { + this.name = name; + } + + public void setDepartmentCode(String departmentId) { + this.departmentCode = departmentId; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/DepartmentId.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/DepartmentId.java new file mode 100644 index 0000000000..0d69724811 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/DepartmentId.java @@ -0,0 +1,10 @@ +package org.hibernate.orm.test.annotations.onetomany.orderby; + +import java.io.Serializable; + +public class DepartmentId implements Serializable { + + private ECompany company; + private String departmentCode; + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/ECompany.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/ECompany.java new file mode 100644 index 0000000000..66f94b8c5a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/ECompany.java @@ -0,0 +1,36 @@ +package org.hibernate.orm.test.annotations.onetomany.orderby; + +import java.util.Set; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OrderBy; + +@Entity(name = "Company") +public class ECompany { + + @Id + @GeneratedValue + private Long id; + + private String name; + + @OneToMany(mappedBy = "company", cascade = CascadeType.ALL) + @OrderBy("departmentCode DESC") + private Set departments; + + public Set getDepartments() { + return departments; + } + + public void setName(String name) { + this.name = name; + } + + public void setDepartments(Set departments) { + this.departments = departments; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/IdClassAndOrderByTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/IdClassAndOrderByTest.java new file mode 100644 index 0000000000..b9cbf3dd78 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/onetomany/orderby/IdClassAndOrderByTest.java @@ -0,0 +1,93 @@ +package org.hibernate.orm.test.annotations.onetomany.orderby; + +import org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy; +import org.hibernate.boot.model.naming.Identifier; +import org.hibernate.boot.model.naming.ImplicitJoinTableNameSource; +import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl; +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.SettingProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +@Jpa( + annotatedClasses = { + ECompany.class, + Department.class + }, + settingProviders = { + @SettingProvider( + settingName = AvailableSettings.PHYSICAL_NAMING_STRATEGY, + provider = IdClassAndOrderByTest.PhysicalNamingStrategyProvider.class + ), + @SettingProvider( + settingName = AvailableSettings.IMPLICIT_NAMING_STRATEGY, + provider = IdClassAndOrderByTest.ImplicitNamingStrategyProvider.class + ), + + } +) +@TestForIssue(jiraKey = "HHH-16009") +public class IdClassAndOrderByTest { + + public static final String COMPANY_NAME = "Foo Company"; + + public static class PhysicalNamingStrategyProvider implements SettingProvider.Provider { + @Override + public String getSetting() { + return CamelCaseToUnderscoresNamingStrategy.class.getName(); + } + } + + public static class ImplicitNamingStrategyProvider implements SettingProvider.Provider { + @Override + public String getSetting() { + return CustomImplicitNamingStrategy.class.getName(); + } + } + + @BeforeAll + public void setUp(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + ECompany company = new ECompany(); + company.setName( COMPANY_NAME ); + entityManager.persist( company ); + + Department department = new Department(); + department.setCompany( company ); + department.setDepartmentCode( "1234567" ); + department.setName( "Foo Department" ); + entityManager.persist( department ); + } + ); + + } + + @Test + public void testSelect(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + entityManager.createQuery( + "SELECT c FROM Company c LEFT JOIN FETCH c.departments WHERE c.name = :name" ) + .setParameter( "name", COMPANY_NAME ).getResultList(); + } + ); + } + + + public static class CustomImplicitNamingStrategy extends ImplicitNamingStrategyJpaCompliantImpl { + public CustomImplicitNamingStrategy() { + } + + @Override + public Identifier determineJoinTableName(ImplicitJoinTableNameSource source) { + String var10000 = source.getOwningPhysicalTableName(); + String name = var10000 + "_" + source.getAssociationOwningAttributePath().getProperty(); + return this.toIdentifier( name, source.getBuildingContext() ); + } + } +}