Skip to content

Commit 1b9f636

Browse files
committed
HHH-12969 - Metadata for composite-id attributes inaccurate when an id-class is specified
1 parent d29b842 commit 1b9f636

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.metamodel;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.EntityManagerFactory;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.IdClass;
11+
import jakarta.persistence.ManyToOne;
12+
import jakarta.persistence.Table;
13+
import jakarta.persistence.metamodel.EmbeddableType;
14+
import jakarta.persistence.metamodel.EntityType;
15+
import jakarta.persistence.metamodel.SingularAttribute;
16+
import org.hibernate.testing.orm.junit.DomainModel;
17+
import org.hibernate.testing.orm.junit.SessionFactory;
18+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
19+
import org.junit.jupiter.api.Test;
20+
21+
import java.io.Serializable;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.fail;
25+
26+
/**
27+
* @author Steve Ebersole
28+
*/
29+
@SuppressWarnings("JUnitMalformedDeclaration")
30+
public class CompositeIdAttributeAccessTests {
31+
@Test
32+
@DomainModel(annotatedClasses = {AnEntity.PK.class, AnEntity.class, AnotherEntity.class})
33+
@SessionFactory
34+
void testSubAttributeAccess(SessionFactoryScope sessions) {
35+
final EntityManagerFactory sessionFactory = (EntityManagerFactory) sessions.getSessionFactory();
36+
final EntityType<AnEntity> entityType = sessionFactory.getMetamodel().entity( AnEntity.class );
37+
checkEntityType( entityType );
38+
39+
final EmbeddableType<AnEntity.PK> idClassType = sessionFactory.getMetamodel().embeddable( AnEntity.PK.class );
40+
checkIdClassType( idClassType );
41+
}
42+
43+
private void checkEntityType(EntityType<AnEntity> entityType) {
44+
final SingularAttribute<? super AnEntity, ?> key1Attribute = entityType.getSingularAttribute( "key1" );
45+
assertThat( key1Attribute.isId() ).isTrue();
46+
assertThat( key1Attribute.getJavaType() ).isEqualTo( Integer.class );
47+
assertThat( key1Attribute.getJavaMember().getDeclaringClass() ).isEqualTo( AnEntity.class );
48+
49+
final SingularAttribute<? super AnEntity, ?> key2Attribute = entityType.getSingularAttribute( "key2" );
50+
assertThat( key2Attribute.isId() ).isTrue();
51+
assertThat( key2Attribute.getJavaType() ).isEqualTo( AnotherEntity.class );
52+
assertThat( key2Attribute.getJavaMember().getDeclaringClass() ).isEqualTo( AnEntity.class );
53+
54+
assertThat( entityType.getIdClassAttributes() ).hasSize( 2 );
55+
for ( SingularAttribute<? super AnEntity, ?> idClassAttribute : entityType.getIdClassAttributes() ) {
56+
if ( "key1".equals( idClassAttribute.getName() ) ) {
57+
assertThat( idClassAttribute ).isSameAs( key1Attribute );
58+
}
59+
else if ( "key2".equals( idClassAttribute.getName() ) ) {
60+
assertThat( idClassAttribute ).isSameAs( key2Attribute );
61+
}
62+
else {
63+
fail( "Unexpected attribute : " + idClassAttribute );
64+
}
65+
}
66+
}
67+
68+
private void checkIdClassType(EmbeddableType<AnEntity.PK> idClassType) {
69+
assertThat( idClassType.getAttributes() ).hasSize( 2 );
70+
71+
final SingularAttribute<? super AnEntity.PK, ?> key1Attribute = idClassType.getSingularAttribute( "key1" );
72+
// we don't know this at the embeddable level
73+
assertThat( key1Attribute.isId() ).isFalse();
74+
assertThat( key1Attribute.getJavaType() ).isEqualTo( Integer.class );
75+
assertThat( key1Attribute.getJavaMember().getDeclaringClass() ).isEqualTo( AnEntity.PK.class );
76+
77+
final SingularAttribute<? super AnEntity.PK, ?> key2Attribute = idClassType.getSingularAttribute( "key2" );
78+
// we don't know this at the embeddable level
79+
assertThat( key2Attribute.isId() ).isFalse();
80+
assertThat( key2Attribute.getJavaType() ).isEqualTo( Integer.class );
81+
assertThat( key2Attribute.getJavaMember().getDeclaringClass() ).isEqualTo( AnEntity.PK.class );
82+
}
83+
84+
@Entity(name="AnEntity")
85+
@Table(name="entity_a")
86+
@IdClass(AnEntity.PK.class)
87+
public static class AnEntity {
88+
@Id
89+
private Integer key1;
90+
@Id @ManyToOne
91+
private AnotherEntity key2;
92+
93+
private String name;
94+
95+
public static class PK implements Serializable {
96+
private Integer key1;
97+
private Integer key2;
98+
}
99+
}
100+
101+
@Entity(name="AnotherEntity")
102+
@Table(name="entity_b")
103+
public static class AnotherEntity {
104+
@Id
105+
private Integer id;
106+
private String name;
107+
}
108+
}

0 commit comments

Comments
 (0)