I am getting this error:
org.hibernate.exception.SQLGrammarException: could not get next sequence valueI am using Netbeans IDE 6.9 full release. I have an Account class and am trying to insert the class. The problem is that Hibernate is looking for a sequence object in my Oracle database. I do not have one. I've simply got a NUMBER(9,0) column as the primary key and I have set this as @Id and @GeneratedValue in the Account class. I even added the strategy with GenerationType.AUTO set. When I attempted to use GenerationType.IDENTITY, I received an error stating that it could not insert a null value for the account_id property, which means it did not auto-generate the primary key and map it to the account_id property like I expected.
Following is my code. I appreciate any help resolving this issue.
Here is the SQL I used to create the Account table in the Oracle databse:
Code:
create table account (
account_id number(9,0) not null primary key,
account_type number(4,0),
first_name varchar2(100),
last_name varchar2(100),
email varchar2(300),
phone_number varchar2(25),
status number(4,0),
password varchar2(50)
);
Here is my annotated class definition:
Code:
package dwd.dao.entity;
import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.Table;
@Entity
@Table(name="account", schema="testDB")
public class Account implements Serializable {
private int account_id;
private int account_type;
private String first_name;
private String last_name;
private String email;
private String phone_number;
private int status;
private String password;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getAccount_id() {
return account_id;
}
public void setAccount_id(int account_id) {
this.account_id = account_id;
}
public int getAccount_type() {
return account_type;
}
public void setAccount_type(int account_type) {
this.account_type = account_type;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
Here is my HibernateUtil class:
Code:
package dwd.dao;
import dwd.dao.entity.Account;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(Account.class);
sessionFactory = config.configure().buildSessionFactory();
}
public static Session getSession() {
return sessionFactory.openSession();
}
public static void releaseSession(Session session) {
session.flush();
session.clear();
session.close();
}
public static void handleException(Exception e, Transaction trans, Session session) {
try {
e.printStackTrace();
if (trans != null) trans.rollback();
if (e instanceof RuntimeException) {
throw new RuntimeException("Runtime type exception rethrown from HibernateUtil", e);
}
} catch (HibernateException e2) {
e2.printStackTrace();
throw new RuntimeException("Exception in HibernateUtil while handling the contained exception", e2);
} finally {
releaseSession(session);
}
}
}
Here is the code where I am creating the Account object before I send it to the Data Access Object:
Code:
Account account = new Account();
account.setAccount_type(1);
account.setEmail("me@email.com");
account.setFirst_name("John");
account.setLast_name("Dao");
account.setPassword("myPassword");
account.setPhone_number("555-555-5555");
account.setStatus(1);
boolean result = Dao.createAccount(account);
And Finally here is the code from my Dao object that attempts to persist the object:
Code:
package dwd.dao;
import dwd.dao.entity.Account;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Dao {
public static boolean createAccount(Account account) {
boolean success = true;
Session session = null;
Transaction trans = null;
try {
session = HibernateUtil.getSession();
trans = session.beginTransaction();
session.merge(account);
trans.commit();
success = true;
} catch (Exception e) {
e.printStackTrace();
}
return success;
}
}