JPA主键自动生成 [英] JPA primary key auto generate

查看:118
本文介绍了JPA主键自动生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主键实体如下所示:

$ p $ $ $ c $ @GeneratedValue(strategy = GenerationType.TABLE)
private Long id ;

当我运行时,我收到错误

无法获取或更新下一个值;嵌套的异常是org.hibernate.exception.SQLGrammerException:无法获取或更新下一个值



但是当我更改为

p>

  @GeneratedValue 
私人长ID;

没有错误抛出。我想在 oracle db的每个表上生成唯一的主键。 @GeneratedValue(strategy = GenerationType.TABLE)告诉我们: 当使用Hibernate作为提供者时,这将导致一个表 hibernate_sequences 它有两列:实体名称和已经分配给该实体的最大身份。在这里,看起来Hibernate没有成功从您的实体获取下一个ID,但很难说明原因,因为您没有提供足够的信息。

那么,您能否提供完整的堆栈跟踪?另外,请将日志记录设置为 hibernate.show_sql 属性设置为 true ,并设置正确的日志级别 log4j.logger.org.hibernate.SQL = DEBUG 。如果可能的话,加入日志到你的问题。



也许只是检查你是否配置了正确的 hibernate.dialect 甲骨文。实际上,如果可能,请加入您的hibernate配置。



PS:使用Oracle生成PK的传统方法是使用序列(您可以让Hibernate猜测最佳策略为您的数据库类型使用 GenerationType.AUTO 或强制它使用 SEQUENCE ),但我会假设您想要结果数据结构是数据库不可知的。如果没有,我建议去序列。



编辑:回答关于 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)
私人长ID;

换句话说,您可以为每个表使用不同的序列名称,而不会丢失可移植性。


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

but when i just change to

@GeneratedValue 
private Long id;

no error throw . I want to generate unique primary key per table on oracle db .

解决方案

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.

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.

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.

Maybe just check that you did configure the correct hibernate.dialect for Oracle. Actually, join your hibernate configuration too if possible.

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.

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:

@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.

这篇关于JPA主键自动生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆