Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

JPA Criteria API - unintentional CROSS JOIN  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I'm suffering which a crazy behavior of the JPA criteria API (with hibernate ORM 5.2.10.Final).

MyEntity looks like this:


That's it basically... Table creation and crud operations all work fine...

Now, I want to find MyEntity by EntityA and EntityB doing this with the criteria API:


It results in the statement below... with a CROSS JOIN on itself.. and I have no Idea where it came from...


As you see it creates 2 references to the MyEntityTable... alias0_ and alias1_
The joins are done with alias0, but the selection is done with alias1_
And bot beferenced were connected in a cross join so 1 get all entities in the table every time...

using root.get(..) instead of root.join(..) changes the sql, but the cross joins is allways there...

Can anybody help?

kind regards

Lars
 
Sheriff
Posts: 21466
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just use cq.select(root) instead of cq.select(getRoot(cq))? I think this is the cause of your problem. If you look closely at the generated query, it selects from alias1_ - the cross joined table.
 
Rob Spoor
Sheriff
Posts: 21466
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Lars Drie├čnack
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Why don't you just use cq.select(root) instead of cq.select(getRoot(cq))? I think this is the cause of your problem. If you look closely at the generated query, it selects from alias1_ - the cross joined table.



Yes indeed... yesterday evening a colleague was looking at the code an said the same... Problem is solved

Could be an improvment to JPA to dertermine equal roots to avoid this..

kind regards
Lars
 
Rob Spoor
Sheriff
Posts: 21466
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what if you actually want the cross join? I think that JPA shouldn't make any changes, except maybe log a warning (well, the implementations would). The warning would have helped a lot already.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!