-->
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.  [ 4 posts ] 
Author Message
 Post subject: Using a sequence without a table
PostPosted: Wed Mar 02, 2005 7:23 am 
Newbie

Joined: Wed Mar 02, 2005 7:01 am
Posts: 2
In a web application I want to use a sequence in the DB to generate unique, incrementing ids I need for an external interface.

Can I do it without using native SQL (in Hibernate 2.1)?

The closest I found was:
Dialect.getSequenceNextValString ( "my_sequnce_name" )
But I found no way to find out the Dialect of the running session.

Actually I need something like:
Serializable Session.getSequenceNextVal ( "my_sequnce_name" );


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 9:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Use the sequence generator and you don't need to think about it since it will just work.
[url]http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-id-sequences
[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 9:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Sorry i missed the 'external interface'. In this case use native SQL interface where you may hide the impl in a support package to get the clean method call. Alternative, you could use a direct JDBC call.


Note: Keep the Configuration object around and it can tell you the dialect being used.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 6:05 am 
Newbie

Joined: Wed Mar 02, 2005 7:01 am
Posts: 2
during setup:

Configuration config = new Configuration ().configure ( "/config/hibernate.cfg.xml" );

SessionFactory sf = config.buildSessionFactory ();
DatabaseSession.setSessionFactory ( sf );

Dialect dialect = Dialect.getDialect ( config.getProperties () );
DatabaseSession.setDialect ( dialect );

later usage:

Dialect dialect = DatabaseSession.getDialect ();
String query = dialect.getSequenceNextValString ( "my_sequence" );

PreparedStatement ps = DatabaseSession.getSession ().connection ().prepareStatement ( query );


Seems to work, but I see ample room for simplification by the Hibernate team :-)


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