Skip to content

Commit 0860d7f

Browse files
committed
DATAJPA-1822 - workaround for EclipseLink
1 parent 372ca69 commit 0860d7f

File tree

3 files changed

+13
-26
lines changed

3 files changed

+13
-26
lines changed

src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import javax.persistence.metamodel.Bindable;
5151
import javax.persistence.metamodel.ManagedType;
5252
import javax.persistence.metamodel.PluralAttribute;
53+
import javax.persistence.metamodel.SingularAttribute;
5354

5455
import org.springframework.core.annotation.AnnotationUtils;
5556
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -688,13 +689,19 @@ private static boolean requiresOuterJoin(From<?, ?> from, PropertyPath property,
688689
Bindable<?> propertyPathModel;
689690
Bindable<?> model = from.getModel();
690691

692+
// required for EclipseLink: we try to avoid using from.get as EclipseLink produces an inner join
693+
// regardless of which join operation is specified next
694+
// see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=413892
695+
// still occurs as of 2.7
696+
ManagedType<?> managedType = null;
691697
if (model instanceof ManagedType) {
692-
693-
/*
694-
* Required to keep support for EclipseLink 2.4.x. TODO: Remove once we drop that (probably Dijkstra M1)
695-
* See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=413892
696-
*/
697-
propertyPathModel = (Bindable<?>) ((ManagedType<?>) model).getAttribute(segment);
698+
managedType = (ManagedType<?>) model;
699+
} else if (model instanceof SingularAttribute
700+
&& ((SingularAttribute<?, ?>) model).getType() instanceof ManagedType) {
701+
managedType = (ManagedType<?>) ((SingularAttribute<?, ?>) model).getType();
702+
}
703+
if (managedType != null) {
704+
propertyPathModel = (Bindable<?>) managedType.getAttribute(segment);
698705
} else {
699706
propertyPathModel = from.get(segment).getModel();
700707
}

src/test/java/org/springframework/data/jpa/repository/query/EclipseLinkQueryUtilsIntegrationTests.java

-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,4 @@ int getNumberOfJoinsAfterCreatingAPath() {
2727
return 1;
2828
}
2929

30-
boolean isSkipOptionalDetection() {
31-
return true;
32-
}
33-
3430
}

src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsIntegrationTests.java

-16
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ void createsJoinForOptionalOneToOneInReverseDirection() {
118118
@Test // DATAJPA-1822
119119
void createsLeftJoinForOptionalToOneWithNestedNonOptional() {
120120

121-
if(isSkipOptionalDetection())
122-
return;
123-
124121
CriteriaBuilder builder = em.getCriteriaBuilder();
125122
CriteriaQuery<Invoice> query = builder.createQuery(Invoice.class);
126123
Root<Invoice> root = query.from(Invoice.class);
@@ -137,9 +134,6 @@ void createsLeftJoinForOptionalToOneWithNestedNonOptional() {
137134
@Test // DATAJPA-1822
138135
void createsLeftJoinForNonOptionalToOneWithNestedOptional() {
139136

140-
if(isSkipOptionalDetection())
141-
return;
142-
143137
CriteriaBuilder builder = em.getCriteriaBuilder();
144138
CriteriaQuery<InvoiceItem> query = builder.createQuery(InvoiceItem.class);
145139
Root<InvoiceItem> root = query.from(InvoiceItem.class);
@@ -159,9 +153,6 @@ void createsLeftJoinForNonOptionalToOneWithNestedOptional() {
159153
@Test // DATAJPA-1822
160154
void reusesLeftJoinForNonOptionalToOneWithNestedOptional() {
161155

162-
if(isSkipOptionalDetection())
163-
return;
164-
165156
CriteriaBuilder builder = em.getCriteriaBuilder();
166157
CriteriaQuery<InvoiceItem> query = builder.createQuery(InvoiceItem.class);
167158
Root<InvoiceItem> root = query.from(InvoiceItem.class);
@@ -186,9 +177,6 @@ void reusesLeftJoinForNonOptionalToOneWithNestedOptional() {
186177
@Test // DATAJPA-1822
187178
void reusesInnerJoinForNonOptionalToOneWithNestedOptional() {
188179

189-
if(isSkipOptionalDetection())
190-
return;
191-
192180
CriteriaBuilder builder = em.getCriteriaBuilder();
193181
CriteriaQuery<InvoiceItem> query = builder.createQuery(InvoiceItem.class);
194182
Root<InvoiceItem> root = query.from(InvoiceItem.class);
@@ -344,10 +332,6 @@ int getNumberOfJoinsAfterCreatingAPath() {
344332
return 0;
345333
}
346334

347-
boolean isSkipOptionalDetection() {
348-
return false;
349-
}
350-
351335
private Set<Join<?, ?>> getNonInnerJoins(Root<?> root) {
352336

353337
return getNonInnerJoins((From<?, ?>) root);

0 commit comments

Comments
 (0)