java - 为什么 Hibernate 在使用策略 GenerationType.IDENTITY 时尝试访问 hibernate_sequence?
问题描述
在我的项目中,我将 MySQL 与 Hibernate 和 JPA 一起使用。我的实体都用@ID @GeneratedValue(strategy = GenerationType.IDENTITY)
.
hibernate_sequence
尽管如此,当将新对象插入数据库时,Hibernate 会尝试从表中选择下一个 ID - 这显然失败了。
我检查了整个项目是否在某个实体上意外使用了不同的生成类型,但事实并非如此。还有什么可能导致这个问题?
这是我用来创建表的 SQL:
create table versioned_string_attribute (
id int primary key auto_increment not null,
timestamp timestamp,
value text
);
create table backend_address (
id int primary key auto_increment not null,
street_id int references versioned_string_attribute(id),
house_number_id int references versioned_string_attribute(id),
zip_code_id int references versioned_string_attribute(id),
town_id int references versioned_string_attribute(id),
addition_id int references versioned_string_attribute(id)
);
这是我的application.properties
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.driverClassName=com.mysql.jdbc.Driver
解决方案
spring.jpa.hibernate.use-new-id-generator-mappings= # 是否对 AUTO、TABLE 和 SEQUENCE 使用 Hibernate 较新的 IdentifierGenerator。
所以我通过添加解决了这个问题spring.jpa.hibernate.use-new-id-generator-mappings=false
有关此的更多详细信息:https ://vladmihalcea.com/from-jpa-to-hibernates-legacy-and-enhanced-identifier-generators/
推荐阅读
- android - 从后台服务中检索手机信号塔信息
- javascript - Braintree 集成 - 无法在服务器端代码中访问 javascript nonce
- sql - 得到以下查询的错误答案
- git - 如何避免在git中合并狐步
- macos - 在 Mac 上将“/usr/lib”添加到 clang 链接器搜索路径中
- mysql - 无法使用 R 将 R 数据帧写入 mysql 表
- javascript - 如何检查是否是DataTable的最后一页?
- neural-network - 创建一个 3 层神经网络,使权重矩阵正交?
- ruby-on-rails - 如何在 Linux+Apache 环境下安装 Easy Redmine?
- docusignapi - 是否有通过 API 调用签署文档的规定