4343 * @author Florian Lüdiger
4444 * @author Bastian Wilhelm
4545 * @author Kurt Niemi
46+ * @author Sergey Korotaev
4647 */
4748public class BasicRelationalPersistentProperty extends AnnotationBasedPersistentProperty <RelationalPersistentProperty >
4849 implements RelationalPersistentProperty {
@@ -51,11 +52,11 @@ public class BasicRelationalPersistentProperty extends AnnotationBasedPersistent
5152
5253 private final Lazy <SqlIdentifier > columnName ;
5354 private final boolean hasExplicitColumnName ;
54- private final @ Nullable Expression columnNameExpression ;
55+ private final Lazy < SqlIdentifier > columnNameExpression ;
5556 private final SqlIdentifier sequence ;
5657 private final Lazy <Optional <SqlIdentifier >> collectionIdColumnName ;
5758 private final Lazy <SqlIdentifier > collectionKeyColumnName ;
58- private final @ Nullable Expression collectionKeyColumnNameExpression ;
59+ private final Lazy < SqlIdentifier > collectionKeyColumnNameExpression ;
5960 private final boolean isEmbedded ;
6061 private final String embeddedPrefix ;
6162
@@ -101,10 +102,10 @@ public BasicRelationalPersistentProperty(Property property, PersistentEntity<?,
101102 () -> StringUtils .hasText (mappedCollection .keyColumn ()) ? createSqlIdentifier (mappedCollection .keyColumn ())
102103 : createDerivedSqlIdentifier (namingStrategy .getKeyColumn (this )));
103104
104- this .collectionKeyColumnNameExpression = detectExpression (mappedCollection .keyColumn ());
105+ this .collectionKeyColumnNameExpression = Lazy . of (() -> detectExpression (mappedCollection .keyColumn () ));
105106 } else {
106107
107- this .collectionKeyColumnNameExpression = null ;
108+ this .collectionKeyColumnNameExpression = Lazy . empty () ;
108109 }
109110
110111 if (isAnnotationPresent (Column .class )) {
@@ -114,7 +115,7 @@ public BasicRelationalPersistentProperty(Property property, PersistentEntity<?,
114115
115116 this .columnName = Lazy .of (() -> StringUtils .hasText (column .value ()) ? createSqlIdentifier (column .value ())
116117 : createDerivedSqlIdentifier (namingStrategy .getColumnName (this )));
117- this .columnNameExpression = detectExpression (column .value ());
118+ this .columnNameExpression = Lazy . of (() -> detectExpression (column .value () ));
118119
119120 if (collectionIdColumnName == null && StringUtils .hasText (column .value ())) {
120121 collectionIdColumnName = Lazy .of (() -> Optional .of (createSqlIdentifier (column .value ())));
@@ -123,7 +124,7 @@ public BasicRelationalPersistentProperty(Property property, PersistentEntity<?,
123124 } else {
124125 this .hasExplicitColumnName = false ;
125126 this .columnName = Lazy .of (() -> createDerivedSqlIdentifier (namingStrategy .getColumnName (this )));
126- this .columnNameExpression = null ;
127+ this .columnNameExpression = Lazy . empty () ;
127128 }
128129
129130 this .sequence = determineSequenceName ();
@@ -145,17 +146,19 @@ void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator) {
145146 * {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
146147 *
147148 * @param potentialExpression can be {@literal null}
148- * @return can be {@literal null}.
149149 */
150- @ Nullable
151- private static Expression detectExpression (@ Nullable String potentialExpression ) {
150+ private SqlIdentifier detectExpression (@ Nullable String potentialExpression ) {
152151
153152 if (!StringUtils .hasText (potentialExpression )) {
154153 return null ;
155154 }
156155
157156 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 ));
159162 }
160163
161164 private SqlIdentifier createSqlIdentifier (String name ) {
@@ -186,12 +189,8 @@ public boolean isEntity() {
186189
187190 @ Override
188191 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 ());
195194 }
196195
197196 @ Override
@@ -218,11 +217,8 @@ public SqlIdentifier getKeyColumn() {
218217 return null ;
219218 }
220219
221- if (collectionKeyColumnNameExpression == null ) {
222- return collectionKeyColumnName .get ();
223- }
224-
225- return createSqlIdentifier (expressionEvaluator .evaluate (collectionKeyColumnNameExpression ));
220+ return collectionKeyColumnNameExpression .getOptional ()
221+ .orElse (collectionKeyColumnName .get ());
226222 }
227223
228224 @ Override
0 commit comments