HHH-16009 Add test for issue

This commit is contained in:
Andrea Boriero 2023-02-15 18:49:29 +01:00 committed by Christian Beikov
parent 546d145d88
commit ffd9c9b0f6
4 changed files with 176 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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<Department> departments;
public Set<Department> getDepartments() {
return departments;
}
public void setName(String name) {
this.name = name;
}
public void setDepartments(Set<Department> departments) {
this.departments = departments;
}
}

View File

@ -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<String> {
@Override
public String getSetting() {
return CamelCaseToUnderscoresNamingStrategy.class.getName();
}
}
public static class ImplicitNamingStrategyProvider implements SettingProvider.Provider<String> {
@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() );
}
}
}