-->
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.  [ 3 posts ] 
Author Message
 Post subject: Switching between @GeneratedValue(AUTO) and sequences
PostPosted: Sun Jan 25, 2009 2:55 pm 
Newbie

Joined: Sun Jan 25, 2009 12:50 pm
Posts: 2
I would like to know if it is possible (and how) to switch from a @GeneratedValue(AUTO) annotation to sequences, without changing the source code.

The scenario is this:

I have an application that must run on different databases - like MySQL and Oracle -, and by default the code was written using @GeneratedValue(AUTO), which works for MySQL. However, for Oracle (and any other sequence-driven database) I must use one sequence per entity - i.e, the generic "hibernate_sequence" approach is not an option at all.

I would like to know if there is any way to configure Hibernate, be either via another annotations, or xml overriding, instead of changing the source code.
I have read http://www.hibernate.org/hib_docs/annot ... iding.html, but this seems to be JPA (EntityManager) specific, and I'm using SessionFactory. Switching to JPA is not an option as well.

I also tried using .hbm.xml files to override the annotations, but didn't work, as Hibernate appears to consider only the hbm file, discarding all annotation configuration.

I also tried implementing my own generator, with the idea that it would check the dialect and delegate to SequenceGenerator or IdentityGenerator, but that didn't work as well, as, for the little I read of Hibernate's source code, the approach for Identities is kind of hardcoded in the code (like ".. if the generator is identity, execute the statement this way, using getGeneratedKeys(), else execute the code that way").

I looked at SequenceIdentiyGenerator, that in its documentation says "A generator which combines sequence generation with immediate retrieval through JDBC3 {@link java.sql.Connection#prepareStatement(String, String[]) getGeneratedKeys}.". Unless I'm using it incorrectly (see code below), it didn't work as well (mysql crashes saying that the dialect does not support sequences).

The code I tried with SequenceIdentityGenerator is:

Code:
   @Id
   @GeneratedValue(generator = "my_generator")
   @GenericGenerator(name = "my_generator", strategy = "sequence-identity",
      parameters = { @Parameter(name = "sequence", value="my_user_sequence")})
   private int id;


Does anyone have a suggestion?

Thank you very much.
Rafael


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2009 7:43 pm 
Newbie

Joined: Sun Jan 25, 2009 12:50 pm
Posts: 2
Ok, just found the solution:

Code:
   @Id
   @SequenceGenerator(name = "user_sequence", sequenceName = "my_user_sequence")
   @GeneratedValue(strategy = GenerationType.AUTO, generator = "user_sequence")
   private int id;


Rafael


Top
 Profile  
 
 Post subject: Re: Switching between @GeneratedValue(AUTO) and sequences
PostPosted: Wed May 04, 2011 11:09 am 
Newbie

Joined: Mon Mar 01, 2010 5:04 pm
Posts: 3
Location: Turkey
thank you so much Rafael, this is exactly what I'm looking for..


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