java - 缺少序列或表 : hibernate_sequence

标签 java hibernate postgresql

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



}

最佳答案

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

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

关于java - 缺少序列或表 : hibernate_sequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14561385/

相关文章:

java - @OneToOne 单向映射中的实体映射中的重复列

数据库更新顺序

sql - 用于自定义/预定义 SQL 查询的 Web GUI

java - 如何将手动 SQL 行映射与 CrudRepository (Spring Data) 混合

python - 在 Django 中使用多个数据库,让另一个数据库包含不是由任何模型创建的表

Java套接字-客户端和服务器IP地址

java - 如何在开发模式而非生产模式下调用默认错误页面(Play Framework 2.3.4 - Java)

java - 这里如何推断通用类型?

java - Java读取txt文件第一行的第一个整数

java - 如何在 hibernate 中覆盖 transient 对象的哈希码和等于?