mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-27 21:01:15 +08:00
Improve any path serialization
This commit is contained in:
parent
23e68a7d6d
commit
c2d201fd9b
@ -18,11 +18,8 @@ import com.mysema.query.JoinExpression;
|
||||
import com.mysema.query.JoinType;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.support.DetachableQuery;
|
||||
import com.mysema.query.types.CollectionExpression;
|
||||
import com.mysema.query.types.EntityPath;
|
||||
import com.mysema.query.types.MapExpression;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.Predicate;
|
||||
import com.mysema.query.support.Expressions;
|
||||
import com.mysema.query.types.*;
|
||||
import com.mysema.query.types.query.NumberSubQuery;
|
||||
import com.mysema.query.types.template.NumberTemplate;
|
||||
|
||||
@ -70,6 +67,11 @@ public class AbstractJPASubQuery<Q extends AbstractJPASubQuery<Q>> extends Detac
|
||||
return queryMixin.from(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <P> Q from(CollectionExpression<?,P> target, Path<P> alias) {
|
||||
return queryMixin.fullJoin(Expressions.as((Path)target, alias));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public <P> Q fullJoin(CollectionExpression<?,P> target) {
|
||||
|
||||
@ -13,21 +13,15 @@
|
||||
*/
|
||||
package com.mysema.query.jpa;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.mysema.query.support.CollectionAnyVisitor;
|
||||
import com.mysema.query.support.Context;
|
||||
import com.mysema.query.types.EntityPath;
|
||||
import com.mysema.query.types.ExpressionUtils;
|
||||
import com.mysema.query.types.Ops;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.PathMetadataFactory;
|
||||
import com.mysema.query.types.Predicate;
|
||||
import com.mysema.query.types.PredicateOperation;
|
||||
import com.mysema.query.types.ToStringVisitor;
|
||||
import com.mysema.query.types.*;
|
||||
import com.mysema.query.types.path.EntityPathBase;
|
||||
import com.mysema.query.types.path.ListPath;
|
||||
import com.mysema.query.types.path.SimplePath;
|
||||
|
||||
/**
|
||||
* JPACollectionAnyVisitor extends the {@link CollectionAnyVisitor} class with module specific
|
||||
@ -48,8 +42,7 @@ public final class JPACollectionAnyVisitor extends CollectionAnyVisitor {
|
||||
Path<?> child = c.paths.get(i).getMetadata().getParent();
|
||||
EntityPath<Object> replacement = (EntityPath<Object>) c.replacements.get(i);
|
||||
if (c.paths.get(i).getType().isAnnotationPresent(Entity.class)) {
|
||||
query.from(replacement);
|
||||
query.where(PredicateOperation.create(Ops.IN, replacement, child));
|
||||
query.from(new ListPath(c.paths.get(i).getType(), SimplePath.class, child.getMetadata()), replacement);
|
||||
} else {
|
||||
// join via parent
|
||||
Path<?> parent = child.getMetadata().getParent();
|
||||
|
||||
@ -14,6 +14,8 @@
|
||||
package com.mysema.query.jpa;
|
||||
|
||||
import com.mysema.query.Detachable;
|
||||
import com.mysema.query.types.CollectionExpression;
|
||||
import com.mysema.query.types.Path;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
@ -21,4 +23,6 @@ import com.mysema.query.Detachable;
|
||||
*/
|
||||
public interface JPQLSubQuery extends Detachable, JPACommonQuery<JPQLSubQuery>{
|
||||
|
||||
<P> JPQLSubQuery from(CollectionExpression<?,P> target, Path<P> alias);
|
||||
|
||||
}
|
||||
|
||||
@ -13,10 +13,6 @@
|
||||
*/
|
||||
package com.mysema.query.jpa;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.jpa.domain.JobFunction;
|
||||
import com.mysema.query.jpa.domain.QCat;
|
||||
import com.mysema.query.jpa.domain.QDomesticCat;
|
||||
@ -26,6 +22,8 @@ import com.mysema.query.types.ConstantImpl;
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Predicate;
|
||||
import com.mysema.query.types.TemplateExpressionImpl;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
public class JPACollectionAnyVisitorTest {
|
||||
@ -46,8 +44,8 @@ public class JPACollectionAnyVisitorTest {
|
||||
public void Simple_BooleanOperation() {
|
||||
Predicate predicate = cat.kittens.any().name.eq("Ruth123");
|
||||
assertMatches("exists \\(select 1\n" +
|
||||
"from Cat cat_kittens.*\n" +
|
||||
"where cat_kittens.* in elements\\(cat\\.kittens\\) and cat_kittens.*\\.name = \\?1\\)", serialize(predicate));
|
||||
"from cat.kittens as cat_kittens.*\n" +
|
||||
"where cat_kittens.*\\.name = \\?1\\)", serialize(predicate));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -64,18 +62,18 @@ public class JPACollectionAnyVisitorTest {
|
||||
public void Simple_StringOperation() {
|
||||
Predicate predicate = cat.kittens.any().name.substring(1).eq("uth123");
|
||||
assertMatches("exists \\(select 1\n"+
|
||||
"from Cat cat_kittens.*\n" +
|
||||
"where cat_kittens.* in elements\\(cat.kittens\\) and substring\\(cat_kittens.*\\.name,2\\) = \\?1\\)", serialize(predicate));
|
||||
"from cat.kittens as cat_kittens.*\n" +
|
||||
"where substring\\(cat_kittens.*\\.name,2\\) = \\?1\\)", serialize(predicate));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void And_Operation() {
|
||||
Predicate predicate = cat.kittens.any().name.eq("Ruth123").and(cat.kittens.any().bodyWeight.gt(10.0));
|
||||
assertMatches("exists \\(select 1\n"+
|
||||
"from Cat cat_kittens.*\n" +
|
||||
"where cat_kittens.* in elements\\(cat.kittens\\) and cat_kittens.*\\.name = \\?1\\) and exists \\(select 1\n" +
|
||||
"from Cat cat_kittens.*\n" +
|
||||
"where cat_kittens.* in elements\\(cat.kittens\\) and cat_kittens.*\\.bodyWeight > \\?2\\)", serialize(predicate));
|
||||
"from cat.kittens as cat_kittens.*\n" +
|
||||
"where cat_kittens.*\\.name = \\?1\\) and exists \\(select 1\n" +
|
||||
"from cat.kittens as cat_kittens.*\n" +
|
||||
"where cat_kittens.*\\.bodyWeight > \\?2\\)", serialize(predicate));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -83,8 +81,8 @@ public class JPACollectionAnyVisitorTest {
|
||||
Expression<Boolean> templateExpr = TemplateExpressionImpl.create(Boolean.class, "{0} = {1}",
|
||||
cat.kittens.any().name, ConstantImpl.create("Ruth123"));
|
||||
assertMatches("exists \\(select 1\n" +
|
||||
"from Cat cat_kittens.*\n" +
|
||||
"where cat_kittens.* in elements\\(cat\\.kittens\\) and cat_kittens.*\\.name = \\?1\\)", serialize(templateExpr));
|
||||
"from cat.kittens as cat_kittens.*\n" +
|
||||
"where cat_kittens.*\\.name = \\?1\\)", serialize(templateExpr));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -98,8 +96,8 @@ public class JPACollectionAnyVisitorTest {
|
||||
Predicate predicate = anyCat.name.eq("X");
|
||||
|
||||
assertMatches("exists \\(select 1\n" +
|
||||
"from DomesticCat cat_kittens.*\n" +
|
||||
"where cat_kittens.* in elements\\(cat.kittens\\) and cat_kittens.*\\.name = \\?1\\)", serialize(predicate));
|
||||
"from cat.kittens as cat_kittens.*\n" +
|
||||
"where cat_kittens.*\\.name = \\?1\\)", serialize(predicate));
|
||||
}
|
||||
|
||||
private String serialize(Expression<?> expression) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user