-->
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.  [ 4 posts ] 
Author Message
 Post subject: Result from multiple tables
PostPosted: Mon Apr 17, 2006 5:50 am 
Newbie

Joined: Mon Apr 17, 2006 5:01 am
Posts: 4
Hi,

I'm working on getting the data from two tables of DB2 database. (Contact and Address)

If I code as following in the DAO.

session.get(Contact.class,"XYZ")

Where "Contact" is the value object, containing the getter setter methods of the values of both the tables. In this class, I've defined a "Set" to get the values from Address table.

session is an object of org.hibernate.Session.

Now, after running the code using above statement, i'm getting the result from only one table (i.e. from Contact table), i'm not getting the records from Address table.

When i debug the trace, internally it's creating two collection objects instead of one. And returning one collection object which has only the values of Contact table.

If I code the DAO using Named Query

session.getNamedQuery("contactDetails").setString("param", "XYZ");

I'm getting the result from 2 tables (from Contact & Address) (what i expected).


In contact.hbm.xml file, i wrote the code for one-to-many association which returns the set of addresses.

Internally, while debugging, i could see that, the queries are generated correctly in both the cases, and they are also been executed properly, but the difference is only while returning.

Using 1st method, the return result is 2 collections. (the values in 1 collection are from only 1 table, and the other new collection for other table)

Using 2nd method (named query), the return result is 1 collection.

Could you please let me know, what changes do i need to make the code working (internally it should return 1 collection for the 1st method) using session.get() (without named query) ???

Thanx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 4:40 am 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
Could you please quote the entire mapping file for both classes, as it's impossible to tell with this information what Hibernate is trying to load.

Thanks!

Christian Ashby
Spiralinks, Inc.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 6:02 am 
Newbie

Joined: Mon Apr 17, 2006 5:01 am
Posts: 4
Hi,
Here are the contents of both mapping files...

contact.hbm.xml

<?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="Contacts" table="SPURCONTS">

<composite-id unsaved-value="any">

<key-property name="vndrId" column="vndr_id" type="string"
length="9" />
<key-property name="vndrName" column="vndr_name"
type="string" length="40" />
<key-property name="addrFunct" column="addr_funct"
type="string" length="3" />
<key-property name="seqKey" column="seq_key" type="string"
length="3" />
</composite-id>

<property name="contactPrsnName" not-null="false"
column="contact_prsn_name" type="string" length="20" />
<property name="contactPrsnPhn" not-null="false"
column="contact_prsn_phn" type="string" length="20" />

<set name="addresses" inverse="false" cascade="all"
lazy="false">
<key>
<column name="vndr_id" />
</key>
<one-to-many class="Addresses" />
</set>
</class>
</hibernate-mapping>


address.hbm.xml
<?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="Addresses" table="saddrs">

<composite-id unsaved-value="any">
<key-property name="vndrId" column="vndr_id" type="string"
length="9" />
<key-property name="vndrName" column="vndr_name"
type="string" length="40" />
<key-property name="addrOwner" column="addr_owner"
type="string" length="3" />
</composite-id>


<property name="addrLn1" not-null="false" column="addr_ln_1"
type="string" length="40" />
<property name="addrLn2" not-null="false" column="addr_ln_2"
type="string" length="31" />
<property name="city" not-null="false" column="city"
type="string" length="20" />
<property name="stCd" not-null="false" column="st_cd"
type="string" length="2" />
<property name="zipCd" not-null="false" column="zip_cd"
type="string" length="9" />
<property name="ctryCd" not-null="false" column="ctry_cd"
type="string" length="3" />

</class>
</hibernate-mapping>


Thanx
Phanny


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 3:47 am 
Beginner
Beginner

Joined: Sun Jul 10, 2005 12:11 pm
Posts: 32
Location: Hertfordshire, England
The composite-id of the Address class is not the same as the key column specified in the Contact class - therefore the set is not going to contain any results.

Are you sure that the composite-id is necessary? It looks to me like the vndr_id is the primary key of the address table, so there's no need to specify the composite ID.

HTH, and sorry for the delay,

Christian.

_________________
--
Christian Ashby
Spiralinks, Inc.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.