【问题标题】:Missing sequence or table: hibernate_sequence缺少序列或表:hibernate_sequence
【发布时间】:2013-01-11 18:04:31
【问题描述】:

我是 hibernate 和 postgres 的新手。实际上我正在尝试使用 Hibernate 映射 potgres 数据库。这是我在 postgresql 中的表结构

CREATE TABLE employee
(
id serial NOT NULL,
firstname character varying(20),
lastname character varying(20),
birth_date date,
cell_phone character varying(15),
CONSTRAINT employee_pkey PRIMARY KEY (id )
)

我正在尝试使用以下代码向数据库添加记录

 System.out.println("******* WRITE *******");
    Employee empl = new Employee("Jack", "Bauer", new Date(System.currentTimeMillis()), "911");
    empl = save(empl);



 //This is the save function

    private static Employee save(Employee employee) {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();

    session.beginTransaction();


    int id = (Integer) session.save(employee);
    employee.setId(id);

    session.getTransaction().commit();

    session.close();

    return employee;
}

当我执行代码时出现以下错误

org.hibernate.HibernateException: Missing sequence or table: hibernate_sequence
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.tcs.com.Hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at org.tcs.com.Hibernate.HibernateUtil.<clinit>(HibernateUtil.java:8)
at org.tcs.com.Hibernate.MainApp.list(MainApp.java:51)
at org.tcs.com.Hibernate.MainApp.main(MainApp.java:17)
Caused by: org.hibernate.HibernateException: Missing sequence or table: hibernate_sequence
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1282)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:498)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1740)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1778)
at org.tcs.com.Hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
... 3 more

我的数据库中有名为“employee_id_seq”的序列。但我不知道数据库为什么要寻找hibernate_seq。有人可以解释错误和原因。

提前致谢!

添加信息

这是我的员工班

import java.sql.Date;

public class Employee {

private int id;

private String firstname;

private String lastname;

private Date birthDate;

private String cellphone;

public Employee() {

}

public Employee(String firstname, String lastname, Date birthdate, String phone) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.birthDate = birthdate;
    this.cellphone = phone;

}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public Date getBirthDate() {
    return birthDate;
}

public void setBirthDate(Date birthDate) {
    this.birthDate = birthDate;
}

public String getCellphone() {
    return cellphone;
}

public void setCellphone(String cellphone) {
    this.cellphone = cellphone;
}



}

【问题讨论】:

  • 我会说 Hibernate 不知道您正在使用序列。它似乎想使用它自己的本土(慢)序列表。应该有某种注释告诉 Hibernate 你的 id 列链接到一个序列。
  • @Lakshmi 你解决了这个问题吗?

标签: java hibernate postgresql


【解决方案1】:

您还没有发布重要的部分:Employee 类。

但我的猜测是您的 Employee 类正在使用 @GeneratedValue() 而不指定要使用的序列。因此,Hibernate 使用其默认名称:hibernate_sequence。

您可以提供序列名称作为 GeneratedValue 注释的一部分。例如。

@GeneratedValue(strategy=SEQUENCE, generator="employee_id_seq")

【讨论】:

  • 我没有在我的员工类中使用任何注释。我现在在我的问题中添加了员工类
  • 这里也一样,我只在由我的架构的所有 126 个实体扩展的 AbstractEntity 中使用一个 GeneratedValue(strategy = GenerationType.AUTO)。这适用于 MySQL、H2 和 HSQLDB。现在我的 Oracle 模式每个表都有一个序列,所以我想我需要为 Oracle 案例复制所有模型实体的复制粘贴。叹息……
  • 根据你的数据库,你也可以指定@Generated(GenerationTime.INSERT)来代替
【解决方案2】:

如果你不使用注解,你应该更改 YourClass.hbm.xml 文件。

您的 ID 部分应为:

<id name="id" type="int" column="id">
     <generator class="sequence">
         <param name="sequence">employee_id_seq</param>
     </generator>
</id>

文件示例:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE">
      <meta attribute="class-description">
         This class contains the employee detail. 
      </meta>
      <id name="id" type="int" column="id">
         <generator class="sequence">
             <param name="sequence">employee_id_seq</param>
         </generator>
      </id>
      <property name="firstName" column="first_name" type="string"/>
      <property name="lastName" column="last_name" type="string"/>
      <property name="salary" column="salary" type="int"/>
   </class>
</hibernate-mapping>

【讨论】:

    【解决方案3】:

    你可以做两件事。 一种是手动创建一个空白 hibernate_sequence 表 postgresql。 第二,很可能与不允许 grails 创建该表的用户帐户权限发生冲突。

    【讨论】:

      【解决方案4】:

      对我来说,导致此错误的是 MySql.Data 库的错误版本。

      我在 web.config 中定义了 6.9.6.0 版本,但实际引用的版本较旧。

      我刚刚注释掉了:

           <system.data>
            <DbProviderFactories>
            <remove invariant="MySql.Data.MySqlClient" />
              <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
           </DbProviderFactories>
         </system.data>
      

      【讨论】:

        【解决方案5】:

        简单的解决方案:

        创建 hibernate_sequence 表为:

        "create sequence <schema>.hibernate_sequence"
        

        【讨论】:

          【解决方案6】:

          在您的域或模型对象中,如下注释 id 字段,它应该可以工作。对我来说,GenerationType.AUTO 失败了

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

          【讨论】:

          • 您能说出 GenerationType.AUTO 失败的原因吗?
          • 我会警告大家,这个答案会起作用,但会对 Hibernate 造成巨大的性能损失。这将在每次插入时进行两次写入:一次确定 ID 是什么,然后执行实际的持久性。真正的答案是@David Lavender 提供的答案
          【解决方案7】:

          如果您在使用 Spring Boot 或 Spring Boot/Hibernate Migration 时遇到此问题,那么您可能可以尝试以下方法

          https://mkyong.com/spring-boot/spring-boot-mysql-table-db_name-hibernate_sequence-doesnt-exist/
          

          【讨论】:

            【解决方案8】:

            除了创建具有列 next_val 的表 hibernate_sequence 您还可以设置 quarkus.hibernate-orm.database.generation = drop-and-create。请注意,这将删除您数据库中的所有记录。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-11-12
              • 2022-01-01
              • 1970-01-01
              • 2019-11-29
              • 1970-01-01
              • 2014-08-30
              • 1970-01-01
              • 2019-01-04
              相关资源
              最近更新 更多