@@ -205,26 +205,56 @@ Path aliases
205
205
+++++++++++++++
206
206
207
207
Imagine that we have the scenario of many-to-many relation through a separate table.
208
- For the previous example, let's add a table **'artistAlbums ' ** with three columns, **'id' **, **'artistId' ** and **'albumId' **
208
+ For the previous example, let's add a table **'albumartist ' ** with three columns, **'id' **, **'artistId' ** and **'albumId' **
209
209
Table **'albums' ** does not contain the 'artistID' column any more, but both forward and reverse relations are still
210
210
called **'albumArtists' ** and **'artistAlbums' **
211
211
212
- If we need to select artists that released albums both in 1980 and 1990,
213
- joining expressions neither with exp.andExp nor exp.orExp would give us appropriate queries.
212
+ Let us imagine that we need to select the artists that released their albums both in 1973 and 1997.
214
213
215
- exp.andExp() would return no results,
214
+ Joining expressions neither with exp.andExp nor exp.orExp would give us appropriate queries.
216
215
217
- exp.orExp() would return all artists that released albums either in 1980 or 1990.
216
+ ** exp.andExp() ** would produce a query::
218
217
219
- For such a case, Cayenne provides aliases mechanism::
218
+ SELECT DISTINCT t0.name, t0.id
219
+ FROM artist t0
220
+ JOIN albumartist t1 ON (t0.id = t1.idArtist)
221
+ JOIN album t2 ON (t1.idAlbum = t2.id)
222
+ WHERE (t2.year = 1973) AND (t2.year = 1997)
220
223
221
- Expression e1 = ExpressionFactory.match("artistAlbumsAlias1.year", 1980);
222
- Expression e2 = ExpressionFactory.match("artistAlbumsAlias2.year", 1990);
224
+ that obviously never returns the data since the WHERE sub-conditions are mutually exclusive.
225
+
226
+ **exp.orExp() ** would produce a query::
227
+
228
+ SELECT DISTINCT t0.name, t0.id
229
+ FROM artist t0
230
+ JOIN albumartist t1 ON (t0.id = t1.idArtist)
231
+ JOIN album t2 ON (t1.idAlbum = t2.id)
232
+ WHERE (t2.year = 1973) OR (t2.year = 1997)
233
+
234
+ that returns all the artists that released albums either in 1973 or 1997.
235
+
236
+ To resolve the problem, Apache Cayenne provides the aliases mechanism::
237
+
238
+ Expression e1 = ExpressionFactory.match("artistAlbumsAlias1.year", 1997);
239
+ Expression e2 = ExpressionFactory.match("artistAlbumsAlias2.year", 1973);
223
240
Expression e = e1.andExp(e2);
224
- q = new SelectQuery(Artists.class, e);
241
+ SelectQuery q = new SelectQuery(Artists.class, e);
225
242
q.aliasPathSplits("artistAlbums", "artistAlbumsAlias1", "artistAlbumsAlias2");
226
243
227
244
That last command tells the select query how to interpret the alias.
228
- Because the aliases are different, the SQL generated will have two completely separate set of joins.
229
- This is called a "split path".
245
+ Because the aliases are different, the SQL generated will have two completely separate set of joins::
246
+
247
+ SELECT DISTINCT t0.name, t0.id
248
+ FROM artist t0
249
+ JOIN albumartist t1 ON (t0.id = t1.idArtist)
250
+ JOIN album t2 ON (t1.idAlbum = t2.id)
251
+ JOIN albumartist t3 ON (t0.id = t3.idArtist)
252
+ JOIN album t4 ON (t3.idAlbum = t4.id)
253
+ WHERE (t2.year = 1997) AND (t4.year = 1973)
254
+
255
+ This is called "split path" in the Apache Cayenne terms.
256
+
257
+ If applied to the case of VAMDC databases,
258
+ this approach may be used for handling the cases where the data is linked
259
+ through a separate join table, such as articles by author or year, or reactants in chemical reaction databases.
230
260
0 commit comments