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

Problem in hibernate join result

 
vinoth subramaniam
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am trying to fetch some information from two tables in the database. I used the inner join, and i can able to get the data from the database. The only problem is the resulted data is not in the way i expected. Please see the code here.

Table Striucture



And the CONTACT and person class have one to many relationship according to the business logic. I.e for one contact there may be one or more streets (just for some testing purpouse i wrote like that..)

Here is the CONTACT class



And here is the person class



And here is the hbm.xml file for the CONTACT class

And here is the hbm.xml file for person class

And i am using the following classes to get the data



The using sessionfactory class


The resulted list have the three CONTACT object, in which each one have the persons list object. That particular persons list have no of person object.

Resulted List == [CONTACT OBJ, ....]
|
|
[ID, FirstName, Persons]
|
|
[Person OBJ, person OBJ]
|
|
[Street, personID]

The part of the result in the console as follows



But i need the result in the following way

Size of the list 3

9
Antony
North Street

2
selvaraj
North Street

2
Selvaraj
Kallur Street

Because i want to display this data in a table. Where i need to change, Is there any modification needed in the Mapping or in the hql query or in the classes declaration. Please help me to solve this issue.

Thanks in advance ..!
 
vinoth subramaniam
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please anyone respond to this question please...!
 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vinoth subramaniam:
Can you please anyone respond to this question please...!


Hey Wait ! Wait !!

Please look at the following code you can find your solution.



I think it will solve your problem

Thanking You
- Kuldeep
[ November 11, 2008: Message edited by: Kuldeep Yadav ]
 
vinoth subramaniam
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
QUOTE]
Hey Wait ! Wait !!

Please look at the following code you can find your solution.


code:
--------------------------------------------------------------------------------

List list = session.createQuery( "from CONTACT C") .setFetchMode( "persons ", FetchMode.SELECT ) .list(); Iterator iter = list.iterator(); while (iter.hasNext()) { CONTACT con = (CONTACT) iter.next(); List personlist=con.getPersons(); for (Iterator iterator = personlist.iterator(); iterator.hasNext() { Person personobj= (Person) iterator.next(); System.out.println(con.getID()); System.out.println(con.getFIRSTNAME()); System.out.println(personobj.getStreet()); } }

--------------------------------------------------------------------------------



I think it will solve your problem

Thanking You
- Kuldeep


Sorry boss, can you please point out the error... sorry i cant catch that one...
 
vinoth subramaniam
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I resolved my problem, i used criteria API instead of the HQL. The problem is with the result set. And i resloved it by the following way

list = session.createCriteria(PLAN.class).setFetchMode("suspenses",FetchMode.JOIN). setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY).list();

Thanks for your time... :
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic