Skip to content

Commit 75238ba

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

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

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

Lines changed: 10 additions & 3 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;
@@ -58,9 +59,9 @@ public ImplicitToOneJoinTableSecondPass(
5859
}
5960

6061
// Note: Instead of deferring creation of the whole Table object, perhaps
61-
// we could create it in the first pass, and reset its name here in
62-
// the second pass. The problem is that there is some quite involved
63-
// logic in TableBinder that isn't set up for that.
62+
// we could create it in the first pass and reset its name here in
63+
// the second pass. The problem is that there is some quite involved
64+
// logic in TableBinder that isn't set up for that.
6465

6566
private void inferJoinTableName(TableBinder tableBinder, Map<String, PersistentClass> persistentClasses) {
6667
if ( isEmpty( tableBinder.getName() ) ) {
@@ -116,6 +117,12 @@ 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 owner = propertyHolder.getPersistentClass();
121+
final Property property = owner.getProperty( inferredData.getPropertyName() );
122+
assert property != null;
123+
// move the property from the main table to the new join table
124+
owner.removeProperty( property );
125+
join.addProperty( property );
119126
if ( notFoundAction != null ) {
120127
join.disableForeignKeyCreation();
121128
}

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,52 @@
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+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.AssertionsKt.assertNotNull;
15+
16+
@Jpa(annotatedClasses =
17+
{ManyToOneImplicitJoinTableTest.X.class,
18+
ManyToOneImplicitJoinTableTest.Y.class})
19+
class ManyToOneImplicitJoinTableTest {
20+
@JiraKey("HHH-19564") @Test
21+
void test(EntityManagerFactoryScope scope) {
22+
scope.inTransaction( s -> {
23+
X x = new X();
24+
Y y = new Y();
25+
y.x = x;
26+
s.persist( x );
27+
s.persist( y );
28+
} );
29+
scope.inTransaction( s -> {
30+
Y y = s.find( Y.class, 0L );
31+
y.name = "Gavin";
32+
} );
33+
scope.inTransaction( s -> {
34+
Y y = s.find( Y.class, 0L );
35+
assertEquals("Gavin", y.name);
36+
assertNotNull(y.x);
37+
} );
38+
}
39+
@Entity(name="Y")
40+
static class Y {
41+
@Id
42+
long id;
43+
String name;
44+
@JoinTable
45+
@ManyToOne X x;
46+
}
47+
@Entity(name="X")
48+
static class X {
49+
@Id
50+
long id;
51+
}
52+
}

0 commit comments

Comments
 (0)