hibernate - how to set auto increment in both mysql and oracle databases?
1: If you define your own generator, your have to use the generator attribute in @GeneratedValue
. And if you have created your own sequence you have to define the name with sequenceName
otherwise hibernate will create one for you.
@SequenceGenerator(name="some_gen", sequenceName="Emp_Seq")@GeneratedValue(generator="some_gen")
2:The most flexible (and portable) way is to use the TABLE
strategy
@GeneratedValue(strategy=GenerationType.TABLE)
or more explicit
@GeneratedValue(generator="some_gen")@TableGenerator(name="some_gen", table="ID_GEN", pkColumnName="GEN_NAME", valueColumnName="GEN_VAL")
This will generate (if schema generation is enabled) a table ID_GEN
with the columns GEN_NAME
, GEN_VALUE
, if schema generation is not available you have to create this table on your own.
You find more complete information from hibernate docs here: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch05.html#mapping-declaration-id-generator