Search code examples
javajpacriteria

Querying specific object from a jpa datamodel


I have a question about querying with an orm mapper like hibernate. Suppose you have a database model like this

class A {
    int i;
    B b;
}

class B {
    int j;
}

If I want to save an object the hibernate engine can add all class instances at once by cascading queries. This will also work if I fetch an object from the database by value from class A.

But what if i want to have all entries of Class A with holds the objects of B with value j = 10?

In SQL i would have something like this:

SELECT * FROM A a, B b WHERE a.ref = b.ref AND b.j = 10;

But with an ORM the ref is modeled with the object references. How am i supposed to create a query like this? First of all: Do I have to write a query? And secondly: How will hibernate resolve these references a.ref = b.ref


Solution

  • You can navigate over the references in JPQL:

    SELECT a FROM A a WHERE a.b.j = 10;