Skip to content

Commit 282371f

Browse files
committed
HHH-19564 fix @manytoone @jointable with implicit table name
1 parent 44c62a5 commit 282371f

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.hibernate.mapping.Join;
1515
import org.hibernate.mapping.ManyToOne;
1616
import org.hibernate.mapping.PersistentClass;
17+
import org.hibernate.mapping.Property;
1718
import org.hibernate.mapping.Table;
1819

1920
import jakarta.persistence.JoinTable;
@@ -116,6 +117,11 @@ public void doSecondPass(Map<String, PersistentClass> persistentClasses) {
116117
final Table table = tableBinder.bind();
117118
value.setTable( table );
118119
final Join join = propertyHolder.addJoin( joinTable, table, true );
120+
final PersistentClass persistentClass = propertyHolder.getPersistentClass();
121+
final Property property = persistentClass.getProperty( inferredData.getPropertyName() );
122+
assert property != null;
123+
persistentClass.removeProperty( property );
124+
join.addProperty( property );
119125
if ( notFoundAction != null ) {
120126
join.disableForeignKeyCreation();
121127
}

hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,4 +1211,11 @@ public Supplier<? extends Expectation> getDeleteExpectation() {
12111211
public void setDeleteExpectation(Supplier<? extends Expectation> deleteExpectation) {
12121212
this.deleteExpectation = deleteExpectation;
12131213
}
1214+
1215+
public void removeProperty(Property property) {
1216+
if ( !declaredProperties.remove( property ) ) {
1217+
throw new IllegalArgumentException( "Property not among declared properties: " + property.getName() );
1218+
}
1219+
properties.remove( property );
1220+
}
12141221
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.mapping.manytoone.jointable;
6+
7+
import jakarta.persistence.*;
8+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
9+
import org.hibernate.testing.orm.junit.JiraKey;
10+
import org.hibernate.testing.orm.junit.Jpa;
11+
import org.junit.jupiter.api.Test;
12+
13+
@Jpa(annotatedClasses =
14+
{ManyToOneImplicitJoinTableTest.X.class,
15+
ManyToOneImplicitJoinTableTest.Y.class})
16+
class ManyToOneImplicitJoinTableTest {
17+
@JiraKey("HHH-19564") @Test
18+
void test(EntityManagerFactoryScope scope) {
19+
scope.inTransaction( s -> {
20+
X x = new X();
21+
Y y = new Y();
22+
y.x = x;
23+
s.persist( x );
24+
s.persist( y );
25+
} );
26+
scope.inTransaction( s -> {
27+
Y y = s.find( Y.class, 0L );
28+
y.name = "Gavin";
29+
} );
30+
}
31+
@Entity
32+
static class Y {
33+
@Id
34+
long id;
35+
String name;
36+
@JoinTable
37+
@ManyToOne X x;
38+
}
39+
@Entity
40+
static class X {
41+
@Id
42+
long id;
43+
}
44+
}

0 commit comments

Comments
 (0)