tags:

views:

72

answers:

3

I have following POJO`s: - Company

  • Node (nodeID, company)

  • User (userID, node)

I want to create where clause(via Criteria) which will return to me every user for given company. Something like ...

Criteria criteria = session.createCriteria(User.class)
criteria.add(Restrinctions.eq("node.company", someCompanyObject);

But this is not working, so is it possible to do this with criteria class or should use HQL/SQL?

Thanks in advance!

+2  A: 

You use the id...

Restrinctions.eq("node.company.id", someCompanyObject.id);
dotjoe
It should also work with the object itself.
kgiannakakis
it does? that's cool!
dotjoe
+2  A: 

What you want is possible with Criteria API. Your code has some errors, but perhaps they are just typos. It is hard to tell what the error is without looking at the table and the hibernate configuration. Try something like this:

Criteria criteria = session.createCriteria(User.class);
List users = criteria.add(Restrictions.eq("node.company", someCompanyObject))
            .list();
kgiannakakis
+4  A: 

The hibernate documentation says:

By navigating associations using createCriteria() you can specify constraints upon related entities:

List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "F%") )
    .createCriteria("kittens")
        .add( Restrictions.like("name", "F%") )
    .list();

Transposed to your problem:

Criteria criteria = session.createCriteria(User.class) 
    .createCriteria( "node" )
    .add( Restrinctions.eq( "company", someCompanyObject) );
tangens
Thanks everyone for helping and excuse me for errors, but i write example code manually. I think that this way is the better solution for me!
NikolayGS