Skip to content

Commit e3266c6

Browse files
committed
HHH-4396 - Ability to patternize embedded column names
1 parent 8baef74 commit e3266c6

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.embeddable;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Embeddable;
9+
import jakarta.persistence.Embedded;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.Id;
12+
import jakarta.persistence.Table;
13+
import org.hibernate.MappingException;
14+
import org.hibernate.annotations.EmbeddedColumnNaming;
15+
import org.hibernate.engine.spi.SessionFactoryImplementor;
16+
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
17+
import org.hibernate.persister.entity.EntityPersister;
18+
import org.hibernate.testing.orm.junit.DomainModel;
19+
import org.hibernate.testing.orm.junit.NotImplementedYet;
20+
import org.hibernate.testing.orm.junit.ServiceRegistry;
21+
import org.hibernate.testing.orm.junit.SessionFactory;
22+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.hibernate.orm.test.embeddable.EmbeddedColumnNamingTests.verifyColumnNames;
27+
import static org.junit.jupiter.api.Assertions.fail;
28+
29+
/**
30+
* @author Steve Ebersole
31+
*/
32+
@SuppressWarnings("JUnitMalformedDeclaration")
33+
public class EmbeddedColumnNamingMixedTests {
34+
35+
/**
36+
* Test use of {@code @EmbeddedColumnNaming} one 2 separate embedded attributes with
37+
* a nested embeddable using explicit column names
38+
*/
39+
@Test
40+
@ServiceRegistry
41+
@DomainModel(annotatedClasses = {Address.class, Person.class})
42+
@SessionFactory
43+
void smokeTest(SessionFactoryScope sfScope) {
44+
// atm this mixing is not supported / implemented - tbd
45+
try (SessionFactoryImplementor sessionFactory = sfScope.getSessionFactory()) {
46+
fail( "Expecting an exception" );
47+
}
48+
catch (RuntimeException e) {
49+
assertThat( e ).hasCauseExactlyInstanceOf( MappingException.class );
50+
assertThat( e ).cause().hasMessageStartingWith( "Column 'zip_code' is duplicated in mapping for entity" );
51+
}
52+
}
53+
54+
/**
55+
* Test use of {@code @EmbeddedColumnNaming} one 2 separate embedded attributes with
56+
* a nested embeddable using explicit column names
57+
*/
58+
@Test
59+
@ServiceRegistry
60+
@DomainModel(annotatedClasses = {Address.class, Person.class})
61+
@SessionFactory
62+
@NotImplementedYet( reason = "this mixing is not supported / implemented - tbd" )
63+
void testMixedNamingPattern(SessionFactoryScope sfScope) {
64+
final SessionFactoryImplementor sessionFactory = sfScope.getSessionFactory();
65+
final MappingMetamodelImplementor mappingMetamodel = sessionFactory.getMappingMetamodel();
66+
final EntityPersister persister = mappingMetamodel.getEntityDescriptor( Person.class );
67+
verifyColumnNames( persister.findAttributeMapping( "homeAddress" ), "home_" );
68+
verifyColumnNames( persister.findAttributeMapping( "workAddress" ), "work_" );
69+
}
70+
71+
@Entity(name="Person")
72+
@Table(name="person")
73+
public static class Person {
74+
@Id
75+
private Integer id;
76+
private String name;
77+
78+
@Embedded
79+
@EmbeddedColumnNaming("home_%s")
80+
private Address homeAddress;
81+
82+
@Embedded
83+
@EmbeddedColumnNaming("work_%s")
84+
private Address workAddress;
85+
}
86+
87+
@Embeddable
88+
public static class Address {
89+
private String street;
90+
private String city;
91+
private String state;
92+
@Embedded
93+
private ZipPlus zip;
94+
}
95+
96+
@Embeddable
97+
public static class ZipPlus {
98+
@Column(name="zip_code")
99+
private String code;
100+
@Column(name="zip_plus4")
101+
private String plus4;
102+
}
103+
}

hibernate-core/src/test/java/org/hibernate/orm/test/naming/NestedEmbeddableNamingTests.java renamed to hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/EmbeddedColumnNamingNestedTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: LGPL-2.1-or-later
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.orm.test.naming;
5+
package org.hibernate.orm.test.embeddable;
66

77
import jakarta.persistence.Embeddable;
88
import jakarta.persistence.Embedded;
@@ -20,13 +20,13 @@
2020
import org.hibernate.testing.orm.junit.SessionFactoryScope;
2121
import org.junit.jupiter.api.Test;
2222

23-
import static org.hibernate.orm.test.naming.EmbeddedColumnNamingTests.verifyColumnNames;
23+
import static org.hibernate.orm.test.embeddable.EmbeddedColumnNamingTests.verifyColumnNames;
2424

2525
/**
2626
* @author Steve Ebersole
2727
*/
2828
@SuppressWarnings("JUnitMalformedDeclaration")
29-
public class NestedEmbeddableNamingTests {
29+
public class EmbeddedColumnNamingNestedTests {
3030

3131
/**
3232
* Test use of {@code @EmbeddedColumnNaming} one 2 separate embedded attributes

hibernate-core/src/test/java/org/hibernate/orm/test/naming/EmbeddedColumnNamingTests.java renamed to hibernate-core/src/test/java/org/hibernate/orm/test/embeddable/EmbeddedColumnNamingTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: LGPL-2.1-or-later
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.orm.test.naming;
5+
package org.hibernate.orm.test.embeddable;
66

77
import org.hibernate.MappingException;
88
import org.hibernate.annotations.EmbeddedColumnNaming;
@@ -265,4 +265,21 @@ public static class BadPatternPerson2 {
265265
@EmbeddedColumnNaming("work_%s")
266266
private Address workAddress;
267267
}
268+
269+
@Embeddable
270+
public static class PhoneNumber {
271+
private String countryCode;
272+
private String areaCode;
273+
private String prefix;
274+
private String lineNumber;
275+
}
276+
277+
@Entity(name="Company")
278+
@Table(name="companies")
279+
public static class Company {
280+
@Id
281+
private Integer id;
282+
private String name;
283+
284+
}
268285
}

0 commit comments

Comments
 (0)