19
19
* preferring to conform to JPA standards.
20
20
* <p>
21
21
* For the legacy JPA-based naming standards initially implemented by Hibernate,
22
- * see/use {@link ImplicitNamingStrategyLegacyJpaImpl}
22
+ * see/use {@link ImplicitNamingStrategyLegacyJpaImpl}.
23
23
*
24
24
* @author Steve Ebersole
25
25
*/
@@ -31,55 +31,50 @@ public ImplicitNamingStrategyJpaCompliantImpl() {
31
31
32
32
@ Override
33
33
public Identifier determinePrimaryTableName (ImplicitEntityNameSource source ) {
34
- if ( source == null ) {
35
- // should never happen, but to be defensive...
36
- throw new HibernateException ( "Entity naming information was not provided." );
37
- }
38
-
34
+ assert source != null ;
39
35
final String tableName = transformEntityName ( source .getEntityNaming () );
40
-
41
36
if ( tableName == null ) {
42
- // todo : add info to error message - but how to know what to write since we failed to interpret the naming source
43
- throw new HibernateException ( "Could not determine primary table name for entity" );
37
+ throw new HibernateException ( "Could not determine primary table name for entity: "
38
+ + source . getEntityNaming (). getClassName () );
44
39
}
45
-
46
40
return toIdentifier ( tableName , source .getBuildingContext () );
47
41
}
48
42
49
43
protected String transformEntityName (EntityNaming entityNaming ) {
50
- // prefer the JPA entity name, if specified...
51
- if ( isNotEmpty ( entityNaming .getJpaEntityName () ) ) {
52
- return entityNaming .getJpaEntityName ();
53
- }
54
- else {
55
- // otherwise, use the Hibernate entity name
56
- return unqualify ( entityNaming .getEntityName () );
57
- }
44
+ return isNotEmpty ( entityNaming .getJpaEntityName () )
45
+ // prefer the JPA entity name, if specified
46
+ ? entityNaming .getJpaEntityName ()
47
+ // otherwise, use the unqualified Hibernate entity name
48
+ : unqualify ( entityNaming .getEntityName () );
58
49
}
59
50
60
51
52
+ /**
53
+ * JPA states we should use the following as default:
54
+ * <blockquote>The concatenated names of the two associated primary entity
55
+ * tables (owning side first), separated by an underscore.</blockquote>
56
+ * That is:
57
+ * <pre>{OWNING SIDE PRIMARY TABLE NAME}_{NON-OWNING SIDE PRIMARY TABLE NAME}</pre>
58
+ */
61
59
@ Override
62
60
public Identifier determineJoinTableName (ImplicitJoinTableNameSource source ) {
63
- // JPA states we should use the following as default:
64
- // "The concatenated names of the two associated primary entity tables (owning side
65
- // first), separated by an underscore."
66
- // aka:
67
- // {OWNING SIDE PRIMARY TABLE NAME}_{NON-OWNING SIDE PRIMARY TABLE NAME}
68
61
final String name = source .getOwningPhysicalTableName ()
69
62
+ '_'
70
63
+ source .getNonOwningPhysicalTableName ();
71
64
return toIdentifier ( name , source .getBuildingContext () );
72
65
}
73
66
74
-
67
+ /**
68
+ * JPA states we should use the following as default:
69
+ * <blockquote>The concatenation of the name of the containing entity and the
70
+ * name of the collection attribute, separated by an underscore.</blockquote>
71
+ * That is, if owning entity has a JPA entity name:
72
+ * <pre>{OWNER JPA ENTITY NAME}_{COLLECTION ATTRIBUTE NAME}</pre>
73
+ * otherwise:
74
+ * <pre>{OWNER ENTITY NAME}_{COLLECTION ATTRIBUTE NAME}</pre>
75
+ */
75
76
@ Override
76
77
public Identifier determineCollectionTableName (ImplicitCollectionTableNameSource source ) {
77
- // JPA states we should use the following as default:
78
- // "The concatenation of the name of the containing entity and the name of the
79
- // collection attribute, separated by an underscore.
80
- // aka:
81
- // if owning entity has a JPA entity name: {OWNER JPA ENTITY NAME}_{COLLECTION ATTRIBUTE NAME}
82
- // otherwise: {OWNER ENTITY NAME}_{COLLECTION ATTRIBUTE NAME}
83
78
final String name = transformEntityName ( source .getOwningEntityNaming () )
84
79
+ '_'
85
80
+ transformAttributePath ( source .getOwningAttributePath () );
@@ -111,51 +106,47 @@ public Identifier determineTenantIdColumnName(ImplicitTenantIdColumnNameSource s
111
106
);
112
107
}
113
108
109
+ /**
110
+ * JPA states we should use the following as default:
111
+ * <blockquote>The property or field name</blockquote>
112
+ * That is, the unqualified attribute path.
113
+ */
114
114
@ Override
115
115
public Identifier determineBasicColumnName (ImplicitBasicColumnNameSource source ) {
116
- // JPA states we should use the following as default:
117
- // "The property or field name"
118
- // aka:
119
- // The unqualified attribute path.
120
116
return toIdentifier ( transformAttributePath ( source .getAttributePath () ), source .getBuildingContext () );
121
117
}
122
118
119
+ /**
120
+ * JPA states we should use the following as default:
121
+ * <ul>
122
+ * <li>If there is a "referencing relationship property":
123
+ * <blockquote>The concatenation of the following: the name of the referencing
124
+ * relationship property or field of the referencing entity or embeddable class;
125
+ * {@code _}; the name of the referenced primary key column.</blockquote>
126
+ * <li>If there is no such "referencing relationship property",
127
+ * or if the association is an element collection:
128
+ * <blockquote>The concatenation of the following: the name of the entity;
129
+ * {@code _}; the name of the referenced primary key column</blockquote>
130
+ * </ul>
131
+ */
123
132
@ Override
124
133
public Identifier determineJoinColumnName (ImplicitJoinColumnNameSource source ) {
125
- // JPA states we should use the following as default:
126
- //
127
- // (1) if there is a "referencing relationship property":
128
- // "The concatenation of the following: the name of the referencing relationship
129
- // property or field of the referencing entity or embeddable class; "_"; the
130
- // name of the referenced primary key column."
131
- //
132
- // (2) if there is no such "referencing relationship property", or if the association is
133
- // an element collection:
134
- // "The concatenation of the following: the name of the entity; "_"; the name of the
135
- // referenced primary key column"
136
-
137
- // todo : we need to better account for "referencing relationship property"
138
-
134
+ // TODO: we need to better account for "referencing relationship property"
135
+ final String referencingPropertyOrEntity =
136
+ source .getNature () == ELEMENT_COLLECTION || source .getAttributePath () == null
137
+ ? transformEntityName ( source .getEntityNaming () )
138
+ : transformAttributePath ( source .getAttributePath () );
139
139
final String referencedColumnName = source .getReferencedColumnName ().getText ();
140
-
141
- final String name ;
142
- if ( source .getNature () == ELEMENT_COLLECTION
143
- || source .getAttributePath () == null ) {
144
- name = transformEntityName ( source .getEntityNaming () )
145
- + '_' + referencedColumnName ;
146
- }
147
- else {
148
- name = transformAttributePath ( source .getAttributePath () )
149
- + '_' + referencedColumnName ;
150
- }
151
-
140
+ final String name = referencingPropertyOrEntity + '_' + referencedColumnName ;
152
141
return toIdentifier ( name , source .getBuildingContext () );
153
142
}
154
143
144
+ /**
145
+ * JPA states we should use the following as default:
146
+ * <blockquote>the same name as the primary key column [of the referenced table]</blockquote>
147
+ */
155
148
@ Override
156
149
public Identifier determinePrimaryKeyJoinColumnName (ImplicitPrimaryKeyJoinColumnNameSource source ) {
157
- // JPA states we should use the following as default:
158
- // "the same name as the primary key column [of the referenced table]"
159
150
return source .getReferencedPrimaryKeyColumnName ();
160
151
}
161
152
0 commit comments