当使用strategy = GenerationType.IDENTITY时,Hibernate会为ID插入null
为什么 Hibernate 在表中插入 ID 为空值。
@Id
@Getter
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
这是我在表 blabla 中插入内容时收到的消息 ↓↓↓
Hibernate:插入 blabla (id, bla1, bla2) 值 (null, ?, ?)
但是,当使用 strategy = GenerationType.IDENTITY
→ strategy = GenerationType.AUTO/SEQUANSE
时代码> 我收到这条消息 ↓↓↓
Hibernate:调用 hibernate_sequence 的下一个值
Hibernate:插入 blabla (bla1, bla2, id) 值 (?, ?, ?)
有谁知道为什么会这样,因为 ID 位置的 null 在连接到数据库时导致异常。
更新:
我已经使用strategy = SEQUENCE和AUTO
运行我的应用程序,但我还得到了一个异常
数据库是H2
解决方案:
您转到 application.properties
文件并添加以下代码行:
hibernate.hbm2ddl.auto=validate
Why is Hibernate inserts null value for the ID in the table.
@Id
@Getter
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
This is the Message I am getting when inserting somthing in table blabla ↓↓↓
Hibernate: insert into blabla (id, bla1, bla2) values (null, ?, ?)
However when instad of using strategy = GenerationType.IDENTITY
→ strategy = GenerationType.AUTO/SEQUANSE
I am getting this message ↓↓↓
Hibernate: call next value for hibernate_sequence
Hibernate: insert into blabla (bla1, bla2, id) values (?, ?, ?)
Does anyone know why it's like that because this null in ID place is causing an Exception when connecting to a database.
Update:
I've run my Application with strategy = SEQUENCE and AUTO
but I am getting also an Exception
Database is H2
Solution:
you go to your application.properties
file and add this line of code:
hibernate.hbm2ddl.auto=validate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这两个命令在 Hibernate ORM 5.6.5.Final 中得到了修复,旧版本由于某些历史原因会产生无效的 SQL,它被旧版本的 H2 接受,但新版本会抛出预期的异常。
该版本还具有 H2 最新版本所需的各种其他更改。
您需要将
hibernate-core
升级到此版本或更新版本。Both these commands were fixed in Hibernate ORM 5.6.5.Final, older versions produce invalid SQL for a some historic reason, it was accepted by old versions of H2, but new versions throw an expected exception.
This version also has various other changes needed for recent versions of H2.
You need to upgrade
hibernate-core
to this or newer version.也许您的表尚未定义为自动增量 PK。
尝试将此添加到 application.properties:
Maybe your table hasn't been defined as an auto incremental PK.
Try to add this one to application.properties: