VJS
VJS

Reputation: 2941

Hibernate : getting multiple table columns results from multiple tables

I am using hibernate 4 and Spring 3.

I have 5 tables and each table is mapped with 1 entity class.Now if I have to select columns from 1 table than i will do below :

String hql = "from Employee E";
Query query = session.createQuery(hql);
List results = query.list();

This value in this result will be of type EmployeeEntity.

Or I can use Criteria as well.

Now my requirnment is that I have to get the result from all 5 tables.1-2 columns from each table.

Earlier it was one 1 table so i was getting one entity , now I am getting results from 5 tables so how to map it in entity.

List results1 = query.list(); // considering select and getting 6 columns in results with diffenent tables.

Now how to iterate this result1.

I hope you got my question.

Upvotes: 0

Views: 1616

Answers (1)

Darshan Lila
Darshan Lila

Reputation: 5868

You can use Result Set Transformer of Query:

Say you have 4 columns from different tables like tab1col,tab2col,tab3col,tab4col.

Create a 'POJO' as follows

class MyClass
{
 private Integer tablcol;
 private Integer tab2col;
 private Integer tab3col;
 private Integer tab4col;
  //  getter and setters
}

Following way you can transform you result set:

List<MyClass> myClassList=query.setResultTransformer(Transformers.aliasToBean(MyClass.class)).list();

Note: query should contain a result set(something like cursor in oracle).

Upvotes: 1

Related Questions