facebook

Database returned no natively generated id value [Closed]

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #219603 Reply

    Kevin_L
    Member

    I am using the latest release of MyEclipse 3.8.2 with Eclipse 3.0 on WinXP.
    Using JVM 1.4.2_06 and Tomcat 5.0 with MySQL 4.1

    First of all I was able to fix ( Session is closed ) error because of one of the posting here. Someone pointed out that the code that is created from Hibernate Wizard has a flaw. I had to add
    if(session == null || session.isOpen() == false)
    in currentSession() method.

    well I have two new problems.

    1) After I commit the codes to my CVS repository and downloaded them to my laptop which has the same software configuration, it give me JDBC Exception error: Cannot open connection. I doubled check hinerbate configuration files and .myhinernatedata file to make sure that they all have the correct information. Every files are the same. This is pretty strange. The only different is that I started the project with my office pc ( that is create DB exlpore profile ), add hibernate jar files and so on. I also checked my MySQL db and it has the same privileges user account and database as my office PC. I also created the same DB explore profile on my laptop. I even created new hibernate configuration file…It still doesn’t work…. Anyone know what is going on? May be someone already had the same experience before……I am newbie to hibernate.

    2) Ok Back to my office PC……ok on my office PC, I can display the data fine. But when I try to add new data. it give me this error. What is that mean? I put all the debug statement in ContactService.java and found out that the error exception is happening in addContact( Contact data ) method. The method is implemented as follow. Should I add session.connection().commit(); and if the data doesn’t save, do session.connection().rollback();

    Thanks in advance!
    K~

    public void addContact(Contact data)
    {

    Session session = null;

    try
    {
    session = SessionFactory.currentSession();
    session.save(data);
    session.flush();
    }
    catch (HibernateException e)
    {
    System.err.println(“error from addContact….Hibernate Exception” + e.getMessage());
    throw new RuntimeException(e);
    }
    /*
    * Regardless of whether the above processing resulted in an Exception
    * or proceeded normally, we want to close the Hibernate session. When
    * closing the session, we must allow for the possibility of a Hibernate
    * Exception.
    *
    */
    finally
    {
    if (session != null)
    {
    try
    {

    session.close();
    }
    catch (HibernateException e)
    {
    System.err.println(“session error from addContact..Hibernate Exception” + e.getMessage());
    throw new RuntimeException(e);
    }

    }
    }

    }

    javax.servlet.ServletException: net.sf.hibernate.HibernateException: The database returned no natively generated identity value
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    root cause

    java.lang.RuntimeException: net.sf.hibernate.HibernateException: The database returned no natively generated identity value
    org.rcsb.pdb.cma.hibernate.ContactService.addContact(ContactService.java:267)
    org.rcsb.pdb.cma.action.AddContact.execute(AddContact.java:51)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    #219615 Reply

    Riyad Kalla
    Member

    Kevin,
    Few things:

      1) I can’t help with the home PC question, I don’t know any more than you why its not working.
      2) When doing operations (like create/update/delete) that change the DB, its usually a good idea to get into the habit of using transactions:

      
      session = SessionFactory.currentSession();
      Transaction transaction = session.beginTransaction();
      session.save(data);
      transaction.commit();
      session.close();
      

      This will ensure the data is written out safely.

      3) As far as your exception is going, make sure that your PK fields in your Contact table ARE infact marked with ‘autoincrement’ flag so a native ID can be returned.

    #219637 Reply

    Kevin_L
    Member

    Riyad,

    Thanks for your advice!
    I started using Transaction…
    And your suggestion sure did solve the problem. I had to marked with autoincrement on that contact table

    Thanks again! 🙂
    K~

    #219646 Reply

    Riyad Kalla
    Member

    No problem, glad it worked!

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Database returned no natively generated id value [Closed]

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