-->
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: Can you leave ID generation to Database
PostPosted: Tue Apr 04, 2006 7:52 am 
Newbie

Joined: Wed Nov 30, 2005 7:53 am
Posts: 9
Hibernate version: 3.1

Name and version of the database you are using: Ingres II 3

Hi Hibernators,

Basically i want to set Hibernate to leave all the key generation to the database entirely (i want the scope to be able to insert rows outside of Hibernate and have valid ids generated for them).

I've lookedaround and found the PostInsertGenerator abstract that sounds like the right kind of thing, but have been unable to find any details about an implementation that will retrieve the id generated by the database (though Statement.getGeneratedKeys for example). I really want hibernate just to generate a simple table that defines in the database which column is the PK and just let it look after it.

So before i set off on a potentially red herring chase, i was wondering if there is acutally a solution already availible?

Cheers

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 9:59 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Code:
<id .... >

<generator class="native"/>

</id>

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 10:35 am 
Newbie

Joined: Wed Nov 30, 2005 7:53 am
Posts: 9
Thanks for the thought, but i found something that acutally works!

1. Set up a Sequence in Ingres

CREATE SEQUENCE transIDGen

2. Tell Hibernate about it
<id
name="transID"
column="intTransID"
type="int"
>
<generator class="sequence">
<param name="sequence">transIDGen</param>

Or Xdoc with

@hibernate.generator-param name="sequence" value="transIDGen"

3. Then can use plain sql like

INSERT INTO tempy (primary, field) VALUES (transIDGen.nextVal, \'inserted\')

and Hibernate reports using the sequence itself

Hibernate: select nextval for transIDGen

And bingo, ids generated!

Tom


Top
 Profile  
 
 Post subject: better approach.
PostPosted: Tue Apr 04, 2006 1:35 pm 
Newbie

Joined: Tue Apr 04, 2006 9:56 am
Posts: 10
In addition to the above configuration,
If you configure your hibernate to ignore your foreign key column on insert and update like insert="false" and update="false",
Your insert statement will reduce to

INSERT INTO tempy (field) VALUES ( \'inserted\')

Don't forget to rate...

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 6:26 am 
Newbie

Joined: Wed Nov 30, 2005 7:53 am
Posts: 9
uummm, good thought.

However there is another consideration i have. I need the value generated by the sequence for the rest of the requests journey though the system.

This i found a little difficult to achieve and have ended up with doing a "select sequence.nextVal' setting it in the object (hibernate does this for me on the sly when using it) and then doing my insert. I failed to find a way of doing this in one step.

I hav'nt tried it but i think your solution would suffer from the same problem. Any thoughts on a one step method to achieve both these goals? Essentially lookin for a form of insert that allows me to return any values generated for it (i tried the stat.getGeneratedKeys(), but it'd did'nt appear in the resultset!!) or a select next_val&insert sql_including_val in one statement.

Realise this may be straying out of hibernate territory now....

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 11:19 am 
Newbie

Joined: Tue Apr 04, 2006 9:56 am
Posts: 10
Let me understand your issue, You have table which has a column, whose value is generated by sequence object.
After you insert the record to that table you want to get the column value which was populated by sequence object.
If this is your issue, here is solution,

First you insert the TO, as part of the insert, hibernate sets the column value based on the sequence.nextvalue(). Once the insert is completed,
generated sequence value is in you TO.

I tested this in my application. Column # 15 value is generated by sequence object. Its value is set to 273.
After the insert, I print the value of the column # 15, That is the next debug stm.

[Apr 05, 2006 11:13:09] DEBUG [NullableType] - binding '273' to parameter: 15

AFTER INSERTING THE SEQUENCE VALUE = 273

_________________
Cheers,
Don't forget to rate the post, Thank You


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 12:13 pm 
Newbie

Joined: Wed Nov 30, 2005 7:53 am
Posts: 9
That about sums it up. I am having to make a 'select' before i can insert (both in hibernate and without) to obtain the next sequence value and set it in my object.

I am now working on ways/ of generating the IDs from the request data (ie creating meaningful ids as aposed to meaningless sequential ones) to avoid this problem all together!

Thanks for the thoughts and ideas!

Tom


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.