querydsl joining ElementCollection

1,603 views
Skip to first unread message

Sebastian Dietrich

unread,
Nov 25, 2014, 2:38:29 AM11/25/14
to quer...@googlegroups.com
Hi!
I can't figure out on how to correctly join an @ElementCollection annotated list in JPA.
Assuming that I have the following classes: A, B, C with:

public class A {
   @Id private Integer id;
   @ManyToOne private B b;
   ...

public class B {
   @Id private Integer id;

   @ElementCollection
   @CollectionTable(joinColumns = @JoinColumn(name=B_ID"))
   private List<C> cs;

   @Embeddable
   public static class C {
      private String name;
      private boolean whatever;
      ...
   ...

I'd like to get all As that reference Bs that have Cs with names matching a list of given names and whatever = true.
Joining A with B works fine, but joining B with C (or sub.selecting Bs with given Cs) gives me troubles.

Any suggestions would be appreciated...

Thanks,
Sebastian

timowest

unread,
Nov 25, 2014, 1:25:56 PM11/25/14
to quer...@googlegroups.com
Hi.

Could you be more explicit on what kind of trouble you get?

Br,
Timo

Sebastian Dietrich

unread,
Nov 25, 2014, 6:30:48 PM11/25/14
to quer...@googlegroups.com
Hi!

It's basically due to missing knowledge on my side. I just don't know how to join elementcollections in querydsl:

I've tried the following:
QB b = QB.b;
QB_C c = QB_C c;
return from(b).innerJoin(b.c, c).list(c);
but that gives me a SQLGrammarException: could not prepare statement: user lacks privilege or object not found: c.id
afaik this is due to the fact, that I list c and not b, but rephrasing it to
return from(b, c).innerJoin(b.c, c).list(c);
does not compile since c is no EntityPath but a BeanPath

So how can I query for specific elements in an element-collection?

timowest

unread,
Nov 26, 2014, 12:51:47 PM11/26/14
to quer...@googlegroups.com
Hi.

Joins to element collections are allowed, but in the projection you can only use entities and values. 

You can for example project the properties of C and construct a new C instance via Expressions.bean

from(b).innerJoin(b.cs, c).list(Expressions.bean(C.class, c.name. c.whatever));

Br,
Timo

Sebastian Dietrich

unread,
Dec 3, 2014, 5:35:42 PM12/3/14
to quer...@googlegroups.com
Thanks, that helped - though there is no Expressions.bean method I've found that it could be done via
from(b).innerJoin(b.cs, c).list(Expressions.list(C.class, c.name. c.whatever));

Ruben Dijkstra

unread,
Dec 3, 2014, 5:39:09 PM12/3/14
to quer...@googlegroups.com
Hello Sebastian,

Indeed there is no Expressions.bean, but it should be:
Projections.bean(C.class, c.name, c.whatever)

Hope that helps.

Br,

Ruben

Timo Westkämper

unread,
Dec 4, 2014, 1:14:09 AM12/4/14
to Querydsl on behalf of Dingo

Hi.

Sorry. Projections.bean of course.

Timo

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages