Creating Hibernate Criteria Query for Tables with Multiple Relations

Sanket Patel :

I've got a table structure something like this...

class A{
    Integer id;

    @OneToMany //
    List<B> b;

}

Class B{

    Integer id;

    @ManyToOne
    A a;

    @OneToMany
    List<C> c;
}

Class C{

    Integer id;

    String someField;

    @ManyToOne
    B b;

}

Now, I want to query all class A entries where C.someField equals to the parameter passed.

Note: There is no direct relation of A with C.

Relations are like A -> List < B > and B -> List < C > and I need to check it in the whole List of C.

How can I create criteria query for such a problem?

Sanket Patel :

I've already changed my whole flow of implementation along with database structure, but if someone wants to implement a similar query, it can be done as below :

Criteria criteria = getCurrentSession().createCriteria(A.class,"a");
criteria.createCriteria("a.b",b);
criteria.createCriteria("b.c",c);
criteria.add(Restrictions.eq("c.someField","queryParameter");

What i was doing before was instead of criteria.createCriteria (Criteia on top of Criteria) i was using criteria.crateAlias (that only creates alias) for the lists.

Hope this help.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=323875&siteId=1