0

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.

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Means here we will get list of type MyClass but how queries columns will set to specific fields. Like 1st column of select will map to which MyClass variable.How to determine ?
Just keep class's field name same as column name, hibernate will handle rest.
Thank You for your prompt response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.