缺少序列或表:hibernate_sequence

2022-09-01 05:45:18

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



}

答案 1

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

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

答案 2

您尚未发布重要的部分:类。Employee

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

您可以提供序列名称作为生成值批注的一部分。例如。

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

推荐