-->
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.  [ 7 posts ] 
Author Message
 Post subject: Optimistic Lock using Version causes error on persist
PostPosted: Fri Oct 14, 2005 12:50 pm 
Newbie

Joined: Fri Oct 14, 2005 12:41 pm
Posts: 4
Hibernate version:3.1.rc1

I have a domain object called product that uses OptimisticLockType.VERSION, with a column of type @Version.

This product domain object has a reference to OrderLineItem via a many-to-one relationship. When I try to create a OrderLineItem I get the following error:

not-null property references a null value.

Here is a snippet of the code:
OrderLineItem lineItem = new OrderLineItem();
Product product = new Product();
product.setId(1)

lineItem.setProduct(product).

entityManager.persist(lineItem).

This causes the error above.

If I add the following code:
product.setVersion(1)

Then no error.

Any ideas why I have to set the Version column in the product domain object?


Top
 Profile  
 
 Post subject: Re: Optimistic Lock using Version causes error on persist
PostPosted: Fri Oct 14, 2005 12:58 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Why are you setting the ID yourself ? Are you using the "assigned" ID Generator ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 1:08 pm 
Newbie

Joined: Fri Oct 14, 2005 12:41 pm
Posts: 4
This is the case where the product has already been created, and yes I do use assign generator for the product domain object.

What I am trying to do is create the OrderLineItem using an existing product that a user has selected via a GUI. The id I set gets passed to me and I use that one when associating the product to the OrderLineItem


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 1:15 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
mglicks wrote:
This is the case where the product has already been created, and yes I do use assign generator for the product domain object.

What I am trying to do is create the OrderLineItem using an existing product that a user has selected via a GUI. The id I set gets passed to me and I use that one when associating the product to the OrderLineItem


If you want to add an OrderLineItem to an existing Product and assign it to the new OLI. You can't just create a dummy Product with the same key.

From Hibernate Reference doc - section 6.1.7

"A version or timestamp property should never be null for a detached instance, so Hibernate will detect any instance with a null version or timestamp as transient, no matter what other unsaved-value strategies are specified...."

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 1:20 pm 
Newbie

Joined: Fri Oct 14, 2005 12:41 pm
Posts: 4
makes perfect sense. I have already started to change my code to retrieve the Product from the database and then associating this product to the OrderLineItem object.

I assume this only needs to happen when I use OptimisticLocking in a Domain Object?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 1:21 pm 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
In the reference doc, you can find this:

Quote:
Declaring a nullable version or timestamp property is an easy way to avoid any problems with transitive
reattachment in Hibernate, especially useful for people using assigned identifiers or composite keys!


Hope this is useful.

_________________
Jason Li
Don't forget to rate:)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 1:30 pm 
Newbie

Joined: Fri Oct 14, 2005 12:41 pm
Posts: 4
Useful, but I am using EJB3 with annotations, and it does not look like the @Version annotaion supports these properties.

From my Product domain object
@Version
@Column(name = "version")
private Integer version;


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