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