-->
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.  [ 3 posts ] 
Author Message
 Post subject: Generating a value which depends on id
PostPosted: Tue Jun 05, 2007 10:17 am 
Beginner
Beginner

Joined: Thu May 10, 2007 4:20 am
Posts: 27
Hello,

I came upon an interesting problem:

I have a table,
which has an id and depends_on_id columns.
depends_on_id is constrained not to contain null values,
and I map a class SomeClass to it.

I use org.hibernate.id.enhanced.TableGenerator to generate the ids.

The caveat is that the value of depends_on_id depends on the id generated - that is I need to know the id before I can assign a value to the property mapped to some_column.

So, I tried the following code:

SomeClass obj = new SomeClass();
getCurrentSession().save(obj); // at this point the id is successfully generated
obj.generateValueForSomeColumn(); //using the id just generated
getCurrentSession().getTransaction().commit();

The commit (and not the save) fails with an exception which says that I've violated a not null constraint on depends_on_id.

If I remove the not null constraint - everything works fine.

My guess is that this happens since the commit does actually 2 operations like the following -
1. insert (id,null)
2. update (id,value)
And the insert fails, because of the constraint.


Am I right, and if so, how can I foretell the value of the id?

I can summarize the problem like this:

1. I need the generated id to create a value for depends_on_id column.
2. I cannot save the object to generate this id as I still don't have a value for depends_on_column, if would violate the not null constraint.

How can I possible break this magical cycle?

I will really appreciate any suggestions,
Vitaliy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 12:07 pm 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
save your object with a null ID and let the database create one for you.
example:
Code:
<hibernate-mapping>
    <class name="uk.ltd.conntrol.domain.CostPeriod" optimistic-lock="version" table="CostPeriod" schema="dbo">
        <id name="id" type="int" unsaved-value="null">
            <column name="id" />
            <generator class="native"></generator>
        </id>

Once you've persisted your object you can call getId() if you really need t know what it is.

Hope that helps.

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 12:25 pm 
Beginner
Beginner

Joined: Thu May 10, 2007 4:20 am
Posts: 27
There is a not null constraint over the id column as well.

Thanks,
Vitaliy


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