-->
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: Error telling if an object is persistent or not?
PostPosted: Thu Oct 30, 2003 11:08 am 
Newbie

Joined: Fri Oct 10, 2003 8:53 am
Posts: 10
Location: Leeds, England
Consider the following:

I am writing code for a web app, where session.saveOrUpdate() is called for objects rather than session.flush()

I have an object (say Car) that has a primary key (id), and another column (say driver_id) that has a database unique index on it.

I create a new Car instace, carInstance, populate some values and call session.saveOrUpdate(carInstance).

This happens to fail, because the value I supplied for carInstance.getDriverId() already exists for another instance of Car.

So I change driver_id, and call session.saveOrUpdate(carInstance) again.

However this time I get a HibernateException because object I am attempting to persist does not exist.

It seems as though, when I call the first saveOrUpdate, a unique id is generated and assigned to carInstance. Then Hibernate performs an insert, which fails because of the unique constraint on driver_id.

However, the carInstance has already had its id assigned, and the failure does not set it back to the value specified in "unsaved-value" in the mapping xml.

So next time I call saveOrUpdate, Hibernate tries to do an update, which fails because the row does not exist.

Am I missing something? Is there some mapping xml value that needs to be set to prevent Hibernate behaving in this way, or is it a bug?

Thanks in advance...


Top
 Profile  
 
 Post subject: Re: Error telling if an object is persistent or not?
PostPosted: Thu Oct 30, 2003 11:27 am 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
NeillR wrote:
Consider the following:

Am I missing something? Is there some mapping xml value that needs to be set to prevent Hibernate behaving in this way, or is it a bug?

Thanks in advance...



Are the ids assigned by the DB? (auto sequence or something). If so, you should use "generated" .

pls post your mappings so we can try and help you.

_________________
Mauricio Hern


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 11:36 am 
Newbie

Joined: Fri Oct 10, 2003 8:53 am
Posts: 10
Location: Leeds, England
I used Car as an example. My actual class is called Policy, but its not that different in terms of difficulty!! :-)

So to be explicit:

The class is called Policy. It has four attributes, id, name, description, and item

There is a primary key id and oracle candidate keys on name and item.
I also have an oracle sequence called seq_policy that feeds the policy.id column. This is done via a Hibernate generator.

Here is the mapping xml for the class:

<hibernate-mapping schema="schema">
<class
name="Policy"
table="POLICY"
>
<id
name="id"
type="long"
column="ID"
>
<generator class="sequence">
<param name="sequence">seq_policy</param>
</generator>
</id>
<property
name="name"
type="java.lang.String"
column="NAME"
length="50"
not-null="true"
unique="true"
/>
<property
name="description"
type="java.lang.String"
column="DESCRIPTION"
not-null="true"
length="255"
unique="false"
/>
<property
name="item"
type="integer"
column="item"
not-null="true"
unique="true"
/>
</class>
</hibernate-mapping>

While it is true, following on from my previous mail, that I could catch DataAccessException when it is thrown by session.saveOrUpdate(newPolicyInstance) and reset the id value myself, I want to avoid that if I can, because that means I have to make Policy.setId() public. I would prefer to leave that function as private or protected as it prevents clients of the class changing the primary key (which they should never need to do... :-) ).

Obviously Hibernate still has access to setItem as it is 'injecting code' into the class to do the persistence.

It would seem the natural place to be performing clean up of this type

Regards
N


Top
 Profile  
 
 Post subject: I think this my misunderstanding (again) !!
PostPosted: Thu Oct 30, 2003 1:23 pm 
Newbie

Joined: Fri Oct 10, 2003 8:53 am
Posts: 10
Location: Leeds, England
Right,

The error is occurring when I call session.flush(). Actually because I'm using the Spring framework I get a Spring DataAccessException because the HibernateTransactionManager is perform a flush which is failing...

So I guess, there is not much that can be done at this point. If an error occurs here because of constraint issues, then it should be up to the caller to sort out...

Thanks anyway!!
:-)


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.