Skip to content

Commit 5e1ffc6

Browse files
committed
alter the logic of left/right-join, make it more intuitive (see the docs)
1 parent 9b7a476 commit 5e1ffc6

File tree

5 files changed

+20
-25
lines changed

5 files changed

+20
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ For spring-boot 3.x:
6363
<dependency>
6464
<groupId>com.github.mhewedy</groupId>
6565
<artifactId>spring-data-jpa-mongodb-expressions</artifactId>
66-
<version>0.1.6</version>
66+
<version>0.1.7</version>
6767
</dependency>
6868

6969
```

docs/include.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
:author: Mohammad Hewedy, The Spring Data JPA MongoDB Expressions Team
2-
:revnumber: 0.1.6
2+
:revnumber: 0.1.7
33
:jsondir: ../src/test/resources
44
:sectlinks: true
55
:source-highlighter: highlight.js

docs/query_specs.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,13 @@ Generated SQL:
308308
+
309309
NOTE: The `distinct` keyword is being used in the queries to eliminate duplicates in case of `one-to-many` and `many-to-many` joins.
310310

311-
TIP: Sometimes the join capabilities is not enough or the not best way for the use case you trying to implement,
311+
TIP: Sometimes the join capabilities is not enough or not the best way for the use case you trying to implement,
312312
in such case you can create a database _view_ and map it to an JPA Entity and then build your search on it.
313313

314314
TIP: The returned properties are the properties of the primary Entity, which means the `projection` is not supported due to limitation in spring-data-jpa addressed in this https://github.com/mhewedy/spring-data-jpa-mongodb-expressions/issues/4[bug,role=external,window=_blank], until it is fixed and if you need to return properties from other entities involved in the join, you need to follow the database _view_ workaround mentioned in the previous tip.
315315

316316
==== Left and right joins
317-
1. Left join uses notation `.<` and right join uses `.>`, exmaple on left join:
317+
1. Left join uses notation `.<` and right join uses `.>`, example on left join (department left join city):
318318
+
319319
[source,json]
320320
----
@@ -324,12 +324,12 @@ Generated SQL:
324324
+
325325
[source,sql]
326326
----
327-
...from employee e left join department d on d.id=e.department_id join city c on c.id=d.city_id where e.first_name=? or c.name=?
327+
...from employee e join department d on d.id = e.department_id left join city c on c.id = d.city_id where e.first_name = ? or c.name = ?
328328
----
329329
+
330330
NOTE: The join notation that define the relation between an object and its association so,
331-
`department.<city.name` means left join between `employee` (current object) and `department`,
332-
where `department.city.<name` means left join between `departement` and `city`
331+
`"<department.city.name"` means left join between `employee` (current object) and `department`,
332+
where `"department.<city.name"` means left join between `departement` and `city`
333333

334334
=== Embedded
335335
1. Using embedded fields:

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>com.github.mhewedy</groupId>
1414
<artifactId>spring-data-jpa-mongodb-expressions</artifactId>
15-
<version>0.1.6</version>
15+
<version>0.1.7</version>
1616
<name>spring-data-jpa-mongodb-expressions</name>
1717
<description>Spring Data JPA Mongodb Expressions</description>
1818

src/main/java/com/github/mhewedy/expressions/ExpressionsPredicateBuilder.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private static List<Predicate> getPredicates(CriteriaQuery<?> query, CriteriaBui
119119
predicate = cb.greaterThan(exprPath, (Comparable) attributeValue);
120120
} else {
121121
throw new IllegalArgumentException("field should be Number or Comparable: " +
122-
singularExpression);
122+
singularExpression);
123123
}
124124
break;
125125
case $gte:
@@ -129,7 +129,7 @@ private static List<Predicate> getPredicates(CriteriaQuery<?> query, CriteriaBui
129129
predicate = cb.greaterThanOrEqualTo(exprPath, (Comparable) attributeValue);
130130
} else {
131131
throw new IllegalArgumentException("field should be Number or Comparable: " +
132-
singularExpression);
132+
singularExpression);
133133
}
134134
break;
135135
case $lt:
@@ -139,7 +139,7 @@ private static List<Predicate> getPredicates(CriteriaQuery<?> query, CriteriaBui
139139
predicate = cb.lessThan(exprPath, (Comparable) attributeValue);
140140
} else {
141141
throw new IllegalArgumentException("field should be Number or Comparable: " +
142-
singularExpression);
142+
singularExpression);
143143
}
144144
break;
145145
case $lte:
@@ -149,7 +149,7 @@ private static List<Predicate> getPredicates(CriteriaQuery<?> query, CriteriaBui
149149
predicate = cb.lessThanOrEqualTo(exprPath, (Comparable) attributeValue);
150150
} else {
151151
throw new IllegalArgumentException("field should be Number or Comparable: " +
152-
singularExpression);
152+
singularExpression);
153153
}
154154
break;
155155
// like
@@ -258,7 +258,7 @@ private static List<Predicate> getPredicates(CriteriaQuery<?> query, CriteriaBui
258258
throw new IllegalArgumentException(
259259
String.format(
260260
"Unable to locate attribute with the given name [%s] on this ManagedType [%s]," +
261-
" Are you sure this ManagedType or one of its ancestors contains such attribute?",
261+
" Are you sure this ManagedType or one of its ancestors contains such attribute?",
262262
field,
263263
type.getJavaType().getName()
264264
)
@@ -280,22 +280,17 @@ private static ManagedType<?> extractSubFieldType(Attribute<?, ?> attribute) {
280280
}
281281

282282
private static String extractField(String field) {
283-
return field.contains(".") ? field.split("\\.")[0] : field;
283+
return field.contains(".") ? field.split("\\.")[0].replaceAll("^[<>]+", "") : field;
284284
}
285285

286286
private static SubField extractSubField(String field) {
287-
//if field is "abc.efg.xyz", then return "efg.xyz", so to support n-level association
287+
//if field is "abc.efg.xyz", then mainField=>"abc" and subField => "efg.xyz", so to support n-level association
288+
String mainField = Arrays.stream(field.split("\\.")).limit(1).collect(Collectors.joining("."));
288289
String subField = Arrays.stream(field.split("\\.")).skip(1).collect(Collectors.joining("."));
289-
JoinType joinType;
290-
if (subField.startsWith("<")) { /// .<
291-
subField = subField.substring(1);
292-
joinType = JoinType.LEFT;
293-
} else if (subField.startsWith(">")) { /// .>
294-
subField = subField.substring(1);
295-
joinType = JoinType.RIGHT;
296-
} else {
297-
joinType = JoinType.INNER;
298-
}
290+
291+
JoinType joinType = mainField.startsWith("<") ? JoinType.LEFT // <abc
292+
: mainField.startsWith(">") ? JoinType.RIGHT // >abc
293+
: JoinType.INNER; //// abc
299294
return new SubField(subField, joinType);
300295
}
301296

0 commit comments

Comments
 (0)