-->
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.  [ 6 posts ] 
Author Message
 Post subject: Hibernate Bug - Composite-element read problem
PostPosted: Tue Nov 23, 2004 5:15 pm 
Newbie

Joined: Tue Mar 30, 2004 9:58 am
Posts: 8
Location: Chicago, IL
Hello again,
I have the following mappings for a composite-element as a list. I have an Order object and a list of lineitem objects.

<class name="Order" table="orders">
<id name="orderId" column="orderid" type="int">
<generator class="increment"/>
</id>

<property name="username" column="userid" type="java.lang.String" not-null="true" />
<property name="orderDate" column="orderdate" type="java.util.Date" not-null="true" />
<property name="shipAddress1" column="shipaddr1" type="java.lang.String" not-null="true" />


<list name="lineItems" table="lineitem" lazy="false">
<key column="orderid"/>
<index column="linenum"/>
<composite-element class="LineItem">
<property name="quantity" column="quantity"/>
<property name="unitPrice" column="unitprice"/>
<many-to-one name="item" column="itemid" cascade="none"/>
</composite-element>
</list>

</class>

I can insert an oder and a set of lineitems with no problem. But when I read I read the Parent class (Order), the children (lineItems) get loaded. But the attribues for the key column (orderid) and the index column (linenum) do not get loaded. (They are always zero-default value for java). All the other properties (like quantity, unitPrice and Item are loaded).

The orderid and linenum form the primary key in the lineitem table.

Any ideas? Any help would be appreciated.

Thanks
Matt

_________________
Matt Madhavan, Consultant


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 23, 2004 5:38 pm 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Before saying something is a bug please read an understand the documentation. The key column and the index columns are no properties of your composite element. Hibernate uses the key column to reference the association between Order and LineItem. The index is used to as the index value of Map/List in java (Map/List are indexed Collections in java) If you want them in your composite element (for whatever reason) you have to name them seperate as properties and even then I don't know if Hibernate accepts it.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 23, 2004 6:47 pm 
Newbie

Joined: Tue Mar 30, 2004 9:58 am
Posts: 8
Location: Chicago, IL
Hi Dencel,
My apologies to call it a "bug". I did it to get some attention. Please note that my requirements are similar to example given in chapter 18 of the hibernate refernce documentation. (customers/orders/line_items/products).

Please note that, line_number is a column in the line_items table. The order_id and product_id combined make a primary key. When I save the parent object (Order) with a list of line_items, the item_number is saved as well. Why cannot I read them back? order_id and line_number both are columns in the table. Why cannot I treat them as properties? What if I want line numebers to be sequentially generated instead of going from 0 to the number of line_items for each order.

May be there is a different ways to do this. I am trying to port the jpetstore sample application to spring (The data access is currebtely done by iBatis).

Any thoughts/help on this will be appreciated.

Once again my apologies.

Thanks
Matt Madhavan

_________________
Matt Madhavan, Consultant


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 23, 2004 6:49 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
My apologies to call it a "bug". I did it to get some attention.


For the record: never do this again.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 23, 2004 6:58 pm 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Quote:
Why cannot I treat them as properties? What if I want line numebers to be sequentially generated instead of going from 0 to the number of line_items for each order.


Java List does not have a feature to supply your own index numbering strategy. If you want to use your own sequentially numbering for the List I suggest you add an extra (db indexed) property to Listitem and add order-by="mySequence" to the hibernate mapping of your collection.

Hope this helps.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 23, 2004 7:46 pm 
Newbie

Joined: Tue Mar 30, 2004 9:58 am
Posts: 8
Location: Chicago, IL
Hi Dencel,
I went to <set> instead of <list>. It serves my requirements better now.

Instead of :

<list name="lineItems" table="lineitem" lazy="false">
<key column="orderid"/>
<index column="linenum"/>
<composite-element class="LineItem">
<property name="quantity" column="quantity"/>
<property name="unitPrice" column="unitprice"/>
<many-to-one name="item" column="itemid" cascade="none"/>
</composite-element>
</list>

Now I have :

<set name="lineItems" table="lineitem" lazy="false">
<key column="orderid"/>
<!-- index column="linenum"/-->
<composite-element class="LineItem">
<property name="linenum" column="linenum"/>
<property name="quantity" column="quantity"/>
<property name="unitPrice" column="unitprice"/>
<many-to-one name="item" column="itemid" cascade="none"/>
</composite-element>
</set>

It works just fine now.

Thanks
Matt Madhavan

_________________
Matt Madhavan, Consultant


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