-->
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: generating an id before the object is persisted
PostPosted: Fri Sep 16, 2005 9:32 am 
Newbie

Joined: Thu Oct 23, 2003 6:12 am
Posts: 18
I need to separately generate an ID,

Do I use the IdentifierGenerator class?

What I need to do is:

1) get an (native generated) ID from the database (<generator class="native">)

2) close the session!

3) open a new session!

4) create pojo with the ID

5) set some nice properties

6) persist the POJO

Some help with step 1 (en maybe step 4) is greatly appreciated,

Ben


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 9:51 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
"native" isn't an actual identifier generator, it's a placeholder that turns into "sequence", "identity", or "hilo", depending on the database Dialect. Those three generators all behave differently. The "identity" generator uses identity columns for those databases that support it, and I don't think you can really know what the identifier will be until after you insert the record (I think it's possible to find out what it WOULD be if no one else inserts anything before you, but that's error prone).

Anyway, if you have to know the identifier before-hand, you probably have to write a custom generator, and do the work yourself. I don't think that Hibernate exposes any access to the IdentifierGenerator. At least in the javadoc it says:

Quote:
It is not intended that this interface ever be exposed to the application. It is intended that users implement this interface to provide custom identifier generation strategies.


Top
 Profile  
 
 Post subject: Re: generating an id before the object is persisted
PostPosted: Fri Sep 16, 2005 9:54 am 
Newbie

Joined: Wed Mar 23, 2005 2:32 pm
Posts: 12
I would recommend option 1. For example
<id
name="id"
type="java.lang.Long"
>
<column name="ID" length="19" not-null="true" unique="true" sql-type="BIGINT" />
<generator class="com.example.LongGenerator" />
</id>

The generator class can be written with a combination of timestamp and random id.

In addition you may wish to include the <version> tag and a version column in your tables to handle concurrency.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 9:58 am 
Newbie

Joined: Thu Oct 23, 2003 6:12 am
Posts: 18
In Oracle terminology: I need to get a sequence value from the database beforehand, en use it later at insert time (then the sequence-insert-trigger in the database is not used because I specify the previously fetched serial value)

Shouldn't this be possible with hibernate as well?

I did find some code that looks like what I want:

Code:
ClassPersister classPersister = ((SessionFactoryImplementor) sessionFactory).getPersister(Person.class);
Person person = new Person();
IdentifierGenerator idGenerator = classPersister.getIdentifierGenerator();
Serializable id = idGenerator.generate((SessionImplementor) session, person);


But, I just want the serial value for later use, I don't want to instantiate a POJO right away, (as in this exmaple)

thanks,

Ben


Top
 Profile  
 
 Post subject: Re: generating an id before the object is persisted
PostPosted: Fri Sep 16, 2005 10:00 am 
Newbie

Joined: Thu Oct 23, 2003 6:12 am
Posts: 18
roadway wrote:
I would recommend option 1. For example
<id
name="id"
type="java.lang.Long"
>
<column name="ID" length="19" not-null="true" unique="true" sql-type="BIGINT" />
<generator class="com.example.LongGenerator" />
</id>

The generator class can be written with a combination of timestamp and random id.

In addition you may wish to include the <version> tag and a version column in your tables to handle concurrency.


Do I really need to implement my own generator just because I want to get a value before insert time? I ratherly want to use the serial/autonum from the db.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 10:18 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
I'd try out that code you posted. It might work. The problem you might run into is that if you tell Hibernate that the generator is "sequence", for example, even if you assign the identifier in your object, I think Hibernate will think the object is already persistent, and will try to update instead of insert.

Set the generator to "assigned", and then do your own query to get the sequence value (don't even bother with the IdentifierGenerator stuff). Then you can create the id value whenever you want, and save the object later.


Top
 Profile  
 
 Post subject: Re: generating an id before the object is persisted
PostPosted: Fri Sep 16, 2005 10:25 am 
Newbie

Joined: Wed Mar 23, 2005 2:32 pm
Posts: 12
Regarding: Do I really need to implement my own generator just because I want to get a value before insert time? I ratherly want to use the serial/autonum from the db.

The response by nathanmoon probably applies. I am not sure how, but I think they may be a way to request the db to give you "ten" generated keys which you can then use.


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.