-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to retrieve id generated by trigger in DB
PostPosted: Tue Mar 06, 2007 4:50 pm 
Newbie

Joined: Fri Dec 02, 2005 12:43 pm
Posts: 6
Location: New York, NY
Hibernate version: 3.2

I am generating the id with a trigger in the DB, that is itself calling a sequence. But I cannot retrieve the generated id for future use in the application.

Something like this:

myObjectToPersist = new ObjectToPersist();
myObjectToPersist.setId(Long.valueOf("-1"));
...

Then, when I want to persist just call:
saveOrUpdate(myObjectToPersist)

After this is executed, on the DB itself the new row is added with the generated id by the trigger. But my persisted object (myObjectToPersist) still contains the "-1" I initially set as dummy (updated by the DB trigger).

This is my mapping (just showing significant code):

...
<id name="id" column="ID" type="long" unsaved-value="-1">
<!--generator class="native"/-->
</id>
...

Any ideas?

Thanks,
Gabriel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 06, 2007 7:24 pm 
Newbie

Joined: Wed Apr 19, 2006 12:50 pm
Posts: 8
hey
i think it is due to your mapping - i am using this mapping for my sequence generation:

<generator class="sequence">
<param name="sequence">customer_sequence</param>
</generator>


and it works very well. Hibernate use my sequence objects in my db to generate correct ids - i dont need to set them my self when i create new objects.

the value of param is the name of my db sequence..

rasmus


Top
 Profile  
 
 Post subject: What if additional info is added in the trigger
PostPosted: Tue Mar 06, 2007 10:58 pm 
Newbie

Joined: Fri Dec 02, 2005 12:43 pm
Posts: 6
Location: New York, NY
The only problem is that I would like to standarize the id creation (also for external access of my hibernate-enabled app), plus the id creation is not just a sequence number.

In the trigger I am using the sequence, plus extra process.

In fact, thank you, I was using the sequence as you are saying and works as a charm.

Any ideas, for that extra functionality, then always use my trigger for id's creation process?

Thanks,
Gabriel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 08, 2007 5:52 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
If your table contains some unique business-key you could use the select-generator to retrieve your DB-generated id. Just do something like
Code:
<generator class="select">
    <parameter name="key">name_of_businesskey_column</parameter>
</generator>


If you don't have some unique identifier (apart form the id-property) hibernate will have trouble finding the inserted row that belongs to the object you just persisted.

Greets

piet


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 08, 2007 12:23 pm 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
check http://forum.hibernate.org/viewtopic.php?t=965442
tested on Oracle 10g (does not work with 8i due to jdbc bug)


Top
 Profile  
 
 Post subject: Thanks for you comments.
PostPosted: Thu Mar 08, 2007 1:19 pm 
Newbie

Joined: Fri Dec 02, 2005 12:43 pm
Posts: 6
Location: New York, NY
i just decided to use the sequence generator. and in the trigger ask if the id is null, then use the sequence. if not (when external access) use the predefined on the row.

Thanks


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