-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: map single pojo with multiple tables
PostPosted: Sun Aug 08, 2010 11:47 am 
Newbie

Joined: Sun May 23, 2010 1:58 pm
Posts: 3
Hi Everyone,
I am facing problem to map single pojo to multiple table when second table has multiple values for single id.
sql query
-------
SELECT p.id,p.name,ph.phno FROM Person p,phone ph
where p.id=ph.id and p.id=1;

here one person id has multiople phone numbers.
expected result is

1 abc 1234
1 abc 5678
1 abc 3578

but iam getting result like below

1 abc 3578
1 abc 3578
1 abc 3578

my hbm file is below
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.vo.Person" table="Person">
<id name="id" type="java.lang.Integer" column="id" >
<generator class="assigned"/>
</id>

<property name="name">
<column name="name" />
</property>
<join table="phone" >
<key column="id" property-ref="id"/>
<property name="phno" column="phno"/>


</join>

</class>




<sql-query name="getPerson" callable="true">
<return alias="person" class="com.vo.Person" >
<return-property name="name" column="name"/>
<return-property name="id" column="id"/>
<return-property name="phno" column="phno" />
</return>
{?=call getPerson() }
</sql-query>

</hibernate-mapping>


please help me how can i solve this problem.......


Top
 Profile  
 
 Post subject: Re: map single pojo with multiple tables
PostPosted: Mon Aug 09, 2010 12:53 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
Hello Ravindra,

Person -> Telephone , it is one to many relation ship. I am assuming you have 2 tables. Persons & Telephones. Now for the given person you need to display the telephone numbers. So it needs to be a collection in Person entity . (<set> can be used.)

Also the hql query needs correction.

person.phno-> phone.id

so from the record phone.id , you need to display phone.phno.

select p.id,p.name,ph.no from person p inner join phone ph
where p.id=ph.pd and p.id=1;

Also 3578 is the phone number of the identifier 1 of phone. So table design is important.

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.