mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-19 21:00:53 +08:00
Improve join creation #666
This commit is contained in:
parent
6e6980c637
commit
dc0b362b8c
@ -0,0 +1,14 @@
|
||||
package com.mysema.query.domain.p9;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class Article {
|
||||
|
||||
String name;
|
||||
|
||||
Content content;
|
||||
|
||||
Person author;
|
||||
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package com.mysema.query.domain.p9;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Content {
|
||||
|
||||
Article article;
|
||||
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.mysema.query.domain.p9;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
String firstName, lastName;
|
||||
}
|
||||
@ -16,6 +16,8 @@ package com.mysema.query.jpa;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.mysema.query.JoinFlag;
|
||||
@ -34,6 +36,7 @@ import com.mysema.query.types.PathImpl;
|
||||
import com.mysema.query.types.PathMetadata;
|
||||
import com.mysema.query.types.PathType;
|
||||
import com.mysema.query.types.Predicate;
|
||||
import com.mysema.query.types.path.CollectionPathBase;
|
||||
|
||||
/**
|
||||
* JPAQueryMixin extends {@link QueryMixin} to support JPQL join construction
|
||||
@ -78,6 +81,15 @@ public class JPAQueryMixin<T> extends QueryMixin<T> {
|
||||
return super.createAlias(expr, alias);
|
||||
}
|
||||
|
||||
private boolean isEntityPath(Path<?> path) {
|
||||
if (path instanceof CollectionPathBase) {
|
||||
return isEntityPath((Path<?>) ((CollectionPathBase)path).any());
|
||||
} else {
|
||||
return path instanceof EntityPath
|
||||
|| path.getType().isAnnotationPresent(Entity.class);
|
||||
}
|
||||
}
|
||||
|
||||
private <T> Class<T> getElementTypeOrType(Path<T> path) {
|
||||
if (path instanceof CollectionExpression) {
|
||||
return ((CollectionExpression)path).getParameter(0);
|
||||
@ -94,6 +106,14 @@ public class JPAQueryMixin<T> extends QueryMixin<T> {
|
||||
return (Path<T>) aliases.get(path);
|
||||
} else if (metadata.getPathType() == PathType.COLLECTION_ANY) {
|
||||
return (Path<T>) shorten(metadata.getParent());
|
||||
} else if (!isEntityPath(path)) {
|
||||
Path<?> parent = shorten(metadata.getParent());
|
||||
if (parent.equals(metadata.getParent())) {
|
||||
return path;
|
||||
} else {
|
||||
return new PathImpl<T>(path.getType(),
|
||||
new PathMetadata(parent, metadata.getElement(), metadata.getPathType()));
|
||||
}
|
||||
} else if (metadata.getParent().getMetadata().isRoot()) {
|
||||
Class<T> type = getElementTypeOrType(path);
|
||||
Path<T> newPath = new PathImpl<T>(type, path.toString().replace('.', '_'));
|
||||
|
||||
@ -9,9 +9,13 @@ import org.junit.Test;
|
||||
import com.mysema.query.JoinExpression;
|
||||
import com.mysema.query.JoinType;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.domain.p9.QArticle;
|
||||
import com.mysema.query.jpa.domain.QCat;
|
||||
import com.mysema.query.jpa.domain4.QBookMark;
|
||||
import com.mysema.query.jpa.domain4.QBookVersion;
|
||||
import com.mysema.query.types.PathMetadataFactory;
|
||||
import com.mysema.query.types.Predicate;
|
||||
import com.mysema.query.types.path.StringPath;
|
||||
|
||||
public class JPAQueryMixinTest {
|
||||
|
||||
@ -108,4 +112,48 @@ public class JPAQueryMixinTest {
|
||||
assertEquals(Arrays.asList(cat_kittens.name.asc()),
|
||||
md.getOrderBy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void OrderBy_Embeddable() {
|
||||
QBookVersion bookVersion = QBookVersion.bookVersion;
|
||||
mixin.from(bookVersion);
|
||||
mixin.orderBy(bookVersion.definition.name.asc());
|
||||
|
||||
QueryMetadata md = mixin.getMetadata();
|
||||
assertEquals(Arrays.asList(new JoinExpression(JoinType.DEFAULT, bookVersion)),
|
||||
md.getJoins());
|
||||
assertEquals(Arrays.asList(bookVersion.definition.name.asc()),
|
||||
md.getOrderBy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void OrderBy_Embeddable2() {
|
||||
QArticle article = QArticle.article;
|
||||
QArticle article_content_article = new QArticle("article_content_article");
|
||||
mixin.from(article);
|
||||
mixin.orderBy(article.content.article.name.asc());
|
||||
|
||||
QueryMetadata md = mixin.getMetadata();
|
||||
assertEquals(Arrays.asList(
|
||||
new JoinExpression(JoinType.DEFAULT, article),
|
||||
new JoinExpression(JoinType.LEFTJOIN, article.content.article.as(article_content_article))),
|
||||
md.getJoins());
|
||||
assertEquals(Arrays.asList(article_content_article.name.asc()),
|
||||
md.getOrderBy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void OrderBy_Embeddable_Colllection() {
|
||||
QBookVersion bookVersion = QBookVersion.bookVersion;
|
||||
QBookMark bookMark = new QBookMark("bookVersion_definition_bookMarks");
|
||||
mixin.from(bookVersion);
|
||||
mixin.orderBy(bookVersion.definition.bookMarks.any().comment.asc());
|
||||
|
||||
QueryMetadata md = mixin.getMetadata();
|
||||
assertEquals(Arrays.asList(new JoinExpression(JoinType.DEFAULT, bookVersion)),
|
||||
md.getJoins());
|
||||
assertEquals(Arrays.asList(new StringPath(bookVersion.definition.bookMarks, "comment").asc()),
|
||||
md.getOrderBy());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user