This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When Command Object/POJO is not sufficient

 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found Criteria particularly useful when doing queries that aren't simply POJO-oriented.
 
Eduards Sizovs
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic