Skip to content

Commit e97d327

Browse files
committed
[HHH-17420][JBEAP-29294] JoinColumn throws an AnnotationException
1 parent f6c6827 commit e97d327

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,16 @@ else if ( columnOwner instanceof Join ) {
444444
// which are mapped to that column. (There might be multiple such
445445
// properties for each column.)
446446
if ( columnOwner instanceof PersistentClass ) {
447-
PersistentClass persistentClass = (PersistentClass) columnOwner;
447+
final PersistentClass persistentClass = (PersistentClass) columnOwner;
448+
// Process ToOne associations after Components, Basic and Id properties
449+
final List<Property> toOneProperties = new ArrayList<>();
448450
for ( Property property : persistentClass.getProperties() ) {
449-
matchColumnsByProperty( property, columnsToProperty );
451+
if ( property.getValue() instanceof ToOne ) {
452+
toOneProperties.add( property );
453+
}
454+
else {
455+
matchColumnsByProperty( property, columnsToProperty );
456+
}
450457
}
451458
if ( persistentClass.hasIdentifierProperty() ) {
452459
matchColumnsByProperty( persistentClass.getIdentifierProperty(), columnsToProperty );
@@ -458,6 +465,9 @@ else if ( columnOwner instanceof Join ) {
458465
matchColumnsByProperty( p, columnsToProperty );
459466
}
460467
}
468+
for ( Property property : toOneProperties ) {
469+
matchColumnsByProperty( property, columnsToProperty );
470+
}
461471
}
462472
else {
463473
for ( Property property : ((Join) columnOwner).getProperties() ) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.hibernate.orm.test.annotations.onetoone;
2+
3+
import java.io.Serializable;
4+
5+
import org.hibernate.testing.junit4.BaseUnitTestCase;
6+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
7+
import org.hibernate.testing.orm.junit.JiraKey;
8+
import org.hibernate.testing.orm.junit.Jpa;
9+
import org.junit.jupiter.api.Test;
10+
11+
import jakarta.persistence.Column;
12+
import jakarta.persistence.Embeddable;
13+
import jakarta.persistence.EmbeddedId;
14+
import jakarta.persistence.Entity;
15+
import jakarta.persistence.Id;
16+
import jakarta.persistence.JoinColumn;
17+
import jakarta.persistence.OneToOne;
18+
19+
@JiraKey("HHH-17420")
20+
@Jpa(
21+
annotatedClasses = {
22+
JoinColumnTest.LeftEntity.class,
23+
JoinColumnTest.MidEntity.class,
24+
JoinColumnTest.RightEntity.class,
25+
}
26+
)
27+
class JoinColumnTest extends BaseUnitTestCase {
28+
29+
@Test
30+
void testLeftToRight(EntityManagerFactoryScope scope) {
31+
scope.inTransaction(
32+
entityManager -> {
33+
}
34+
);
35+
}
36+
37+
@Entity(name = "LeftEntity")
38+
static class LeftEntity {
39+
40+
@Embeddable
41+
static class Pk implements Serializable {
42+
@Column
43+
String id_one_2;
44+
45+
@Column
46+
String id_two_2;
47+
48+
@Column
49+
String id_three_2;
50+
}
51+
52+
@EmbeddedId
53+
Pk id;
54+
55+
@OneToOne(mappedBy = "aLeftEntity")
56+
MidEntity midEntity;
57+
}
58+
59+
@Entity(name = "MidEntity")
60+
static class MidEntity {
61+
62+
@Id
63+
@Column
64+
String id;
65+
66+
@Column
67+
String id_one;
68+
69+
@Column
70+
String id_two;
71+
72+
@Column
73+
String id_three;
74+
75+
@OneToOne
76+
@JoinColumn(name = "id_one", referencedColumnName = "id_one_2", insertable = false, updatable = false)
77+
@JoinColumn(name = "id_two", referencedColumnName = "id_two_2", insertable = false, updatable = false)
78+
@JoinColumn(name = "id_three", referencedColumnName = "id_three_2", insertable = false, updatable = false)
79+
LeftEntity aLeftEntity;
80+
81+
@OneToOne(mappedBy = "midEntity")
82+
RightEntity rightEntity;
83+
}
84+
85+
@Entity(name = "RightEntity")
86+
static class RightEntity {
87+
88+
@Id
89+
Long id;
90+
91+
@Column
92+
String id_three;
93+
94+
@OneToOne
95+
@JoinColumn(name = "id_one", referencedColumnName = "id_one", insertable = false, updatable = false)
96+
@JoinColumn(name = "id_two", referencedColumnName = "id_two", insertable = false, updatable = false)
97+
MidEntity midEntity;
98+
}
99+
}

0 commit comments

Comments
 (0)