- This topic has 7 replies, 5 voices, and was last updated 19 years, 1 month ago by
shameel.
-
AuthorPosts
-
TringleMemberIn 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?March 8, 2006 at 10:37 pm #248110
Riyad KallaMemberThis 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.
March 8, 2006 at 11:05 pm #248119
TringleMemberYes 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?March 9, 2006 at 2:11 am #248128
TringleMemberAre the following two ways the ssame?
…..
Transaction tx=session.beginTransaction();
…..
tx.commit();and
……
session.currentSession().connection().commit();March 9, 2006 at 3:32 am #248134
Haris PecoMemberAre 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
March 11, 2006 at 2:24 pm #248363
Don NelsonMemberWell, the first recommendation would be to get rid of the “classic.Session” and just use that. Which Session are you using?
March 12, 2006 at 7:34 am #248372
Haris PecoMemberI 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.SessionSession#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 objectBest
May 23, 2006 at 2:03 am #252398
shameelMemberHi dear friends,
From Hibernate3.0 onwards a transaction will be automatically committed.No need to bother about it.
Rgds,
Shameel -
AuthorPosts