-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Persisting an object with hibernate 3
PostPosted: Sat Jun 03, 2006 3:05 pm 
Beginner
Beginner

Joined: Tue Nov 09, 2004 12:22 pm
Posts: 44
Hibernate version:
3.1.3

I'm following the hibernate reference to save an object :
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();


But the problem is that there is no getTransaction() method associated with the Session object (org.hibernate.Session).

What do you think about the following code :
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.flush();
session.close();


Do I have to close the session each time I try to access the DB?

I looked in the hibernate3.jar file and it seems there is no getTransaction method in the Session class.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 03, 2006 7:27 pm 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
Quote:
But the problem is that there is no getTransaction() method associated with the Session object (org.hibernate.Session).

Are you positive that you didn't import org.hibernate.classic.Session by any chance? Netbeans offers that first, if you use code assist, for example.
org.hibernate.classic.Session does not have the getTransaction() method, but org.hibernate.Session does.

Quote:
Do I have to close the session each time I try to access the DB?

I guess that pretty much depends on how you use Hibernate. You don't even have to bother with starting transactions in a managed environment.
If you are not in a managed environment, I think by default commit and rollback will close the session for you. You can change that behaviour, though.
See the API doc for ThreadLocalSessionContext, which is the default CurrentSessionContext.
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/context/ThreadLocalSessionContext.html
Roland


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 04, 2006 6:59 am 
Beginner
Beginner

Joined: Tue Nov 09, 2004 12:22 pm
Posts: 44
Finally, the problem was my hibernate3.jar file was not a good one. It has been imported by MiddleGenIDE plugin. I replaced it with the jar file from hibernate package download from the site.

As regards the persistance of objects, what method do you recommend when I want to save them to the DB for example.

I looked in the reference about managed environments but I don't understand what it is exactly and I don't know if I'm using such an environment...

I created a HibernateUtil class as explained in the hibernate reference.
If I don't have to bother with starting a transaction, how can I proceed?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 04, 2006 8:10 am 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
speedster wrote:
As regards the persistance of objects, what method do you recommend when I want to save them to the DB for example.

I looked in the reference about managed environments but I don't understand what it is exactly and I don't know if I'm using such an environment...


A managed environment is a J2EE application server, where you can have EJB-s with declarative transactions. I think you would know if you used them. :)
Quote:
I created a HibernateUtil class as explained in the hibernate reference.
If I don't have to bother with starting a transaction, how can I proceed?


I'm afraid you will have to bother with transactions. See the reference or your own previous post for an example. If you have the correct jar, the example should work fine. If it is not working, you will have to be more specific about the problem you experience.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 04, 2006 8:51 am 
Beginner
Beginner

Joined: Tue Nov 09, 2004 12:22 pm
Posts: 44
szroland wrote:
A managed environment is a J2EE application server, where you can have EJB-s with declarative transactions. I think you would know if you used them. :)

Indeed, you're right! I'm not using a managed environment. I'm developing a standalone application.
Quote:
I'm afraid you will have to bother with transactions. See the reference or your own previous post for an example. If you have the correct jar, the example should work fine. If it is not working, you will have to be more specific about the problem you experience.


Thank you. That's the way I will proceed. Now I'm having the correct jar, I can access the getTransaction() method.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.