-->
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 get the sequence number without saving the dto
PostPosted: Fri Jan 13, 2006 2:36 am 
Newbie

Joined: Tue Jul 12, 2005 9:50 pm
Posts: 7
Hi all,

I need to get the sequence number to use in my application, before inserting it into the database. <generator....> is generating the sequence number only after i do session.save(somedto). Unfortunatly i have some not null values, hence i cannot just save an empty dto. Hence i have to get the sequence first before saving. Please help in what is the best way to do this.

thank you.
Gupta.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 15, 2006 9:39 am 
Beginner
Beginner

Joined: Sat Dec 17, 2005 1:24 pm
Posts: 42
Location: Berlin, Germany
Hi Gupta!

Could you elaborate a bit more on your use case? I found it rarely necessary to get a primary key identifier as Hibernate would generate it to fulfil database restrictions. If your objects are mapped by Hibernate, than Hibernate is doing just that for you, ensuring, that database inserts occur in the right order.

In mixing legacy (JDBC) code with Hibernate I find it sometimes necessary to flush() the session early to force Hibernate to save the objects, and therefore assign identifiers I can use in the legacy JDBC areas of the code. But even than, my in-memory objects are ready to be saved (i.e. they are complete).

All the best,

René


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 15, 2006 4:24 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello you must be more specific. What DB do you use and could you give your mapping.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 3:24 am 
Newbie

Joined: Tue Jul 12, 2005 9:50 pm
Posts: 7
Thank to both of your for responding.

Currently, I am using MS SQL Server. But this system should be DB independent.

To explain in simple terms. I need to get have a unique reference number(for which i would like to use a sequence) and show it in the input page for the user to input. The input given by the user will be stored in database. Hence the sequence should be generated while generating the input page and stored in the database after user submission.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 4:15 am 
Beginner
Beginner

Joined: Sat Dec 17, 2005 1:24 pm
Posts: 42
Location: Berlin, Germany
Hello Gupta!

I feel this is an odd requirement, but hey, it's your system. ;-)

Did you have a look at the generator type "assigned" ? (see section 5.1.4.5
http://www.hibernate.org/hib_docs/v3/re ... d-assigned)

All the best,

René


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 7:32 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
my ideas:
1)
separate your id and a "task number". Task number may be a separate field in a separated table, eg. a sequence or increment field; or a stored procedure generating numbers or ...
So you will have a separate mapping.

if the user starts the dialog, create the task number just before. You may have unused task numbers if the user does not save his form or if you do not delete unused task numbers.
2)
nearly as 1)
but you have a 1:1 relationship from task number to your task details.
3)
if you have differnent tasks have a look at inheritance mapping in the reference.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


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.