Skip to content

Commit 78793c3

Browse files
committed
HHH-19542: Fixed an issue with the negative test showing it was not kicking in
1 parent f24f35d commit 78793c3

File tree

2 files changed

+75
-10
lines changed

2 files changed

+75
-10
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import java.util.HashMap;
88
import java.util.Map;
9+
import java.util.Objects;
10+
import java.util.concurrent.atomic.AtomicReference;
911

1012
import org.hibernate.AnnotationException;
1113
import org.hibernate.boot.spi.MetadataBuildingContext;
@@ -29,6 +31,7 @@
2931
import static org.hibernate.boot.model.internal.ClassPropertyHolder.handleGenericComponentProperty;
3032
import static org.hibernate.boot.model.internal.PropertyBinder.hasIdAnnotation;
3133
import static org.hibernate.internal.util.StringHelper.isEmpty;
34+
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;
3235
import static org.hibernate.internal.util.StringHelper.qualifyConditionally;
3336
import static org.hibernate.spi.NavigablePath.IDENTIFIER_MAPPER_PROPERTY;
3437

@@ -68,6 +71,7 @@ public class ComponentPropertyHolder extends AbstractPropertyHolder {
6871

6972
private final String embeddedAttributeName;
7073
private final Map<String,AttributeConversionInfo> attributeConversionInfoMap;
74+
private final AtomicReference<AnnotatedColumn> annotatedColumn;
7175

7276
public ComponentPropertyHolder(
7377
Component component,
@@ -94,6 +98,12 @@ public ComponentPropertyHolder(
9498
this.embeddedAttributeName = "";
9599
this.attributeConversionInfoMap = processAttributeConversions( inferredData.getClassOrElementType() );
96100
}
101+
102+
if ( parent instanceof ComponentPropertyHolder parentHolder ) {
103+
this.annotatedColumn = parentHolder.annotatedColumn;
104+
} else {
105+
this.annotatedColumn = new AtomicReference<>();
106+
}
97107
}
98108

99109
/**
@@ -226,7 +236,7 @@ public void addProperty(Property property, MemberDetails attributeMemberDetails,
226236
// if not, change the component table if no properties are set
227237
// if a property is set already the core cannot support that
228238
final Table table = property.getValue().getTable();
229-
if ( !table.equals( getTable() ) || !columnTableIsExplicit( table, columns ) ) {
239+
if ( !table.equals( getTable() ) || !columnTableIsConsistent( columns ) ) {
230240
if ( component.getPropertySpan() == 0 ) {
231241
component.setTable( table );
232242
}
@@ -241,11 +251,13 @@ public void addProperty(Property property, MemberDetails attributeMemberDetails,
241251
addProperty( property, attributeMemberDetails, declaringClass );
242252
}
243253

244-
private boolean columnTableIsExplicit(Table table, AnnotatedColumns columns) {
254+
private boolean columnTableIsConsistent(AnnotatedColumns columns) {
245255
if ( columns != null ) {
246256
for ( AnnotatedColumn current : columns.getColumns() ) {
247-
final String explicitTableName = current.getExplicitTableName();
248-
if ( columns.isSecondary() && !table.getName().equals( explicitTableName ) ) {
257+
final AnnotatedColumn previous = annotatedColumn.getAndSet( current );
258+
if ( previous != null && !Objects.equals(
259+
nullIfEmpty( current.getExplicitTableName() ),
260+
nullIfEmpty( previous.getExplicitTableName() ) ) ) {
249261
return false;
250262
}
251263
}

hibernate-core/src/test/java/org/hibernate/orm/test/records/RecordNestedEmbeddedWithASecondaryTableTest.java

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,31 @@
1111
import jakarta.persistence.Id;
1212
import jakarta.persistence.SecondaryTable;
1313
import jakarta.persistence.Table;
14+
import org.hibernate.AnnotationException;
15+
import org.hibernate.boot.MetadataSources;
16+
import org.hibernate.boot.registry.StandardServiceRegistry;
1417
import org.hibernate.testing.orm.junit.DomainModel;
1518
import org.hibernate.testing.orm.junit.JiraKey;
19+
import org.hibernate.testing.orm.junit.ServiceRegistryScope;
1620
import org.hibernate.testing.orm.junit.SessionFactory;
1721
import org.hibernate.testing.orm.junit.SessionFactoryScope;
1822
import org.junit.jupiter.api.BeforeAll;
1923
import org.junit.jupiter.api.Test;
2024

2125
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.fail;
2227

2328
@JiraKey("HHH-19542")
2429
@DomainModel(annotatedClasses = {
2530
RecordNestedEmbeddedWithASecondaryTableTest.UserEntity.class
2631
})
2732
@SessionFactory
28-
public class RecordNestedEmbeddedWithASecondaryTableTest {
33+
class RecordNestedEmbeddedWithASecondaryTableTest {
2934

3035
private UserEntity user;
3136

3237
@BeforeAll
33-
public void prepare(SessionFactoryScope scope) {
38+
void prepare(SessionFactoryScope scope) {
3439
scope.inTransaction( session -> {
3540
Person person = new Person( new FullName( "Sylvain", "Lecoy" ), 38 );
3641
user = new UserEntity( person );
@@ -39,7 +44,7 @@ public void prepare(SessionFactoryScope scope) {
3944
}
4045

4146
@Test
42-
public void test(SessionFactoryScope scope) {
47+
void test(SessionFactoryScope scope) {
4348
scope.inTransaction(session -> {
4449
UserEntity entity = session.find( UserEntity.class, user.id );
4550
assertThat( entity ).isNotNull();
@@ -51,10 +56,23 @@ public void test(SessionFactoryScope scope) {
5156
});
5257
}
5358

59+
@Test
60+
void test(ServiceRegistryScope scope) {
61+
final StandardServiceRegistry registry = scope.getRegistry();
62+
final MetadataSources sources = new MetadataSources( registry ).addAnnotatedClass( UserEntity1.class );
63+
64+
try {
65+
sources.buildMetadata();
66+
fail( "Expecting to fail" );
67+
} catch (AnnotationException expected) {
68+
assertThat( expected ).hasMessageContaining( "all properties of the embeddable class must map to the same table" );
69+
}
70+
}
71+
5472
@Entity
5573
@Table(name = "UserEntity")
5674
@SecondaryTable(name = "Person")
57-
public static class UserEntity {
75+
static class UserEntity {
5876
@Id
5977
@GeneratedValue
6078
private Integer id;
@@ -71,19 +89,54 @@ protected UserEntity() {
7189
}
7290

7391
@Embeddable
74-
public record Person(
92+
record Person(
7593
FullName fullName,
7694
@Column(table = "Person")
7795
Integer age) {
7896

7997
}
8098

8199
@Embeddable
82-
public record FullName(
100+
record FullName(
83101
@Column(table = "Person")
84102
String firstName,
85103
@Column(table = "Person")
86104
String lastName) {
87105

88106
}
107+
108+
@Entity
109+
@Table(name = "UserEntity")
110+
@SecondaryTable(name = "Person")
111+
public static class UserEntity1 {
112+
@Id
113+
@GeneratedValue
114+
private Integer id;
115+
private Person1 person;
116+
117+
public UserEntity1(
118+
final Person1 person) {
119+
this.person = person;
120+
}
121+
122+
protected UserEntity1() {
123+
124+
}
125+
}
126+
127+
@Embeddable
128+
public record Person1(
129+
FullName1 fullName,
130+
@Column(table = "Person")
131+
Integer age) {
132+
133+
}
134+
135+
@Embeddable
136+
public record FullName1(
137+
@Column(table = "Person")
138+
String firstName,
139+
String lastName) {
140+
141+
}
89142
}

0 commit comments

Comments
 (0)