未找到序列“HIBERNATE_SEQUENCE”; SQL语句

时间:2016-10-01 13:57:31

标签: spring hibernate jpa hbm2ddl

在我的春季mvc应用程序中,我有以下对象。我正在尝试使用我的应用中的devtool来显示数据。

@Entity
@Data
public class ConsultationRequest {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private String email;

    private String purpose;

    private String programme;

    private int year;

    private String language;

    private String comments;
    @Enumerated(EnumType.STRING)
    private ConsultationStatus status;
}

然后我用jpa来制作实体:

@Repository
public interface ConsultationRequestRepository extends JpaRepository<ConsultationRequest, Long> {

}

问题是当我加载我的应用程序时,我遇到了2个错误:

 Unsuccessful: drop sequence hibernate_sequence
[36morg.hibernate.tool.hbm2ddl.SchemaExport  Sequence "HIBERNATE_SEQUENCE" not found; SQL statement:

然后当我打开

http://localhost:8080/h2-console/

我看不到桌子。 似乎在启动过程中,表没有制作。

6 个答案:

答案 0 :(得分:31)

更新您的代码,如下所示:

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

由于您尚未指定序列表名,因此hibernate将查找名为 hibernate_sequence 的序列表,并将其用作默认

对于Oracle / Postgres,使用的增量字段是序列表 在MySql中,有增量字段自动递增。

答案 1 :(得分:4)

检查persistence.xml

property name="hibernate.hbm2ddl.auto" value="create"

不是hdm2ddl

这适用于我的情况。

答案 2 :(得分:2)

在属性文件中设置以下属性可帮助我解决休眠5.4v的hibernate_sequence问题

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

答案 3 :(得分:0)

如果您将第二个缓存与Liquidbase一起使用,则必须将序列添加到更改日志中,如下所示:

<changeSet author="liquibase-docs"
    id="createSequence-example">
    <createSequence catalogName="cat" cycle="false"
        incrementBy="1" ordered="true" schemaName="public"
        sequenceName="hibernate_sequence" startValue="0" />
</changeSet>

答案 4 :(得分:0)

如果有人通过 Spring Boot测试(使用H2)遇到此错误,我们必须在application.properties(或我们使用的任何配置文件)中使用以下代码:

spring.jpa.hibernate.ddl-auto = create-drop

答案 5 :(得分:0)

对于Mysql:

不加自增,修改你的表:

ALTER TABLE table_name MODIFY COLUMN id BIGINT AUTO_INCREMENT=1

相关问题