43
43
* @author Florian Lüdiger
44
44
* @author Bastian Wilhelm
45
45
* @author Kurt Niemi
46
+ * @author Sergey Korotaev
46
47
*/
47
48
public class BasicRelationalPersistentProperty extends AnnotationBasedPersistentProperty <RelationalPersistentProperty >
48
49
implements RelationalPersistentProperty {
@@ -51,11 +52,11 @@ public class BasicRelationalPersistentProperty extends AnnotationBasedPersistent
51
52
52
53
private final Lazy <SqlIdentifier > columnName ;
53
54
private final boolean hasExplicitColumnName ;
54
- private final @ Nullable Expression columnNameExpression ;
55
+ private final Lazy < SqlIdentifier > columnNameExpression ;
55
56
private final SqlIdentifier sequence ;
56
57
private final Lazy <Optional <SqlIdentifier >> collectionIdColumnName ;
57
58
private final Lazy <SqlIdentifier > collectionKeyColumnName ;
58
- private final @ Nullable Expression collectionKeyColumnNameExpression ;
59
+ private final Lazy < SqlIdentifier > collectionKeyColumnNameExpression ;
59
60
private final boolean isEmbedded ;
60
61
private final String embeddedPrefix ;
61
62
@@ -101,10 +102,10 @@ public BasicRelationalPersistentProperty(Property property, PersistentEntity<?,
101
102
() -> StringUtils .hasText (mappedCollection .keyColumn ()) ? createSqlIdentifier (mappedCollection .keyColumn ())
102
103
: createDerivedSqlIdentifier (namingStrategy .getKeyColumn (this )));
103
104
104
- this .collectionKeyColumnNameExpression = detectExpression (mappedCollection .keyColumn ());
105
+ this .collectionKeyColumnNameExpression = Lazy . of (() -> detectExpression (mappedCollection .keyColumn () ));
105
106
} else {
106
107
107
- this .collectionKeyColumnNameExpression = null ;
108
+ this .collectionKeyColumnNameExpression = Lazy . empty () ;
108
109
}
109
110
110
111
if (isAnnotationPresent (Column .class )) {
@@ -114,7 +115,7 @@ public BasicRelationalPersistentProperty(Property property, PersistentEntity<?,
114
115
115
116
this .columnName = Lazy .of (() -> StringUtils .hasText (column .value ()) ? createSqlIdentifier (column .value ())
116
117
: createDerivedSqlIdentifier (namingStrategy .getColumnName (this )));
117
- this .columnNameExpression = detectExpression (column .value ());
118
+ this .columnNameExpression = Lazy . of (() -> detectExpression (column .value () ));
118
119
119
120
if (collectionIdColumnName == null && StringUtils .hasText (column .value ())) {
120
121
collectionIdColumnName = Lazy .of (() -> Optional .of (createSqlIdentifier (column .value ())));
@@ -123,7 +124,7 @@ public BasicRelationalPersistentProperty(Property property, PersistentEntity<?,
123
124
} else {
124
125
this .hasExplicitColumnName = false ;
125
126
this .columnName = Lazy .of (() -> createDerivedSqlIdentifier (namingStrategy .getColumnName (this )));
126
- this .columnNameExpression = null ;
127
+ this .columnNameExpression = Lazy . empty () ;
127
128
}
128
129
129
130
this .sequence = determineSequenceName ();
@@ -145,17 +146,19 @@ void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator) {
145
146
* {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
146
147
*
147
148
* @param potentialExpression can be {@literal null}
148
- * @return can be {@literal null}.
149
149
*/
150
- @ Nullable
151
- private static Expression detectExpression (@ Nullable String potentialExpression ) {
150
+ private SqlIdentifier detectExpression (@ Nullable String potentialExpression ) {
152
151
153
152
if (!StringUtils .hasText (potentialExpression )) {
154
153
return null ;
155
154
}
156
155
157
156
Expression expression = PARSER .parseExpression (potentialExpression , ParserContext .TEMPLATE_EXPRESSION );
158
- return expression instanceof LiteralExpression ? null : expression ;
157
+ if (expression instanceof LiteralExpression ) {
158
+ return null ;
159
+ }
160
+
161
+ return createSqlIdentifier (expressionEvaluator .evaluate (expression ));
159
162
}
160
163
161
164
private SqlIdentifier createSqlIdentifier (String name ) {
@@ -186,12 +189,8 @@ public boolean isEntity() {
186
189
187
190
@ Override
188
191
public SqlIdentifier getColumnName () {
189
-
190
- if (columnNameExpression == null ) {
191
- return columnName .get ();
192
- }
193
-
194
- return createSqlIdentifier (expressionEvaluator .evaluate (columnNameExpression ));
192
+ return columnNameExpression .getOptional ()
193
+ .orElse (columnName .get ());
195
194
}
196
195
197
196
@ Override
@@ -218,11 +217,8 @@ public SqlIdentifier getKeyColumn() {
218
217
return null ;
219
218
}
220
219
221
- if (collectionKeyColumnNameExpression == null ) {
222
- return collectionKeyColumnName .get ();
223
- }
224
-
225
- return createSqlIdentifier (expressionEvaluator .evaluate (collectionKeyColumnNameExpression ));
220
+ return collectionKeyColumnNameExpression .getOptional ()
221
+ .orElse (collectionKeyColumnName .get ());
226
222
}
227
223
228
224
@ Override
0 commit comments