views:

594

answers:

2

Can anyone suggest me a DAO implementation for a web application?

What will be the problem if I create a transaction for fundamental operation (e.g. findByID(), findALL(), creatObject(), deleteObject(), etc.)?

Please suggest me a DAO implementation that supports lazy operations.

+1  A: 

If you use Hibernate Tools to generate your code the basic DAOs will be automatically generated for you. You can build upon them.

Anyway, some code snippet I use for transaction:

public void executeTransaction(Object[] parameters, Transact transact) throws ApplicationException
{
    Transaction tx = null;
    try
    {
        tx = HibernateSessionFactory.getSession().beginTransaction();
        transact.execute(parameters, tx);
        tx.commit();
        LOG.trace("executeTransaction() success");
    }
    catch (Exception e)
    {
        rollback(tx);
        throw new ApplicationException(e);
    }
}

private void rollback(Transaction tx) throws ApplicationException
{
    LOG.warn("rollback()");
    if (tx != null)
    {
        try
        {
            tx.rollback();
        }
        catch (Exception ex)
        {
            LOG.error("rollback() failure",ex);                
        }
    }
}

public interface Transact
{
    public void execute(Object[] parameters, Transaction tx) throws Exception;
}

void updateDistrictImpl(final Distretto district) throws ApplicationException, ApplicationValidationException
{    
try
{
    LOG.trace("updateDistrict[" + distrettoToString(district) + "]");

    executeTransaction(new Transact() {
        public void execute(Object[] parameters, Transaction tx) throws ApplicationException
        {
            DistrettoHome DistrettoDAO = new DistrettoHome();
            DistrettoDAO.attachDirty(district);
        }
        });
    LOG.info("updateDistrict[" + distrettoToString(district) + "] success!");
}
catch (ApplicationException e)
{
    LOG.error("updateDistrict() exception: " + e.getLocalizedMessage(), e);
    throw e;
}
}
Manrico Corazzi
i have proble with lazy intialization
Could you please elaborate? Are you getting the dreaded "Lazy Initialization Exception"?
Manrico Corazzi
pleas tell me how can i solve lazy initialization problem.I want to manage transcation in persitance layer (ie in DAO)
A: 

There are 3 main options :

1) Configure correctly lazy loading in your mappings and in your queries: This is not always the easiest way as you dont always know how your objects will be used in the presentation layer when you develop the DAO.

2) Use the OpenSessionInView pattern: This way, you will be able to lazy load related objects in the presentation layer. This is probably the easiest way as it only require a little bit of configuration. But be careful as lazy loading can be quite expensive and if you do something fishy in your presentation layer you can run into performance problems. You can also modify your objects from the presentation which means it is easier to make preogramming errors.

3) Add a service layer that convert your Hibernate object to value objects: this is the most fine grained option as you can also restrict wchi properties are exposed to the presentation. You keep transaction boundaries around the service call, so you are sure that nothing will be modified outside of a service.

In all cases, you should at least try to configure lazy loading correctly. Else you will probably run into performance problems !

Guillaume