Java 表“DBNAME.hibernate_sequence”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49813666/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 03:07:55  来源:igfitidea点击:

Table 'DBNAME.hibernate_sequence' doesn't exist

javamysqlspringspring-bootspring-data

提问by en Peris

I have a SpringBoot 2.0.1.RELEASE application using spring data / jpa

我有一个使用 spring data/jpa 的 SpringBoot 2.0.1.RELEASE 应用程序

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

But when I do an update in the Amazon Aurora DB, I got this error:

但是当我在 Amazon Aurora 数据库中进行更新时,我收到了这个错误:

2018-04-13 09:20 [pool-1-thread-1] ERROR o.h.id.enhanced.TableStructure.execute(148) - could not read a hi value com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'elbar.hibernate_sequence' doesn't exist at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

2018-04-13 09:20 [pool-1-thread-1] 错误 ohid.enhanced.TableStructure.execute(148) - 无法读取高值 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'elbar .hibernate_sequence' 不存在于 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

I have this in the entity I want to save

我在要保存的实体中有这个

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

I would like to avoid also any drawback in the DB to fetch the ID .

我也想避免在数据库中获取 ID 的任何缺点。

采纳答案by Springer F

With the generation GenerationType.AUTOhibernate will look for the default hibernate_sequencetable , so change generation to IDENTITYas below :

生成 GenerationType.AUTOhibernate 将查找默认hibernate_sequence表,因此将生成更改 IDENTITY为如下:

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;

回答by Laures

Just in case you migrate from a previous boot version:

以防万一您从以前的引导版本迁移:

setting the following in your application.ymlwill prevent hibernate from looking for hibernate_sequenceentries.

在您的设置中设置以下内容application.yml将阻止休眠查找hibernate_sequence条目。

spring.jpa.hibernate.use-new-id-generator-mappings

That was the default in Boot 1.x

这是 Boot 1.x 中的默认设置

回答by Neikat

Add the following config in your application.yml:

在 application.yml 中添加以下配置:

spring: jpa: hibernate: use-new-id-generator-mappings: false

spring: jpa: hibernate: use-new-id-generator-mappings: false

Or this if you use application.properties

或者,如果您使用 application.properties

spring.jpa.hibernate.use-new-id-generator-mappings= false

spring.jpa.hibernate.use-new-id-generator-mappings= false

回答by Chris Maggiulli

JPA and Auto-DDL

JPA 和自动 DDL

When I run into mapping mismatches between a table schema and a java entity I like to do the following.

当我遇到表模式和 java 实体之间的映射不匹配时,我喜欢执行以下操作。

  1. Drop the schema
  2. Add the spring.jpa.hibernate.ddl-auto=createproperty to application.properties
  3. Restart your application
  1. 删除架构
  2. spring.jpa.hibernate.ddl-auto=create属性添加到application.properties
  3. 重新启动您的应用程序

This will now recreate the schema based on your entity. You can then compare the created table against your old schema to see the difference, if necessary.

现在,这将根据您的实体重新创建架构。然后,如有必要,您可以将创建的表与旧模式进行比较以查看差异。

Warning: This will truncate the data in all tables specified as entities in your application

警告:这将截断应用程序中指定为实体的所有表中的数据

回答by Prasanth Rajendran

If you are using Hibernate version prior to Hibernate5 @GeneratedValue(strategy = GenerationType.IDENTITY)works like a charm. But post Hibernate5 the following fix is necessary.

如果您使用的是 Hibernate5 之前的 Hibernate 版本,那么它@GeneratedValue(strategy = GenerationType.IDENTITY)就像一个魅力。但是在 Hibernate5 之后需要进行以下修复。

@Id
@GeneratedValue(strategy= GenerationType.AUTO,generator="native")
@GenericGenerator(name = "native",strategy = "native")
private Long id;

DDL

数据线

`id` BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY

REASON

原因

Excerpt from hibernate-issue

摘自hibernate-issue

Currently, if the hibernate.id.new_generator_mappings is set to false, @GeneratedValue(strategy = GenerationType.AUTO) is mapped to native. If this property is true (which is the defult value in 5.x), the @GeneratedValue(strategy = GenerationType.AUTO) is always mapped to SequenceStyleGenerator.

For this reason, on any database that does not support sequences natively (e.g. MySQL) we are going to use the TABLE generator instead of IDENTITY.

However, TABLE generator, although more portable, uses a separate transaction every time a value is being fetched from the database. In fact, even if the IDENTITY disables JDBC batch updates and the TABLE generator uses the pooled optimizer, the IDENTITY still scales better.

目前,如果 hibernate.id.new_generator_mappings 设置为 false,@GeneratedValue(strategy = GenerationType.AUTO) 将映射到 native。如果此属性为 true(这是 5.x 中的默认值),则 @GeneratedValue(strategy = GenerationType.AUTO) 始终映射到 SequenceStyleGenerator。

出于这个原因,在任何本身不支持序列的数据库(例如 MySQL)上,我们将使用 TABLE 生成器而不是 IDENTITY。

然而,TABLE 生成器虽然更便携,但每次从数据库中获取值时都使用单独的事务。事实上,即使 IDENTITY 禁用 JDBC 批量更新并且 TABLE 生成器使用池优化器,IDENTITY 仍然可以更好地扩展。