Attach two tables with hibernation criteria

Take the following examples of objects:

Entity Child { long id; String name; long parentKey; } Entity Parent { long id; String desc; } 

Is there a way to request sleep mode:

 select * from Child c, Parent p where c.parentKey = p.id and c.name = "whatever" and p.desc = "whatever" 

Our main concern is how to join the criteria for two objects that are associated only with a long key .

Suppose you cannot put a link to a parent directly in our Child.

+4
source share
1 answer

Well, not sure which version of Hibernate you mean, but something similar is possible in JPA 2.0:

 String query = "SELECT c FROM Child c, Parent p WHERE c.parentId = p.id"; List<Child> children = em.createQuery(query, Child.class).getResultList(); 

This way, you explicitly execute the JOIN based on your user condition, rather than letting the JPA manage it, although this is completely legal.

+1
source

Source: https://habr.com/ru/post/1384393/


All Articles