开发者

QueryDSL: querying relations and properties

开发者 https://www.devze.com 2023-04-04 05:53 出处:网络
I\'m using QueryDSL with JPA. I want to query some properties of an entity, it\'s like this: QPost post = QPost.post;

I'm using QueryDSL with JPA.

I want to query some properties of an entity, it's like this:

QPost post = QPost.post;
JPAQuery q = new JPAQuery(em);
List<Object[]> rows = q.from(post).where(...).list(post.id, post.name);

It works fine.

If i want to query a relation property, e.g. comments of a post:

List<Set<Comment>> rows = q.from(post).where(...).list(post.c开发者_运维百科omments);

It's also fine.

But when I want to query relation and simple properties together, e.g.

List<Object[]> rows = q.from(post).where(...).list(post.id, post.name, post.comments);

Then something went wrong, generiting a bad SQL syntax.

Then I realized that it's not possible to query them together in one SQL statement.

Is it possible that QueryDSL would somehow deal with relations and generate additional queries (just like what hibernate does with lazy relations), and load the results in?

Or should I just query twice, and then merge both result lists?

P.S. what i actually want is each post with its comments' ids. So a function to concat each post's comment ids is better, is this kind of expressin possible?

q.list(post.id, post.name, post.comments.all().id.join())

and generate a subquery sql like (select group_concat(c.id) from comments as c inner join post where c.id = post.id)


Querydsl JPA is restricted to the expressivity of JPQL, so what you are asking for is not possible with Querydsl JPA. You can though try to express it with Querydsl SQL. It should be possible. Also as you don't project entities, but literals and collections it might work just fine.

Alternatively you can load the Posts with only the Comment ids loaded and then project the id, name and comment ids to something else. This should work when accessors are annotated.


The simplest thing would be to query for Posts and use fetchJoin for comments, but I'm assuming that's too slow for you use case.

I think you ought to simply project required properties of posts and comments and group the results by hand (if required). E.g.

QPost post=...;
QComment comment=..;

List<Tuple> rows = q.from(post)
// Or leftJoin if you want also posts without comments
.innerJoin(comment).on(comment.postId.eq(post.id))
.orderBy(post.id) // Could be used to optimize grouping
.list(new QTuple(post.id, post.name, comment.id));

Map<Long, PostWithComments> results=...;
for (Tuple row : rows) {
  PostWithComments res = results.get(row.get(post.id));
  if (res == null) { 
    res = new PostWithComments(row.get(post.id), row.get(post.name));
    results.put(res.getPostId(), res);
  }
  res.addCommentId(row.get(comment.id));
}

NOTE: You cannot use limit nor offset with this kind of queries.

As an alternative, it might be possible to tune your mappings so that 1) Comments are always lazy proxies so that (with property access) Comment.getId() is possible without initializing the actual object and 2) using batch fetch* on Post.comments to optimize collection fetching. This way you could just query for Posts and then access id's of their comments with little performance hit. In most cases you shouldn't even need those lazy proxies unless your Comment is very fat. That kind of code would certainly look nicer without low level row handling and you could also use limit and offset in your queries. Just keep an eye on your query log to make sure everything works as intended.

*) Batch fetching isn't directly supported by JPA, but Hibernate supports it through mapping and Eclipselink through query hints.

Maybe some day Querydsl will support this kind of results grouping post processing out-of-box...

0

精彩评论

暂无评论...
验证码 换一张
取 消