user1817081
user1817081

Reputation: 1193

HQL join tables

Does anyone know how to convert a code like this from SQL to HQL in Hibernate?

SELECT 
     a.Column1, 
     a.Column2, 
     b.Column1, 
     b.Column3 
  FROM 
     table1 a, 
     table2 b 
 WHERE 
     a.Column1 = b.Column3 AND
     a.Column2 = 'some user input'

Table1 and Table2 are properly mapped in Hibernate.

Upvotes: 0

Views: 385

Answers (1)

Manuel Quinones
Manuel Quinones

Reputation: 4246

The Criteria query would look something like this where Supplier is TableA and the products is TableB.

session=sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Supplier.class); 
criteria.createCriteria("products","p");
criteria.add( Restrictions.like("name", "some user input");
 List<Supplier> list = criteria.list();
for (Supplier object : list) {
    //Do stuff with supplier if needed
}

products is a one to many relationship on supplier.

Upvotes: 1

Related Questions