aspose file tools*
The moose likes Object Relational Mapping and the fly likes When Command Object/POJO is not sufficient Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "When Command Object/POJO is not sufficient" Watch "When Command Object/POJO is not sufficient" New topic
Author

When Command Object/POJO is not sufficient

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
I have very common issue. In my application, I am using Hibernate so I have related Java classes(POJOs) also. Now, as long as you're fetching data from one table in a query that's fine but when your query returns data from multiple tables at that time you can't map the result to any of that POJOs.
What is the solution to this scenario? One obvious solution is to make a model(again a Java class) equivalent to the result that is supposed to be return by the query. but if you have tons of queries like that then? Still is this the best solution?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Vishal Pandya wrote:I have very common issue. In my application, I am using Hibernate so I have related Java classes(POJOs) also. Now, as long as you're fetching data from one table in a query that's fine but when your query returns data from multiple tables at that time you can't map the result to any of that POJOs.
What is the solution to this scenario? One obvious solution is to make a model(again a Java class) equivalent to the result that is supposed to be return by the query. but if you have tons of queries like that then? Still is this the best solution?


If you use HQL or Criteria objects you can do that.

If you are using straight SQL, in the SQLQuery object there is a setEntity() method that also will allow it to do it to.

It all depends on if you write the queries correctly in Hibernate. I suggest reading the docs on hibernate.org on running queries.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Mark Spritzler wrote:
If you use HQL or Criteria objects you can do that.

If you are using straight SQL, in the SQLQuery object there is a setEntity() method that also will allow it to do it to.

It all depends on if you write the queries correctly in Hibernate. I suggest reading the docs on hibernate.org on running queries.

Mark

I am afraid but I don't follow you. Do you mean recordset?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

I have found Criteria particularly useful when doing queries that aren't simply POJO-oriented.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eduards Sizovs
Greenhorn

Joined: Apr 25, 2009
Posts: 7
Hi Vishal,

You can use Result Transformers for mapping query result from multiple tables to particular POJO.

Here you can find a tutorial

Hope it will help,

Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When Command Object/POJO is not sufficient