Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA [Collections]

 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I've got a little problem with collections in JPA.

To retrieve a data in a simple field it's ok, exemple


But, if I have un Set<type>, i don't know how to retrieve in my query



I tried this: Query = "SELECT y FROM XXX y WHERE y.errors.code = :code" but it doesn't work, I've got a message like this: "java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager ...invalid navigation expression [xxx.errors.code], cannot navigate collection valued association field [errors]"

Someone can help me?

Thanks in advance !
 
Alex Armenteros
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because a Set or another java collection has not any relation to a column type on the DB.

A set in a mapping class usually refers to a relation between tables, so here you should use the @OneToMany, @ManyToMany, etc associations
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, i forgot in my example to put the annotation So, it's not the problem :p
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use a join to query across a collection.



See,
http://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sutherland wrote:You need to use a join to query across a collection.



See,
http://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF


Perfect! Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic