user1007483
user1007483

Reputation: 1

Hibernate Criteria Collection of Objects

I have a class with collection of Objects. Ex:

class XmlRequest{
            Long id;
             List<PersonNmDTO> persons;
     }

class PersonNmDTO{
    String firstName;
    String lastName;
}

I want to generate SQL as follows: select * from table where (firstname='xxxx' and lastname='yyyy') or (firstname='aaaa' and lastname='bbbb') or (firstname='pppp' and lastname='qqqq') .......

How to use Hibernate Criteria to generate this kins of SQL?

Upvotes: 0

Views: 551

Answers (1)

JB Nizet
JB Nizet

Reputation: 691725

This is really basic usage. Have you read the documentation?

Criteria c = session.createCriteria(Person.class, "person");
Disjunction or = Restrictions.disjunction();
or.add(Restrictions.and(Restrictions.eq("person.firstName", "xxxx"),
                        Restrictions.eq("person.lastName", "yyyy")));
or.add(Restrictions.and(Restrictions.eq("person.firstName", "aaaa"),
                        Restrictions.eq("person.lastName", "bbbb")));

// ...
criteria.add(or);
List<Person> result = (List<Person>) c.list();

Note that DTO is supposed to mean "data transfer object", which is a mean to transfer data without transferring persistent entities. So naming your persistent entity PersonNmDTO is really questionable.

Upvotes: 1

Related Questions