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: Help: Hibernate and Swing
PostPosted: Wed Dec 10, 2003 8:40 pm 
Newbie

Joined: Wed Dec 10, 2003 3:44 pm
Posts: 12
I'm trying to build a small Swing application to learn Hibernate, but all samples I found in the web are related to Hibernate use with web applications.
So I need some guidelines to move forward.

I understand that I need a static SessionFactory, but what about Session ?

I had a look at the code provided by nvolpini in the page:
http://forum.hibernate.org/viewtopic.php?t=925108
and I have some doubts.

1) From these two methods:

Code:
   public User selectAndClose(int id) throws HibernateException {
        User res = null;
        Session session = null;
        try {
            session = factory.openSession();
            res = (User) session.load(User.class, new Integer(id));
   } catch (net.sf.hibernate.ObjectNotFoundException h1) {
   throw h1;
        } catch (HibernateException h2) {
            throw h2;
        } finally {
            factory.closeSession(session);
        }
      return res;
    } //select(int)

public User select(int id) throws HibernateException {
        User res = null;
        Session session = null;
        try {
            session = factory.openSession();
            res = (User) session.load(User.class, new Integer(id));
   } catch (net.sf.hibernate.ObjectNotFoundException h1) {
   throw h1;
        } catch (HibernateException h2) {
            throw h2;
        }
      return res;
    } //select(int)



it seems that I should always open a session. Then I can close it at once (first method) or leave the session open (second method).
I suppose that the difference is that:
- in the first case I can access just the data of the retrieved User object
- in the second case, after the method returns the user object, I am able to browse the other objects that have some relationship with the User class. For example, I could do a: user.getGroups().

Is this correct ?

2) From this method:

[code] public User selectFull(int id) throws HibernateException {
Session session = null;
try {
session = factory.openSession();
User res = (User) session.load(User.class, new Integer(id));

//Inicializa os grupos
Hibernate.initialize( res.getGroups() );

//Inicializar as empresas
Hibernate.initialize( res.getCompanies() );

Iterator it = res.getGroups().iterator();
while (it.hasNext()) {
Group group = (Group) it.next();

//Inicializa as permiss


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 10:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The swing GUI uses an Event thread so that all GUI processing is performed in the one thread (if your not doing this then you are breaking the model). Having said that you should only ever have one session open since it is serialised by the GUI. (Yes you can lauch threads for doing database updates but you will need worker threads to queue any gui changes when the asynchronous operation has completed.)

General guideline is to open a session from the factory for each unit of work (which is [mostly] equal to a transaction when we talk about Swing). Please don't just leave sessions open. If you are using the Swing GUI as a multiple-tier architecture rather then using a client-server senario then your issues are very similar to the web. I would suggest as a very start forget Swing/Web or any other client other than a basic console and try out a few hibernate operations. Once you get them working move to the swing client.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 10:54 pm 
Newbie

Joined: Wed Dec 10, 2003 3:44 pm
Posts: 12
david wrote:
The swing GUI uses an Event thread so that all GUI processing is performed in the one thread (if your not doing this then you are breaking the model). Having said that you should only ever have one session open since it is serialised by the GUI.

Does this mean that I should have a static or singleton session object for my the whole application life ?
Isn't there any risk that the session gets closed against my will ?
Or do you mean that I should care of not opening a new session until the previous one get closed ?

david wrote:
General guideline is to open a session from the factory for each unit of work (which is [mostly] equal to a transaction when we talk about Swing). Please don't just leave sessions open.

Ok.

david wrote:
If you are using the Swing GUI as a multiple-tier architecture rather then using a client-server senario then your issues are very similar to the web. I would suggest as a very start forget Swing/Web or any other client other than a basic console and try out a few hibernate operations. Once you get them working move to the swing client.

I already played with a console app and succeeded in doing everything when using a single session. But if I still have the doubts I listed on my first post about dealing with persisted objects retrieved by different sessions. Can you please give a short answer to each one ?

Thanks a lot for your attention!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 11:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Hi,

There is nothing wrong with having multiple sessions being used at the same time. Just has to be different threads. Unless you use threads to do the work for you swing serialises the actions - this may or may not be appropriate. Once you start to use threads to do processing in your swing application then you need to be carefull. Also note that the hibernate session object is not thread safe. This is why many users use the thread local pattern to manage this issue in a multi-threaded environment. To answer one question, don't use a static session or singleton if anything use the thread local pattern if you have multiple threads.

Answers (brief):
1) Open a session at the start of your unit of work (which may or maynot cross mulitple data operation boundaries) then close the session at the end. Your likely to be using a JDBC connection so make sure you start and commit the transaction appropriately.

2) Collection initialisation is only necessary if your using lasy loading. Where you do require the data then yes you will need to initialise it if you not accessing it while under the control of the open session. The use of proxies and lazy loading of collections is a design issue - you know your data and how you plan to use it.

3) Yes session update, or 2.1 use appropriate session.lock() [see docs]

4) Don't leave your sessions open and use a thread local so then the same object cannot be accessed by different open sessions.

5) No session factory will not close them. Use finally blocks to close the session. If you dont close the session then you have a database resource leak. Not good.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 8:01 am 
Newbie

Joined: Wed Dec 10, 2003 3:44 pm
Posts: 12
David,
thanks a lot for your help!


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.