Question

I am working on an enterprise web app which needs to work on DB2, Oracle or MySQL. The major problem I am facing is to select an hibernate sequence ID generation which will work with all the three DBs. As of now my solution has three different projects for the DBs and I wish to combine the three into one unified project for maintenance ease. What is ID generation strategy that will go with all the three DBs?

Was it helpful?

Solution

You can use @TableGenerator.
This annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. A table generator may be specified on the entity class or on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types).

For more info see here


Example from docs:

@Entity public class Employee {
        ...
        @TableGenerator(
            name="empGen", 
            table="ID_GEN", 
            pkColumnName="GEN_KEY", 
            valueColumnName="GEN_VALUE", 
            pkColumnValue="EMP_ID", 
            allocationSize=1)
        @Id
        @GeneratedValue(strategy=TABLE, generator="empGen")
        public int id;
        ...
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top