-->
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.  [ 10 posts ] 
Author Message
 Post subject: First row of the list is null always....
PostPosted: Thu May 04, 2006 5:43 am 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
I'm trying to run example from Hibernate reference: Customer/Order/Product.

Mapping is this:

<class name="Order" table="orders">
<id name="id">
<generator class="native"/>
</id>
<property name="date"/>
<many-to-one name="customer" column="customer_id"/>
<list name="lineItems" table="line_items">
<key column="order_id"/>
<list-index column="line_number"/>
<composite-element class="LineItem">
<property name="quantity"/>
<many-to-one name="product" column="product_id"/>
</composite-element>
</list>
</class>

In table Order I have one order and in table line_items I have one lineItem for this Order.

But, when I try to load Order:

...
order = (Order)session.load(Order.class, orderId);

In this order object's list of lineitems, first lineitem is always null, and second is good lineitem.

What is the problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 12:38 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Most likely, you don't have any rows with line_number = 0. If line_number starts from 1 and goes up, add the attribute 'base="1"' to the list-index element.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: It's working, but...
PostPosted: Fri May 05, 2006 1:31 am 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
O.K. it is working, but why I don't have element in list with index 0 ? I have one order and one lineitem for this order only.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 1:49 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
When you created the Order object the first time, in order to save it, where did you put the LineItem? If you did this, you'll have put it in position 1:
Code:
order.getLineItems().add(1, item)
The list that you get back when you re-load the order will have a 0 index (all java lists do), so it'll be null.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Inserting Order...
PostPosted: Fri May 05, 2006 3:23 am 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
I have something wierd here, when I load Order with session.load (Order.class, orderID) it loads Order and LineItems, but when I insert new LineItem in Order and try to saveOrUpdate, there is nothing inserted in database. But, also there is no error.

When you created the Order object the first time, in order to save it, where did you put the LineItem?

I'm adding LineItems like this:

order.getLineItems().add(item).


Top
 Profile  
 
 Post subject: Cascade on Order...
PostPosted: Fri May 05, 2006 4:37 am 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
It seems that i forgot to set cascade on Order. Now, when I set it, and call saveOrUpdate on Order, it generates this error:

org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1

?


Top
 Profile  
 
 Post subject: My mistake....
PostPosted: Fri May 05, 2006 5:43 am 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
O.K. I found error:

It's been

<id name="id" column="id" type="long" unsaved-value="null">

but, because it is long (not Long) I assume that this must be:

<id name="id" column="id" type="long" unsaved-value="0">


Top
 Profile  
 
 Post subject: Problem...
PostPosted: Fri May 05, 2006 6:31 am 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
I have problem to insert a new Customer and his Orders:

this is schema:

<class name="Customer" table="customers">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="orders" inverse="true">
<key column="customer_id"/>
<one-to-many class="Order"/>
</set>
</class>

problem is that Customer is not commited, and when orders are trying to insert they don't know for Customers id. How to Customer be first inserted and then to orders be inserted ?


Top
 Profile  
 
 Post subject: Error
PostPosted: Fri May 05, 2006 6:44 am 
Regular
Regular

Joined: Thu May 04, 2006 5:24 am
Posts: 55
I forget, concrete error is:

Field 'customer_id' doesn't have a default value

org.hibernate.exception.GenericJDBCException: could not insert: [org.package.Order]


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 07, 2006 6:44 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
MilanMilanovich wrote:
How to Customer be first inserted and then to orders be inserted ?

Add 'not-null="true"' to the <key> element.
You probably also want to remove 'inverse="true"', as that tells hibernate not to insert or delete Orders just because they've been added to Customer's set.

_________________
Code tags are your friend. Know them and use them.


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