Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

java - Missing sequence or table: hibernate_sequence

I am new to hibernate and postgres. Actually I am trying to map potgres database using Hibernate. This is my table stucture in 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 )
)

I am trying to add a record to the database using the following code

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

When I execute the code I am getting the following error

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

I have the sequence called "employee_id_seq" in my database. But I dont know why the database is looking for hibernate_seq. Could someone explain the error and the reason.

Thanks in advance!

Added info

This is my employee class

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



}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your domain or Model object annotate the id field as below and it should work. For me the GenerationType.AUTO failed

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...