Skip to content

Commit bddd49c

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

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

hibernate-core/src/main/java/org/hibernate/annotations/EmbeddedColumnNaming.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package org.hibernate.annotations;
66

7+
import org.hibernate.Incubating;
8+
79
import java.lang.annotation.Retention;
810
import java.lang.annotation.Target;
911

@@ -18,10 +20,12 @@
1820
* {@code @Embedded @EmbeddedColumnNaming("home_%s)}, we will get columns named
1921
* {@code home_street}, {@code home_city}, etc.
2022
*
23+
* @since 7.0
2124
* @author Steve Ebersole
2225
*/
2326
@Target({METHOD, FIELD})
2427
@Retention(RUNTIME)
28+
@Incubating
2529
public @interface EmbeddedColumnNaming {
2630
/**
2731
* The naming pattern. It is expected to contain a single pattern marker ({@code %})

hibernate-core/src/main/java/org/hibernate/boot/models/HibernateAnnotations.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ public interface HibernateAnnotations {
240240
EmbeddableInstantiatorRegistrationAnnotation.class,
241241
EMBEDDABLE_INSTANTIATOR_REGISTRATIONS
242242
);
243+
OrmAnnotationDescriptor<EmbeddedColumnNaming,EmbeddedColumnNamingAnnotation> EMBEDDED_COLUMN_NAMING = new OrmAnnotationDescriptor<>(
244+
EmbeddedColumnNaming.class,
245+
EmbeddedColumnNamingAnnotation.class
246+
);
243247
OrmAnnotationDescriptor<Fetch,FetchAnnotation> FETCH = new OrmAnnotationDescriptor<>(
244248
Fetch.class,
245249
FetchAnnotation.class
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.boot.models.annotations.internal;
6+
7+
import org.hibernate.annotations.EmbeddedColumnNaming;
8+
import org.hibernate.models.spi.SourceModelBuildingContext;
9+
10+
import java.lang.annotation.Annotation;
11+
import java.util.Map;
12+
13+
@SuppressWarnings({ "ClassExplicitlyAnnotation", "unused" })
14+
public class EmbeddedColumnNamingAnnotation implements EmbeddedColumnNaming {
15+
private String value;
16+
17+
/**
18+
* Used in creating dynamic annotation instances (e.g. from XML)
19+
*/
20+
public EmbeddedColumnNamingAnnotation(SourceModelBuildingContext modelContext) {
21+
}
22+
23+
/**
24+
* Used in creating annotation instances from JDK variant
25+
*/
26+
public EmbeddedColumnNamingAnnotation(
27+
EmbeddedColumnNaming annotation,
28+
SourceModelBuildingContext modelContext) {
29+
this.value = annotation.value();
30+
}
31+
32+
/**
33+
* Used in creating annotation instances from Jandex variant
34+
*/
35+
public EmbeddedColumnNamingAnnotation(
36+
Map<String, Object> attributeValues,
37+
SourceModelBuildingContext modelContext) {
38+
this.value = (String) attributeValues.get( "value" );
39+
}
40+
41+
@Override
42+
public Class<? extends Annotation> annotationType() {
43+
return EmbeddedColumnNaming.class;
44+
}
45+
46+
@Override
47+
public String value() {
48+
return value;
49+
}
50+
51+
public void value(String value) {
52+
this.value = value;
53+
}
54+
}

migration-guide.adoc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,44 @@ in addition to the previous truth-based strategies.
132132
See the link:{user-guide-url}#soft-delete[User Guide] for details.
133133

134134

135+
[[embedded-column-naming]]
136+
== @EmbeddedColumnNaming
137+
138+
A long requested feature for both Hibernate and Jakarta Persistence has been the ability to
139+
define a prefix for the names of columns associated with an embedded value.
140+
141+
7.0 adds support for this using the new `@EmbeddedColumnNaming` annotation. The annotation
142+
accepts a format pattern, so is a little more flexible than just a prefix.
143+
144+
Consider a typical Person / Address composition:
145+
146+
[source,java]
147+
----
148+
@Embeddable
149+
class Address {
150+
String street;
151+
String city;
152+
...
153+
}
154+
155+
@Entity
156+
class Person {
157+
...
158+
159+
@Embedded
160+
@EmbeddedColumnNaming("home_%")
161+
Address homeAddress;
162+
163+
@Embedded
164+
@EmbeddedColumnNaming("work_%")
165+
Address workAddress;
166+
167+
}
168+
----
169+
170+
This triggers Hibernate to use the column names `home_street`, `home_city`, `work_street`, ...
171+
172+
135173
[[envers-rev-types]]
136174
== Hibernate Envers and custom revision entities
137175

0 commit comments

Comments
 (0)