facebook

Unable to Add a record in Hibernate(using sequence)

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #248135 Reply

    Sreenath Gurujala
    Participant

    Hi,
    I am gettin an error when I try to add a new record to a simple table.

    This is the log
    =========
    Caused by: net.sf.hibernate.HibernateException: Session is closed
    at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3339)
    at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:63)
    at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:58)
    at net.sf.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:62)
    at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:776)
    at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)
    at com.example.hibernate.VipService.addVipdata(VipService.java:252)

    The error, I think is occuring because Hibernate is not setting the ID(Primary key) while trying to insert the data.

    I have a sequence declared in my backend “incr_id
    If i do “select INCR_ID.nextval from dual ” it works fine..I guess there is some problem with mappings..

    mapping
    =====
    <class name=”Vipdata” table=”VIPDATA”>
    <id name=”vipid” column=”VIPID” type=”java.lang.Integer”>
    <generator class=”sequence”>
    <param name=”sequence”>incr_id</param>
    </generator>
    </id>

    here is the hibernate code
    =================
    vipdata.setViptitle(f.getViptitle());
    vipdata.setVipname(f.getVipname());
    VipService.getInstance().addVipdata(vipdata);

    Please help to fix this problem….
    Thanks in advance..

    sreenath

    #248176 Reply

    Haris Peco
    Member

    sreenath ,

    I advice that you upgrade to hibernate 3 – it is much, much better.I make your case with sequence generator and it work fine
    If you can’t upgrade, please send us mapping and complete application code for examples

    Best

    #248225 Reply

    Sreenath Gurujala
    Participant

    Hi there,
    Thanks for your early reply.I figured out the problem.The session was getting closed automatically.
    I have to check if the session is open each time i try to do some transaction.I needed to do something like this
    if (session == null || !session.isOpen()) {
    ….
    session = sessionFactory.openSession();
    threadLocal.set(session);
    }

    Thank you once again for your quick response. 😀

    Sreenath

    #248362 Reply

    Don Nelson
    Member

    I recommend that you hide all of that complexity with a delegation pattern. For example, my hibernate persistence manager has this for the save method:

    public PersistentObject save(PersistentObject persistentObject)
    throws EosException {
    BusinessObject businessObject = null;
    if (persistentObject instanceof BusinessObject) {
    businessObject = (BusinessObject) persistentObject;
    }
    Transaction tx = null;
    try {
    tx = session.beginTransaction();
    session.saveOrUpdate(persistentObject);
    tx.commit();
    }

    In this case, PersistentObject is a marker inteterface, but the logic is clear – remove the burden
    of transactions and such from your calling method, and abstract it to a simple-to-use
    save method.

    Don

    #248364 Reply

    Sreenath Gurujala
    Participant

    Hi Don
    Thank you very much.I appreciate your help.
    Sreenath

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Unable to Add a record in Hibernate(using sequence)

You must be logged in to post in the forum log in