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]