Home JPA find grandparent entity given only child unique key
Reply: 2

JPA find grandparent entity given only child unique key

Dee Published in 2018-02-12 19:11:59Z

I am using JPA & MySQL.

If I have a bunch of entities that are related as follows:

  • Grandparent_entity has many Parent_entity (OneToMany). Persisted with a join table.
  • Parent_entity has one Child_entity (OneToOne). Persisted with child_uuid in parent table.

If I am given only the child unique key, how do I get the grandparent entity?

My idea is a query with 3 joins - child to parent, parent to join table, join table to grandparent. Is this the best way?

vladwoguer Reply to 2018-02-12 19:44:16Z

Using hql:

FROM grand_parent gp, parent p WHERE p.child.id = :id AND p in (gp.parents)
Dee Reply to 2018-02-13 12:06:55Z

This worked:

select g from Grandparent_entity g join fetch g.Parent_entity p join fetch p.Child_entity c WHERE (c.uniqueKey = :uniueKey)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317329 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO