Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

When Command Object/POJO is not sufficient

 
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?
 
ranger
Posts: 17346
11
Mac IntelliJ IDE 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?
 
Sheriff
Posts: 67399
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
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,
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic