Skip to content

Commit 94aa9bb

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

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

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

Lines changed: 5 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,10 @@ 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+
persistentClass.removeProperty( property );
123+
join.addProperty( property );
119124
if ( notFoundAction != null ) {
120125
join.disableForeignKeyCreation();
121126
}

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
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.Jpa;
10+
import org.junit.jupiter.api.Test;
11+
12+
@Jpa(annotatedClasses =
13+
{ManyToOneJoinTableUpdateTest.X.class,
14+
ManyToOneJoinTableUpdateTest.Y.class})
15+
class ManyToOneJoinTableUpdateTest {
16+
@Test void test(EntityManagerFactoryScope scope) {
17+
scope.inTransaction( s -> {
18+
X x = new X();
19+
Y y = new Y();
20+
y.x = x;
21+
s.persist( x );
22+
s.persist( y );
23+
} );
24+
scope.inTransaction( s -> {
25+
Y y = s.find( Y.class, 0L );
26+
y.name = "Gavin";
27+
} );
28+
}
29+
@Entity
30+
static class Y {
31+
@Id
32+
long id;
33+
String name;
34+
@JoinTable
35+
@ManyToOne X x;
36+
}
37+
@Entity
38+
static class X {
39+
@Id
40+
long id;
41+
}
42+
}

0 commit comments

Comments
 (0)