Defining database independent JPA object uid

ⅰ亾dé卋堺 提交于 2019-12-04 17:07:26

Using a database table is a portable way to generate identifiers.

The simplest way to use a table to generate identifiers is to specify TABLE as the generation strategy:

@Id
@GeneratedValue(strategy=GenerationType.TABLE)
@Column(name="id")
private long id;

The provider will create the default table if you're using schema generation; if not, you must specify an existing table:

@TableGenerator(name="InvTab",
    table="ID_GEN",
    pkColumnName="ID_NAME",
    valueColumnName="ID_VAL",
    pkColumnValue="INV_GEN")
@Id 
@GeneratedValue(generator="InvTab")
@Column(name="id")
private long id;

http://www.oracle.com/technology/products/ias/toplink/jpa/howto/id-generation.html#table

I have researched using GenerationType.AUTO and it does appear to be the better option. It allows the JPA implementation to choose whatever is best for the data storage system you are using.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!