-->
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: Mapping one-to-many problem
PostPosted: Thu Aug 28, 2008 4:44 am 
Newbie

Joined: Fri Aug 15, 2008 3:33 am
Posts: 17
Hi,

I have 2 tables: customer and house. One customer can have multiple houses. For this reason in the table house I inserted a foreign key customer_id which maps to customer.id field.

In my hibernate mapping file I have:
for Customer
Code:
<class name="Customer" table="customer">
  <id column="id" name="id" type="int" unsaved-value="0">
   <generator class="increment"/>
  </id>
  <property column="name" generated="never" lazy="false"
   name="name" type="java.lang.String"/>
  ...
  <list name="houses" inverse="true" lazy="false" cascade="all">
   <key column="customer_id" not-null="true" />
   <index column="id" />
   <one-to-many class="House"/>
  </list>
</class>


for House:
Code:
<class name="House" table="house">
  <id column="id" name="id" type="java.lang.Integer" unsaved-value="0">
   <generator class="increment"/>
  </id>
  <property column="street" generated="never" lazy="false" name="street" type="java.lang.String"/>
  ...
  <many-to-one class="Customer" fetch="select" name="customer" lazy="false">
   <column name="customer_id" not-null="true"/>
  </many-to-one>
</class>


In the Customer POJO class I have inserted a property:
Code:
private List<House> houses = new ArrayList <House>(0);


The problem is that my DAO object returns a houses list with many empty elements.

For example the house id=7 is the only house for a customer. DAO returns the list with 7 elements, the first 6 are empty and the last one has the right House object.

Maybe someone had already a similar problem and can give me a tip.

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 4:52 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This happens because you use the house "id" column as the list index (<index column="id" />). So the house with id=7 will appear in the list at index 7. When working with lists it is usually better to have a designated column for the list index. Eg. <index column="list_index" />.

Another option that can be used if you don't care about the order of the houses is to use a <set> mapping instead of a <list>. A set doesn't need an index column.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 8:56 am 
Newbie

Joined: Fri Aug 15, 2008 3:33 am
Posts: 17
I have made some test also with Set, but even when the page is reloaded the order is changed.

Do you know a easy mode to order the object from a SET, after an id or something?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 9:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
http://www.hibernate.org/hib_docs/v3/re ... ons-sorted


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.