-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Annotations
PostPosted: Wed Jun 16, 2010 3:32 pm 
Beginner
Beginner

Joined: Mon May 10, 2010 2:00 pm
Posts: 22
I am getting this error: org.hibernate.exception.SQLGrammarException: could not get next sequence value

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

}


Top
 Profile  
 
 Post subject: Re: Hibernate Annotations
PostPosted: Thu Jun 17, 2010 12:09 pm 
Beginner
Beginner

Joined: Mon May 10, 2010 2:00 pm
Posts: 22
I have figured some things out after lots of web searching and playing around with the code. I added a SEQUENCE in the Oracle database to handle the assignment of the primary keys for the Account table. I also modified the Account class to use this sequence as the key generator. This appears to have resolved the issue

Being new to Annotations I find this interesting that, when using xml mapping files, you can assign an increment generator to the key for the table. Using that method the keys are automatically generated without the need for a sequence. When using annotations, it appears that you must take this extra step in preparing your Oracle database. I still prefer annotations over mapping files, but you would think there would be an annotation that would handle key incrementing considering that the functionality must be hidden somewhere in Hibernate for performing a "Select (max(id)+1) as next_value from my_table" sql command. If you know what I'm talking about and know a solution for this in annotations, please share.

Here is my updated Account class:
Code:
package dwd.dao.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="account")
public class Account implements Serializable {

    @Id
    @GeneratedValue(generator="ACCOUNT_ID_SEQ")
    @SequenceGenerator(name="ACCOUNT_ID_SEQ",sequenceName="ACCOUNT_ID_SEQ", allocationSize=1)
    @Column(name="account_id")
    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;

    //...getters and setters...

}


Top
 Profile  
 
 Post subject: Re: Hibernate Annotations
PostPosted: Thu Jun 17, 2010 4:45 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The increment generator is not defined in JPA, but you can use the @GenericGenerator extension in Hibernate to us it. See http://docs.jboss.org/hibernate/stable/ ... identifier for more information.


Top
 Profile  
 
 Post subject: Re: Hibernate Annotations
PostPosted: Fri Jun 18, 2010 9:57 am 
Beginner
Beginner

Joined: Mon May 10, 2010 2:00 pm
Posts: 22
nordborg wrote:
The increment generator is not defined in JPA, but you can use the @GenericGenerator extension in Hibernate to us it. See http://docs.jboss.org/hibernate/stable/ ... identifier for more information.


Thank you. That allowed me to get rid of the Oracle Sequences!

Here is my updated Account class using a @GenericGenerator:
Code:
package dwd.dao.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="account")
public class Account implements Serializable {

    @Id
    @GeneratedValue(generator="auto_increment")
    @GenericGenerator(name="auto_increment", strategy="increment")
    @Column(name="account_id")
    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;

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

}



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.