简体   繁体   中英

What is the Hibernate Sequence generation strategy that works for DB2, Oracle and MSSql

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?

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;
        ...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM