-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate in Action: DAO pattern for standalone j2se java
PostPosted: Wed Jun 01, 2005 8:13 pm 
Newbie

Joined: Sat Apr 23, 2005 10:50 pm
Posts: 17
Hi, I have just finished the book (Hibernate in action), and it was
great!
One chapter that I liked most was the one about DAO patterns.
Anyway, the DAO pattern is for j2ee as said in the book.
But can you use it in standalone j2se java applications as well?
The reason I am asking is because in the book the DAO methods
don't close the sessions.
I think that the HibernateUtil class is made to do it automatically,
correct me if I am wrong.
Can I copy everything as the book including
HibernateUtil and letting HibernateUtil close the session without worrying about it ,for j2se or not?

Here below is my code for the CustomerDAO class that I have just
implemented.
It is meant to retrieve a customer given the phone number.
Here I am closing the session.
Code:

public Customer getCustomer(String phone) throws Exception {
    Transaction tx = null;
    Session session = null;
     try{
       session = HibernateSession.currentSession();
       tx = session.beginTransaction();
          Iterator iter = session.iterate("from Customer customer order by customer");
          while(iter.hasNext()){
              Customer customer = (Customer)iter.next();
             if(customer.getPhone().equals(phone)){
                return customer;
             }
          }
          tx.commit();
       }
       catch (Exception e) {
           if(tx != null) {
            try{
               tx.rollback();
            }
            catch(Exception he){
                 he.printStackTrace();
                    throw e;
            }
          }
          throw e;
       }
       finally{
             try{
                 session.close();
            }
            catch(Exception es){
                 throw es;
            }
      }
      throw new Exception();
   }


So two questions , I repeat.
1) Can I copy the HibernateUtil, UserDAO (and the other related classes)
idea straight to a j2se application and leaving the closing of a session
for the HibernateUtil class.
2) The customer method is that I have provided with this mail, is that a
correct way of doing it safely?
In case I don't want to use the HibernateUtil idea, depending on the
different scenarios that I may encounter.

Thanks for any help!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 5:03 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Of course it also runs outside of a J2EE appserver.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 5:33 pm 
Newbie

Joined: Wed Sep 03, 2003 11:30 pm
Posts: 5
Location: Minneapolis, Minnesota, USA
Note sections 2.3.3 and 2.3.3 (pages 45 - 50) which discuss configuration in a non-managed versus a managed configuration. You are discussing working in a non-managed configuration.

DAO's work well in either configuration, and are helpful (IMHO) even if your are not using Hibernate.


Top
 Profile  
 
 Post subject: Re: Hibernate in Action: DAO pattern for standalone j2se jav
PostPosted: Fri Jul 01, 2005 10:34 am 
Newbie

Joined: Wed Jun 22, 2005 11:09 am
Posts: 3
Location: Ireland
sinoea wrote:
But can you use it in standalone j2se java applications as well?



Quick Answer: Yes - Data Access Objects are valid with both J2EE and J2SE.

More detailed answer:

Part of the confusion with the DAOs is that it's generally known as a Core J2EE Design Pattern:

http://java.sun.com/blueprints/corej2ee ... bject.html


This tends to imply that its only for J2EE - which is untrue.


The other area of confusion is that you can use the DAO pattern with a wide range of Java persistence technologies - Hibernate of course (see http://www.codefutures.com/hibernate), but also JDBC, EJB CMP, etc

_________________
PJ Murray
CodeFutures Software
http://www.codefutures.com/hibernate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.