facebook

Hibernate Tutorial Error – SessionManager

  1. MyEclipse Archived
  2.  > 
  3. Documentation
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #219003 Reply

    clung
    Member

    Has anyone else noticed the problem in the Hibernate Tutorial (Dated Nov. 3, 2004) when you get to the example code (toward the end of the article) you find the following code:

    finally
    {
    SessionManager.closeSession();
    }

    The problem is this will throw an error because SessionManager.java’s closeSession method throws HibernateException, its easily fixable but I just wanted to let others know.

    #219006 Reply

    Riyad Kalla
    Member

    I have fired this off to the doc team, thanks!

    #219007 Reply

    clung
    Member

    I’ll add two more things I’ve noticed. First, unless I put a Hibernate Transaction into this code it never saves. I’ve gone through the tutorial steps twice and couldn’t get this to save. Then added code for a transaction and bang it worked first time. This could be something I am doing wrong from the the tutorial, or…

    Second thing I’ve found is that when you edit the hibernate.cfg.xml file and save it – in particular the <property name=”connection.url”> value does not update the .myhibernatedata file, thus the connection gets messed up because the two are now out of sync. Seems like a possible bug, or I am doing something wrong here?

    At any rate, once I changed the code to add in a transaction it worked fine.

    #219225 Reply

    support-jeff
    Member

    Cannot speak to the tutorial since we did not author it (as I recall, though, there were a lot of customizations the author did that went beyond what MyEclipse does for you).

    As for saving changes to hibernate.cfg.xml, as the warning in the file states – you are on your own, any changes will be overwritten. Currently the only means you have to change property settings in the config are to manually change both the config file and .myhibernatedata. Alternately, you can create a new config file (or overwrite the existing one) using the New -> J2EE -> Hibernate Configuration File wizard. This will update .myhibernatedata for you.

    #219244 Reply

    wouterroux
    Member

    Hi,

    This is probably beyond the scope of MyEclipse but maybe someone can help me.

    I also found that the Hibernate Quickstart tutorial does not save. I followed the advice to put a transaction in but then I get exception underneath. Apparently you have to put journalling on to use trasactions. How can I save without using transactions?

    thnx
    wouter

    net.sf.hibernate.JDBCException: could not insert: [com.mediclinic.hibernate.Test#1]
    at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:478)
    at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:442)
    at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)
    at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2407)
    at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2360)
    at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2229)
    at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
    at com.mediclinic.hibernate.HiberTester.main(HiberTester.java:30)
    Caused by: java.sql.SQLException: [SQL7008] TEST in A0FIL not valid for operation.
    at com.ibm.as400.access.JDError.throwSQLException(JDError.java:594)
    at com.ibm.as400.access.JDError.throwSQLException(JDError.java:565)
    at com.ibm.as400.access.AS400JDBCStatement.commonExecute(AS400JDBCStatement.java:742)
    at com.ibm.as400.access.AS400JDBCPreparedStatement.executeUpdate(AS400JDBCPreparedStatement.java:1102)
    at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
    at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:468)

    #219249 Reply

    Riyad Kalla
    Member

    This is from the hibernate forum, it might get you on the right track to investigate: http://forum.hibernate.org/viewtopic.php?t=924511&highlight=sql7008

    #219582 Reply

    hafor
    Member

    @clung wrote:

    I’ll add two more things I’ve noticed. First, unless I put a Hibernate Transaction into this code it never saves. I’ve gone through the tutorial steps twice and couldn’t get this to save. Then added code for a transaction and bang it worked first time. This could be something I am doing wrong from the the tutorial, or…

    …………………………………..

    At any rate, once I changed the code to add in a transaction it worked fine.

    I have also noticed that the msg stored through session.save() never gets stored in the DB. You don’t have to use a Hibernate transaction or a JTA transaction (even though this is more likely to be used in any serious application). You do however need to control the complete session handling yourself based on the code that is shown in the tutorial (that is the no-transaction example.)

    According to the Hibernate-reference document (version 2.1.6), you should do as follows (and this is without any transactionsupport if the tutorial wants to ignore transaction handling in the tutorial)

    
    try {
          .......
          session.save(msg);
          // You also need to flush
          session.flush();
          // as well as commit yourself 
          //  unless using transactionsupport
          session.connection().commit();
          }
          catch (Exception e) {
             // rollback of exceptions where you cant recover
             session.connection().rollback();
             // re-throw exception for exception handling to take care of
             throw e;
          }
          finally {
             // close the session through the session manager
             SessionManager.closeSession();
          }
    

    I think it is important even for simple tutorials to at least use a complete example even if it is oversimplistic in nature. The tutorial has a SessionManager class to show a pattern used for class based handling of SessionFactory and it’s configuration. Why not also show the importance of a complete session example. It is only a few extra lines. The text could also mention that you would in most cases use transaction support, but it may be out of scope for the tutorial. As it is now I for one get a negative feeling for the Hibernate support tools in MyEclipse, simply by this incomplete tutorial.

    I should also mention that I do not claim to be an expert on Hibernate, but I do know “normal” transaction handling (not only for DB’s) and I have browsed the Hibernate reference doc to find the above code (which I have modified slightly to suite the turorial sample).

    regards,
    Hans

Viewing 7 posts - 1 through 7 (of 7 total)
Reply To: Hibernate Tutorial Error – SessionManager

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