Java JPA主键自动生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1817625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA primary key auto generate
提问by cometta
my primary key entity look like below
我的主键实体如下所示
@GeneratedValue(strategy= GenerationType.TABLE)
private Long id;
when i run, i get error
当我运行时,出现错误
could not get or update next value;nested exception is org.hibernate.exception.SQLGrammerException:could not get or update next value
无法获取或更新下一个值;嵌套异常是 org.hibernate.exception.SQLGrammerException:无法获取或更新下一个值
but when i just change to
但是当我改变为
@GeneratedValue
private Long id;
no error throw . I want to generate unique primary key per table on oracledb .
没有错误抛出。我想在oracledb上为每个表生成唯一的主键。
采纳答案by Pascal Thivent
The @GeneratedValue(strategy=GenerationType.TABLE)
tells the JPA provider to use a table to get IDs from when inserting newly created entities into the database.
该@GeneratedValue(strategy=GenerationType.TABLE)
告诉JPA提供者使用一个表,从插入新创建的实体到数据库时,你得到的ID。
When using Hibernate as provider, this will result in a table hibernate_sequences
which has two columns: the entity name, and the max identity already assigned to this entity. Here, it seems Hibernate doesn't succeed to get the next ID from it for your entity but it's hard to say exactly why because you didn't provide enough informations for that.
当使用 Hibernate 作为提供者时,这将产生一个hibernate_sequences
包含两列的表:实体名称和已分配给该实体的最大标识。在这里,似乎 Hibernate 没有成功从它为您的实体获取下一个 ID,但很难确切地说出原因,因为您没有为此提供足够的信息。
So, could you please provide the full stacktrace? Also, please turn logging with hibernate.show_sql
property set to true
and set the proper log level log4j.logger.org.hibernate.SQL=DEBUG
. Join the log to your question if possible.
那么,您能否提供完整的堆栈跟踪?另外,请将hibernate.show_sql
属性设置为日志记录true
并设置正确的日志级别log4j.logger.org.hibernate.SQL=DEBUG
。如果可能,将日志加入您的问题。
Maybe just check that you did configure the correct hibernate.dialect
for Oracle. Actually, join your hibernate configuration too if possible.
也许只是检查您是否hibernate.dialect
为 Oracle配置了正确的配置。实际上,如果可能的话,也加入你的休眠配置。
PS: The "traditional" way to generate PK with Oracle is to use sequences (you could let Hibernate guess the best strategy for your database type using GenerationType.AUTO
or force it using SEQUENCE
) but I'll assume you want the resultant data structure be database agnostic. If not, I'd suggest to go for sequences instead.
PS:使用 Oracle 生成 PK 的“传统”方法是使用序列(您可以让 Hibernate 使用GenerationType.AUTO
或强制使用它来猜测您的数据库类型的最佳策略SEQUENCE
),但我假设您希望生成的数据结构与数据库无关。如果没有,我建议改为使用序列。
EDIT: Answering a comment from the OP about GenerationType.AUTO
. Indeed, the default is a single global sequence called hibernate_sequence
and this might be a problem. But, with the setup shown below, you can use GenerationType.AUTO
and still control the name of the sequence for the cases where the database uses sequences:
编辑:回答 OP 关于GenerationType.AUTO
. 事实上,默认值是一个单一的全局序列hibernate_sequence
,这可能是一个问题。但是,通过下面显示的设置,您可以GenerationType.AUTO
在数据库使用序列的情况下使用并仍然控制序列的名称:
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
private long id;
In other words, you can use use a different sequence name for each table without loosing portability.
换句话说,您可以为每个表使用不同的序列名称而不会失去可移植性。
回答by Sheng.W
There are 4 strategies for auto generation in JPA:
JPA中有4种自动生成策略:
- Auto
- Identity
- Sequence
- Table
- 汽车
- 身份
- 序列
- 桌子
For Oracle auto generation primary key annotation, Sequence and Table are your choices. The basic logic is to define a generator first, use @SequenceGeneratoror @TableGeneratorrespectively, then use the generator as attribute in @GeneratedValue.
对于 Oracle 自动生成主键注释,Sequence 和 Table 是您的选择。基本逻辑是先定义一个生成器,分别使用@SequenceGenerator或@TableGenerator,然后在@GeneratedValue 中使用生成器作为属性。
This is a sample of how to use Sequence strategy:
这是如何使用 Sequence 策略的示例:
@Id
@SequenceGenerator(name="SEQ_GEN", sequenceName="SEQ_JUST_FOR_TEST", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_GEN")
private long id;
Here is an example of how to use table strategy:
以下是如何使用表策略的示例:
@Id
@TableGenerator(name="TABLE_GEN",table="T_GENERATOR", pkColumnName = "GEN_KEY", pkColumnValue = "MONITOR2012.T_JUST_FOR_TEST", valueColumnName = "GEN_VALUE", initialValue = 1, allocationSize = 1 )
@GeneratedValue(strategy = GenerationType.TABLE, generator="TABLE_GEN")
private long id;
If no generator specified in @GeneratedValueannotation, the choice will leave to the JPA implementation.
如果@GeneratedValue注释中没有指定生成器,则选择将留给 JPA 实现。
If you are working on database with existing tables, make sure you the sequence or the table defined in database before you run your application. The table generator will also need you to insert a line to the table before the @GeneratedValue annotation can work properly.
如果您正在使用现有表处理数据库,请确保在运行应用程序之前在数据库中定义序列或表。 表生成器还需要您在表中插入一行,@GeneratedValue 注释才能正常工作。
Here is a tutorial about how to configure primary key auto generation in JPA for Oracle database.
这是一个关于如何在 JPA 中为 Oracle 数据库配置主键自动生成的教程。