-->
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.  [ 6 posts ] 
Author Message
 Post subject: Advice/best practises wanted : EntityManager/DAO/Transaction
PostPosted: Wed Dec 06, 2006 12:31 pm 
Newbie

Joined: Tue Sep 12, 2006 3:19 am
Posts: 6
hi there,

I'm looking for some best practises regarding the DAOs and their coöperation with Business Objects.

A lot of useful info is on
http://blog.hibernate.org/cgi-bin/blosx ... genericdao

but I'm missing info in the context of EntityManager/EJB3.

I'm developing a Swing application of which the data are persisted using Hibernate/EJB3.
- Data classes contain EJB annotations.
- Persistence is controlled by BusinessObjects and DAOs.
- BO's control beginning and end of a transaction

My DAOs are basically CRUD DAOs with some additional named queries.
The BOs'logic sometimes need to call the methods of different DAOs within a single transaction.
My current implementation features way too many duplicate code, so I'm interested to learn how others write similar code in a generic and elegant way.

I'm using an EntityManagerHelper, which instantiates the EntityManagerFactory and allows to create new EntityManagers.

Is it a good strategy, as featured on the website above, to create new DAOs for each call ?

What about exceptions ?
The BOs should do the rollback when an hibernate exception happens, the application should deal with the failure in the BOs.
I could rethrow the exceptions but I suppose it's clean to throw some kind of ApplicationException from within the BOs ?

I somewhere read about transaction annotations, but in my case these would have to be implemented in the BOs.
@TransactionAttribute(TransactionAttributeType.REQUIRED)
If I wouldn't use these annotations, my BOs would be filled with

EntityManagerHelper.createNewManager();
EntityManager em = EntityManagerHelper.getEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
log.debug("Start transaction");
try {
dao.insert(p);
} catch (Exception e) {
tx.rollback();
log.error("transaction rollback", e);
} finally {
em.getTransaction().commit();
em.close();
log.debug("Close transaction");
}

Currently my BOs are more or less duplactions of the methods in the DAOs.
It look kinda weird but I don't want to put transaction code in the DAOs.
Remarks on this ?


Geert[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 1:49 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
all our design effrot hase been put on EJB3 (PC propagation through session beans etc), check the EJB 3.0 design (or just use the Embeddable EJB 3 container)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 24, 2006 7:46 pm 
Newbie

Joined: Sun Dec 24, 2006 7:38 pm
Posts: 2
ZV_GP

You made some interesting points here. Im going into the same problem and I have to design a rich application in SWT.

The approach Im going to use is just as you wrote. The very same one.

I was searching for a good material on the internet regarding Generic DAOs with BOs and EJB3 when I found you post.

I would like to know if you did any progress, and if you did, please post me some links or give me some advice.

I would like to stay in touch with you too, since we are using very similar development approaches.

Thanks in advance and happy coding...


Top
 Profile  
 
 Post subject: Re: Advice/best practises wanted : EntityManager/DAO/Transac
PostPosted: Tue May 01, 2007 11:40 pm 
Newbie

Joined: Wed Mar 21, 2007 2:07 pm
Posts: 9
Location: Minnesota
Just wondering if you ever got a good answer to this posting.

I'm facing the same problem -- I'm using Hibernate (via JPA interfaces) in a stand-alone (Java SE) environment. All the Java SE examples use the Hibernate interfaces. All the JPA examples are for Java EE.

I am using an abstract class to wrap my business methods (to handle the transaction and exception handling):

Code:
public abstract class Transaction<t> {
    /**
     * Logger for this class.
     */
    private static final Log LOG = LogFactory.getLog(Transaction.class);
   
    private EntityManager em;
   
    /**
     * Constructor.
     */
    public Transaction () {
    }
   
    /**
     * @return generic type
     */
    public t execute() {
       
        // Create a new EntityManager (using my helper class)
        em = PersistenceHelper.getEntityManager(false, true);       
       
        EntityTransaction tx = null;
       
        t returnValue = null;
               
        try {
            tx = em.getTransaction();
            tx.begin();
           
            returnValue = action();
           
            tx.commit();
        } catch (RuntimeException ex) {
            try {
                tx.rollback();
            } catch (RuntimeException rbEx) {
                LOG.error("Couldn't roll back transaction", rbEx);
            }
            throw ex;
        } finally {
            em.close();
            em = null;
        }
       
        return returnValue;
    }
   
    /**
     * Only call from inside action -- otherwise returns null.
     *
     * @return EnityManager used by execute();
     */
    protected EntityManager getEntityManager() {
        return em;
    }
   
    /**
     * @return generic type
     */
    protected abstract t action();
   
}



Here is an example where I use this class:

Code:
    public Integer getWidgetCount() {
       
        Transaction<Integer> transaction = new Transaction<Integer>() {
            public Integer action() {
                WidgetDao dao = new WidgetDao(getEntityManager());
               
                return dao.countWidgets();
            }
        };
       
        return transaction.execute();
    }


I create a new DAO and EntityManager for each business operation (method).

Let me know what you've come up with. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 4:21 pm 
Newbie

Joined: Sun Dec 24, 2006 7:38 pm
Posts: 2
greta

The app I was developing has been under my desk for a while, since Im currently using JBoss Seam for a web project.

But at that time, I think I figured out how to deal with this situation.

I used a Generic DAO provided by Chris Bauer (AFAIR) which manages the entity manager, EJB3 entities (POJO) and a DAO for each entity.

I use JBoss 4.0.5 with EJB3 profile, so my SWT interface uses RMI to talk with the server. But thats quite simple to do it.

I ended up with a simple working example, and that structure gave me a nice clear separation of concerns.

Unfortunately, I did stop developing the app. But I still have that code. I can post here later if you want. Just need to go back to my office ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 4:33 pm 
Newbie

Joined: Wed Mar 21, 2007 2:07 pm
Posts: 9
Location: Minnesota
marcelo-

Please post your code - that would be great.

Do you generate any of your code? Do you generate your DB schema?

Thanks!


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