Reputation: 71
In my project we are using Oracle and we want to give support for MYSQL also. Oracle have sequence support but MYSQL don't have. which GenerationType is good if we want to support ORACLE and MYSQL with same code base using Hibernate persistence provider among AUTO,IDENTITY & TABLE(or Any other way)?, can any one give brief description about these please?
Ex:
Class POJO{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
------
@GeneratedValue(strategy = GenerationType.IDENTITY)
-----
@GeneratedValue(strategy = GenerationType.TABLE)
------
(or Any other best approch?)
}
Upvotes: 3
Views: 3766
Reputation: 1582
You may want to look into configuring all the database-dependant stuff in an xml configuration file. I haven't used xml files to configure hibernate for years and I've never mixed it with annotations. But the way I understand it: Your xml configuration will overwrite this specific configuration from your annotations in your POJO.
So you could leave your POJO with your oracle-specific ID Generator, and in case you deploy it for a MySQL, you would need to add an xml configuration with your MySQL-specific ID Generators.
I can't give you any details on this though. sorry.
Upvotes: 0
Reputation: 354
You can use
@GeneratedValue(generator = GenerationType.TABLE)
It will work for both Oracle and mySql
Upvotes: 1
Reputation: 4939
Since MySQL supports auto_increment use GenerationType.AUTO for its corresponding POJOs.
For the POJOs for Oracle database you could create a sequence for each and use it in your POJOs as illustrated below.
Sequence creation
CREATE SEQUENCE MY_SEQ
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
Generation type
I suppose the primary key is labeled ID in your Oracle table.
@Id
@GeneratedValue(generator = "mySeq")
@SequenceGenerator(name = "mySeq", sequenceName = "MY_SEQ", allocationSize = 1)
@Basic(optional = false)
@NotNull
@Column(name = "ID")
private Long id;
Upvotes: 0