28
28
import java .util .List ;
29
29
30
30
/**
31
- * JPQL exclusive ID(), VERSION() functions/expressions are transformed there to StateFieldPathExpression.
32
- * It should be used in the future for another JPQL functions/expressions which are not available at the DB level.
33
- * E.g. For Entity e with idAttr as a primary key: <code>SELECT ID(e) FROM Entity e -> SELECT e.idAttr FROM Entity e</code>
34
- * For Entity e with versionAttr as a version attribute: <code>SELECT VERSION(e) FROM Entity e -> SELECT e.versionAttr FROM Entity e</code>
31
+ * JPQL exclusive ID(), VERSION() functions/expressions are transformed there to
32
+ * StateFieldPathExpression.
33
+ * It should be used in the future for another JPQL functions/expressions which
34
+ * are not available at the DB level.
35
+ * E.g. For Entity e with idAttr as a primary key:
36
+ * <code>SELECT ID(e) FROM Entity e -> SELECT e.idAttr FROM Entity e</code>
37
+ * For Entity e with versionAttr as a version attribute:
38
+ * <code>SELECT VERSION(e) FROM Entity e -> SELECT e.versionAttr FROM Entity e</code>
35
39
*
36
40
* @author Radek Felcman
37
41
* @since 5.0
38
42
*/
39
43
public abstract class JPQLFunctionsAbstractBuilder extends EclipseLinkAnonymousExpressionVisitor {
40
44
41
45
/**
42
- * The {@link JPQLQueryContext} is used to query information about the application metadata and
46
+ * The {@link JPQLQueryContext} is used to query information about the
47
+ * application metadata and
43
48
* cached information.
44
49
*/
45
50
final JPQLQueryContext queryContext ;
@@ -49,57 +54,94 @@ protected JPQLFunctionsAbstractBuilder(JPQLQueryContext queryContext) {
49
54
}
50
55
51
56
/**
52
- * For Entity e with idAttr as a primary key: <code>SELECT ID(e) FROM Entity e -> SELECT e.idAttr FROM Entity e</code>
57
+ * For Entity e with idAttr as a primary key:
58
+ * <code>SELECT ID(e) FROM Entity e -> SELECT e.idAttr FROM Entity e</code>
53
59
*
54
60
* @param expression The {@link IdExpression} to visit
55
61
*/
56
62
@ Override
57
63
public void visit (IdExpression expression ) {
58
- //Fetch identification variable info
64
+ System . out . println ( "INSIDE VISIT *******" );
59
65
IdentificationVariable identificationVariable = (IdentificationVariable ) expression .getExpression ();
60
66
String variableText = identificationVariable .getText ();
61
67
String variableName = identificationVariable .getVariableName ();
62
-
63
- //Get id attribute name
68
+ // Get id attribute name
64
69
ClassDescriptor descriptor = this .queryContext .getDeclaration (variableName ).getDescriptor ();
65
70
List <DatabaseField > primaryKeyFields = descriptor .getPrimaryKeyFields ();
66
- String idAttributeName = getIdAttributeNameByField (descriptor .getMappings (), primaryKeyFields .get (0 ));
67
- StateFieldPathExpression stateFieldPathExpression = new StateFieldPathExpression (expression .getParent (), variableText + "." + idAttributeName );
68
- expression .setStateFieldPathExpression (stateFieldPathExpression );
71
+ // String idAttributeName = getIdAttributeNameByField(descriptor.getMappings(),
72
+ // primaryKeyFields.get(0));
73
+ // StateFieldPathExpression stateFieldPathExpression = new
74
+ // StateFieldPathExpression(expression.getParent(), variableText + "." +
75
+ // idAttributeName);
76
+ // expression.setStateFieldPathExpression(stateFieldPathExpression);
77
+ // expression.getStateFieldPathExpression().accept(this);
78
+ if (!isEmbeddable (descriptor .getMappings ())) {
79
+ for (DatabaseField primaryKeyField : primaryKeyFields ) {
80
+ String idAttributeName = getIdAttributeNameByField (descriptor .getMappings (), primaryKeyField );
81
+ StateFieldPathExpression stateFieldPathExpression = new StateFieldPathExpression (
82
+ expression .getParent (), variableText + "." + idAttributeName );
83
+ expression .setStateFieldPathExpression (stateFieldPathExpression );
84
+ // Continue with created StateFieldPathExpression
85
+ // It handle by ObjectBuilder booth @Id/primary key types (simple/composite)
86
+ expression .getStateFieldPathExpression ().accept (this );
87
+ }
88
+ } else {
89
+ String idAttributeName = getIdAttributeNameByField (descriptor .getMappings (), primaryKeyFields .get (0 ));
90
+ StateFieldPathExpression stateFieldPathExpression = new StateFieldPathExpression (
91
+ expression .getParent (), variableText + "." + idAttributeName );
92
+ expression .setStateFieldPathExpression (stateFieldPathExpression );
93
+ // Continue with created StateFieldPathExpression
94
+ // It handle by ObjectBuilder booth @Id/primary key types (simple/composite)
95
+ expression .getStateFieldPathExpression ().accept (this );
69
96
70
- //Continue with created StateFieldPathExpression
71
- //It handle by ObjectBuilder booth @Id/primary key types (simple/composite)
72
- expression .getStateFieldPathExpression ().accept (this );
97
+ }
73
98
}
74
99
75
100
/**
76
- * For Entity e with versionAttr as a version attribute: <code>SELECT VERSION(e) FROM Entity e -> SELECT e.versionAttr FROM Entity e</code>
101
+ * For Entity e with versionAttr as a version attribute:
102
+ * <code>SELECT VERSION(e) FROM Entity e -> SELECT e.versionAttr FROM Entity e</code>
77
103
*
78
104
* @param expression The {@link VersionExpression} to visit
79
105
*/
80
106
@ Override
81
107
public void visit (VersionExpression expression ) {
82
- //Fetch identification variable info
108
+ // Fetch identification variable info
83
109
IdentificationVariable identificationVariable = (IdentificationVariable ) expression .getExpression ();
84
110
String variableText = identificationVariable .getText ();
85
111
String variableName = identificationVariable .getVariableName ();
86
112
87
- //Get version attribute name
113
+ // Get version attribute name
88
114
ClassDescriptor descriptor = this .queryContext .getDeclaration (variableName ).getDescriptor ();
89
- String versionAttributeName = ((VersionLockingPolicy ) descriptor .getOptimisticLockingPolicy ()).getVersionMapping ().getAttributeName ();
90
- StateFieldPathExpression stateFieldPathExpression = new StateFieldPathExpression (expression .getParent (), variableText + "." + versionAttributeName );
115
+ String versionAttributeName = ((VersionLockingPolicy ) descriptor .getOptimisticLockingPolicy ())
116
+ .getVersionMapping ().getAttributeName ();
117
+ StateFieldPathExpression stateFieldPathExpression = new StateFieldPathExpression (expression .getParent (),
118
+ variableText + "." + versionAttributeName );
91
119
expression .setStateFieldPathExpression (stateFieldPathExpression );
92
120
93
- //Continue with created StateFieldPathExpression
121
+ // Continue with created StateFieldPathExpression
94
122
expression .getStateFieldPathExpression ().accept (this );
95
123
}
96
124
97
125
private String getIdAttributeNameByField (List <DatabaseMapping > databaseMappings , DatabaseField field ) {
98
126
for (DatabaseMapping mapping : databaseMappings ) {
99
- if (field .equals (mapping .getField ()) || mapping .isPrimaryKeyMapping ()) {
127
+ if (mapping . getFields (). size () > 1 && ( field .equals (mapping .getField ()) || mapping .isPrimaryKeyMapping () )) {
100
128
return mapping .getAttributeName ();
129
+ } else {
130
+ if ((field .equals (mapping .getField ()) && mapping .isPrimaryKeyMapping ())) {
131
+ return mapping .getAttributeName ();
132
+ }
101
133
}
102
134
}
103
135
return null ;
104
136
}
137
+
138
+ private boolean isEmbeddable (List <DatabaseMapping > databaseMappings ) {
139
+
140
+ for (DatabaseMapping databaseMapping : databaseMappings ) {
141
+ if (databaseMapping .isPrimaryKeyMapping () && databaseMapping .getFields ().size () > 1 ) {
142
+ return true ;
143
+ }
144
+ }
145
+ return false ;
146
+ }
105
147
}
0 commit comments