35

我是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;
}



}
4

8 回答 8

41

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

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
于 2018-07-11T01:58:15.627 回答
27

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

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

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

@GeneratedValue(strategy=SEQUENCE, generator="employee_id_seq")
于 2013-01-28T11:55:46.827 回答
10

简单的解决方案:

创建 hibernate_sequence 表为:

"create sequence <schema>.hibernate_sequence"
于 2016-07-15T09:41:02.607 回答
1

如果您在 Spring Boot 或 Spring Boot/Hibernate 迁移中遇到这种情况,那么您可能可以尝试以下操作

https://mkyong.com/spring-boot/spring-boot-mysql-table-db_name-hibernate_sequence-doesnt-exist/
于 2020-07-07T03:00:36.187 回答
0

如果您不使用注释,您应该更改 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>
于 2014-02-07T11:36:03.927 回答
0

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

于 2020-09-26T22:57:29.527 回答
-1

You can do two things. One is to just manually create a blank hibernate_sequence table postgresql. Two, most likely there is a conflict with the user account permissions not allowing grails to create that table.

于 2015-04-24T15:19:37.257 回答
-2

对我来说,导致此错误的原因是 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>
于 2016-02-19T01:24:52.873 回答