Skip to content

Commit b9351cf

Browse files
cigalygavinking
authored andcommitted
HHH-18829 Test:
- Hibernate Processor - generate ID class in metadata class if entity not annotated with @IdClass; order of record components must match order of properties in entity class - Hibernate Core - check if ID class has been properly generated with preserved order of components - Annotated existing test entity classes with @exclude when ID class can not be generated without causing compilation errors
1 parent 158771b commit b9351cf

File tree

10 files changed

+212
-0
lines changed

10 files changed

+212
-0
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/derivedidentities/bidirectional/Order.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import jakarta.persistence.Id;
1414
import jakarta.persistence.OneToMany;
1515
import jakarta.persistence.Table;
16+
import org.hibernate.annotations.processing.Exclude;
1617

1718
@SuppressWarnings("serial")
1819
@Entity
20+
@Exclude
1921
@Table(name = "orders")
2022
public class Order implements Serializable
2123
{

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/derivedidentities/bidirectional/OrderLine.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import jakarta.persistence.JoinColumn;
1111
import jakarta.persistence.ManyToOne;
1212
import jakarta.persistence.Table;
13+
import org.hibernate.annotations.processing.Exclude;
1314

1415
@SuppressWarnings("serial")
1516
@Entity
17+
@Exclude
1618
@Table(name = "order_line")
1719
// @IdClass(OrderLinePK.class)
1820
public class OrderLine implements Serializable

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/derivedidentities/bidirectional/Product.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import jakarta.persistence.GeneratedValue;
1010
import jakarta.persistence.Id;
1111
import jakarta.persistence.Table;
12+
import org.hibernate.annotations.processing.Exclude;
1213

1314
@SuppressWarnings("serial")
1415
@Entity
16+
@Exclude
1517
@Table(name = "products")
1618
public class Product implements Serializable
1719
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.mapping.hhh18829;
6+
7+
import org.hibernate.testing.orm.junit.DomainModel;
8+
import org.hibernate.testing.orm.junit.JiraKey;
9+
import org.hibernate.testing.orm.junit.SessionFactory;
10+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
11+
import org.junit.jupiter.api.BeforeAll;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.lang.reflect.InvocationTargetException;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
18+
@DomainModel(annotatedClasses = EmployeeWithoutIdClass.class)
19+
@JiraKey(" HHH-18829")
20+
@SessionFactory
21+
public class AutoGeneratedIdClassTest {
22+
23+
@BeforeAll
24+
void setUp(SessionFactoryScope sessionFactoryScope) {
25+
sessionFactoryScope.inTransaction( sess -> {
26+
final var one = new EmployeeWithoutIdClass();
27+
one.empName = "John Doe";
28+
one.empId = 1;
29+
one.address = "10 Downing Street, SW1A 2AA";
30+
sess.persist( one );
31+
32+
final var two = new EmployeeWithoutIdClass();
33+
two.empName = "Dave Default";
34+
two.empId = 1;
35+
two.address = "1600 Pennsylvania Avenue";
36+
sess.persist( two );
37+
} );
38+
}
39+
40+
@Test
41+
public void test(SessionFactoryScope sessionFactoryScope)
42+
throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
43+
final var idClass = Class.forName( EmployeeWithoutIdClass.class.getName() + "_$Id" );
44+
final var id = idClass.getConstructors()[0].newInstance( "John Doe", 1 );
45+
final var employees = sessionFactoryScope.fromSession(
46+
sess -> sess.createQuery( "from EmployeeWithoutIdClass where id=:id", EmployeeWithoutIdClass.class ).setParameter( "id", id )
47+
.getResultList()
48+
);
49+
assertEquals( 1, employees.size() );
50+
assertEquals( "10 Downing Street, SW1A 2AA", employees.get( 0 ).address );
51+
}
52+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.mapping.hhh18829;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
10+
@Entity
11+
public class EmployeeWithoutIdClass {
12+
@Id
13+
String empName;
14+
@Id
15+
Integer empId;
16+
String address;
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.hhh18829;
6+
7+
import jakarta.persistence.MappedSuperclass;
8+
9+
@MappedSuperclass
10+
public class Address {
11+
String address;
12+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.hhh18829;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
10+
@Entity
11+
public class AnotherEmployee extends Address {
12+
Integer empId;
13+
14+
String empName;
15+
16+
@Id
17+
public Integer getEmpId() {
18+
return empId;
19+
}
20+
21+
public void setEmpId(Integer empId) {
22+
this.empId = empId;
23+
}
24+
25+
@Id
26+
public String getEmpName() {
27+
return empName;
28+
}
29+
30+
public void setEmpName(String empName) {
31+
this.empName = empName;
32+
}
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.hhh18829;
6+
7+
import org.hibernate.processor.test.util.CompilationTest;
8+
import org.hibernate.processor.test.util.TestForIssue;
9+
import org.hibernate.processor.test.util.TestUtil;
10+
import org.hibernate.processor.test.util.WithClasses;
11+
import org.junit.Test;
12+
13+
import java.lang.reflect.RecordComponent;
14+
import java.util.Arrays;
15+
16+
import static org.hibernate.processor.test.util.TestUtil.getMetamodelClassFor;
17+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
20+
@TestForIssue(jiraKey = " HHH-18829")
21+
public class AutoGeneratedIdClassTest extends CompilationTest {
22+
23+
@Test
24+
@WithClasses({Employee.class, AnotherEmployee.class, Address.class, EmployeeWithIdClass.class})
25+
@TestForIssue(jiraKey = "HHH-18829")
26+
public void test() {
27+
System.out.println( TestUtil.getMetaModelSourceAsString( Employee.class ) );
28+
System.out.println( TestUtil.getMetaModelSourceAsString( AnotherEmployee.class ) );
29+
System.out.println( TestUtil.getMetaModelSourceAsString( Address.class ) );
30+
System.out.println( TestUtil.getMetaModelSourceAsString( EmployeeWithIdClass.class ) );
31+
32+
checkIfIdClassIsGenerated( Employee.class, new String[] {"empName", "empId"} );
33+
checkIfIdClassIsGenerated( AnotherEmployee.class, new String[] {"empId", "empName"} );
34+
35+
final var clazz = getMetamodelClassFor( EmployeeWithIdClass.class );
36+
assertTrue( Arrays.stream( clazz.getClasses() ).map( Class::getSimpleName )
37+
.noneMatch( "Id"::equals ),
38+
"EmployeeWithIdClass_ should not have inner class Id" );
39+
}
40+
41+
private static void checkIfIdClassIsGenerated(Class<?> entityClass, String[] idComponentNames) {
42+
final var clazz = getMetamodelClassFor( entityClass );
43+
final var maybeIdClass = Arrays.stream( clazz.getClasses() )
44+
.filter( c -> c.getSimpleName().equals( "Id" ) ).findAny();
45+
assertTrue( maybeIdClass.isPresent(), () -> clazz.getSimpleName() + "_ should have inner class Id" );
46+
final Class<?> idClass = maybeIdClass.get();
47+
assertTrue( idClass.isRecord(), "Generated ID class should be a record" );
48+
assertArrayEquals(
49+
idComponentNames,
50+
Arrays.stream( idClass.getRecordComponents() ).map( RecordComponent::getName ).toArray( String[]::new )
51+
);
52+
}
53+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.hhh18829;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
10+
@Entity
11+
public class Employee extends Address {
12+
@Id
13+
String empName;
14+
@Id
15+
Integer empId;
16+
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.hhh18829;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.IdClass;
10+
11+
@Entity
12+
@IdClass(EmployeeWithIdClass.EmployeeId.class)
13+
public class EmployeeWithIdClass extends Address {
14+
@Id
15+
String empName;
16+
@Id
17+
Integer empId;
18+
19+
public record EmployeeId(String empName, Integer empId) {
20+
}
21+
22+
}

0 commit comments

Comments
 (0)