I am working on a Java application that must perform CRUD operations (using Hibernate 4.3.8) in two different databases with the same database schema. There is MySQL (version 5.1.73) and Oracle (11g Express Edition version 11.2.0.2.0 - 64 bit).
Java classes with JPA comments were generated from database tables with Hibernate code generation.
The problem is that now we need to use automatic primary key generation, and MySQL uses GenerationType.IDENTITY, and Oracle uses GenerationType.SEQUENCE. In addition, we need the ability to manually manually set the primary key in some rare cases.
The serial code in the annotated class works with generating an automatic key for both databases, but it fails if the primary key itself is installed.
@GeneratedValue(strategy=GenerationType.AUTO, generator="sequence_generator") @SequenceGenerator(name="sequence_generator", sequenceName="SEQUENCE1") @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; }
Without annotations @GeneratedValue and @SequenceGenerator, you can manually set the primary key, but automatic generation does not work.
java oracle mysql hibernate jpa
Simon schürg
source share