This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Problem in hibernate join result

 
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...!
 
Ranch Hand
Posts: 162
Hibernate Spring Java
  • 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... :
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic