facebook

About Hibernate3.1.2

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #248101 Reply

    Tringle
    Member

    In the Hibernate_reference’s Chapter1 there are some codes like the following:

    private void addPersonToEvent(Long personId, Long eventId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    Person aPerson = (Person) session.load(Person.class, personId);
    Event anEvent = (Event) session.load(Event.class, eventId);
    aPerson.getEvents().add(anEvent);
    session.getTransaction().commit();
    }

    But in my environment (Eclipse3.1.2+Myeclipse4.1.1+Hibernate3.1.2) it says that the method getTransaction() in the line “session.getTransaction().commit();” can not be found.
    Who can tell me why?

    #248110 Reply

    Riyad Kalla
    Member

    This was most likely a change in Hibernate 3.1 and the guide may have been written with Hibernate 3.0 in mind. IIRC beginTransaction returns your transaction, just keep a refrence to it and commit it on the last line.

    #248119 Reply

    Tringle
    Member

    Yes I know it can change the codes like the following:

    private void addPersonToEvent(Long personId, Long eventId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx=session.beginTransaction();
    Person aPerson = (Person) session.load(Person.class, personId);
    Event anEvent = (Event) session.load(Event.class, eventId);
    aPerson.getEvents().add(anEvent);
    tx.commit();
    }

    to avoid the error,
    but in the Hibernate 3.0.5 it’s also can not be found.
    What’s the different of the two way?

    #248128 Reply

    Tringle
    Member

    Are the following two ways the ssame?
    …..
    Transaction tx=session.beginTransaction();
    …..
    tx.commit();

    and

    ……
    session.currentSession().connection().commit();

    #248134 Reply

    Haris Peco
    Member

    Are the following two ways the ssame?
    …..
    Transaction tx=session.beginTransaction();
    …..
    tx.commit();

    and

    ……
    session.currentSession().connection().commit();

    it isn’t same.It isn’t recommended that you use pure jdbc transaction with hibernate (it can work, maybe)

    Second, you have 2 Session (org.hibernate and org.hibernate.classic) – if you haven’t “session.getTransaction().commit();” you access to classic Session or hibernate 3.0 libraries

    MyEclipse doesn’t support hibernate 3.1, for now

    Best

    #248363 Reply

    Don Nelson
    Member

    Well, the first recommendation would be to get rid of the “classic.Session” and just use that. Which Session are you using?

    #248372 Reply

    Haris Peco
    Member

    I advice tou that use org.hibernate.Session (org.hibernate.classic.Session is deprecated and it have compatibilty method for hibernate 2 – for example, find,SaveOrUpdate etc).
    You can see your import ..Session and change session to org.hibernate.Session

    Session#getTransaction() is added in hibernate 3.1 .It return curently transaction (you make it with beginTransaction)

    in hibernate 3.0 you have to do next :

    Transaction tx = session.beginTransaction
    .. do any work
    tx.commit; // or tx.rollback();

    in hibernate 3.1 you can do next

    session.beginTransaction();
    … do work
    session.getTransaction().commit(); // or session.getTransaction.rollback();

    if you don’t call beginTransaction then getTransaction return null

    Code is same (do same thing).getTransaction is newer helper method only (you haven’t to remember transcation, but it’s same)

    for example (in 3.1)

    Transaction tx = session.beginTransaction
    … do work
    Transaction tx1 = session.getTransaction();
    // it this point tx and tx1 is same object

    Best

    #252398 Reply

    shameel
    Member

    Hi dear friends,

    From Hibernate3.0 onwards a transaction will be automatically committed.No need to bother about it.

    Rgds,
    Shameel

Viewing 8 posts - 1 through 8 (of 8 total)
Reply To: About Hibernate3.1.2

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