0

I am trying to save below entity in Oracle database.

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Book {

@Id
@GeneratedValue
private int id;

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}



Book book1 = new Book();
book1.setName("Sample");
session.save(book1);

But Hibernate is generating below exception stack trace.

Caused by: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)

Also I am seeing Hibernate is trying to run below query to fetch value from sequence, which is when the exception is getting generated.

Hibernate: select hibernate_sequence.nextval from dual

Table Schema:

CREATE TABLE BOOK(
    ID INTEGER,
    NAME VARCHAR2(50)
);

ALTER TABLE BOOK ADD PRIMARY KEY(ID);
11
  • 1
    Does the sequence exist? Which user are you using to run your query? Is it the same than the sequence? If not, have you created synonyms, given grants, ... ? Commented Jun 6, 2017 at 9:01
  • Can you add @GeneratedValue(strategy = GenerationType.IDENTITY) to your id member variable and try? Also, you should declare member variables in Entity class like private Integer id; rather private int id;. Commented Jun 6, 2017 at 9:07
  • @harshavmb, still not working with the changes u suggested. Commented Jun 6, 2017 at 9:13
  • Can you tell us to which table Book is mapped to ? Also, if you could post the table schema it would be helpful Commented Jun 6, 2017 at 9:14
  • The problem is clear, you aren't setting id so hibernate is complaining. Commented Jun 6, 2017 at 9:17

2 Answers 2

1

I was able to run this program on another machine which I had been using for Hibernate programs for quite a few time. But the program failed to run on a new machine and the reason is apparent in the stack trace :

Hibernate: select hibernate_sequence.nextval from dual.

Hibernate is not able to find out the default hibernate_sequence in database. So instead of creating the sequence manually I added the property

<property name="hibernate.hbm2ddl.auto">create</property> 

in hibernate.cfg.xml and the next time I ran the program, Hibernate created this sequence and added a new row in Book table.

Sign up to request clarification or add additional context in comments.

1 Comment

Note: As @Sergio mentioned, Adding this line: <property name="hibernate.hbm2ddl.auto">create</property> to your Hibernate configuration will drop all your tables and recreate them empty. Please use this carefully
0

Adding this line: <property name="hibernate.hbm2ddl.auto">create</property> to your Hibernate configuration will drop all your tables and recreate them empty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.