简体   繁体   中英

Common Argument Pass in Method

I have a method called makePersistent in my DAO class. Currntly we have this method in all dao classes and what i need to do is convert this method to common format. So is there any way to do it?

Method in UserDao Class

public void makePersistent(User model) throws InfrastructureException {
        try {
            getSession().saveOrUpdate(model);
            getSession().flush();
            getSession().clear();
        } catch (org.hibernate.StaleObjectStateException ex) {
            throw new InfrastructureException(Labels.getString("com.tran.msg.objectDeletedOrUpdated"));
        } catch (HibernateException ex) {
            throw new InfrastructureException(ex);
        }
    }

Method in HolidayDao Class

public void makePersistent(Holiday model) throws InfrastructureException {
        try {
            getSession().saveOrUpdate(model);
            getSession().flush();
            getSession().clear();
        } catch (org.hibernate.StaleObjectStateException ex) {
            throw new InfrastructureException(Labels.getString("com.tran.msg.objectDeletedOrUpdated"));
        } catch (HibernateException ex) {
            throw new InfrastructureException(ex);
        }
    }

Please help me to get rid of this redundant coding. Thank you.

Just use Object the hibernate will persist it.


public void makePersistent(Object model) throws InfrastructureException {
         try {
            getSession().saveOrUpdate(model);
            getSession().flush();
            getSession().clear();
        } catch (org.hibernate.StaleObjectStateException ex) {
            throw new InfrastructureException(Labels.getString("com.tran.msg.objectDeletedOrUpdaed"));
        } catch (HibernateException ex) {
            throw new InfrastructureException(ex);
        }
    }

Create a superclass for your DAOs with a type parameter and make your DAO classes extend that superclass with the appropriate type argument. For example:

public class BaseDao<T> {

    public void makePersistent(T model) throws InfrastructureException {
        try {
            getSession().saveOrUpdate(model);
            getSession().flush();
            getSession().clear();
        } catch (org.hibernate.StaleObjectStateException ex) {
            throw new InfrastructureException(Labels.getString("com.tran.msg.objectDeletedOrUpdated"));
        } catch (HibernateException ex) {
            throw new InfrastructureException(ex);
        }
    }
}

public class UserDao extends BaseDao<User> {
    // ...
}

public class HolidayDao extends BaseDao<Holiday> {
    // ...
}

UserDao and HolidayDao inherit the makePersistent method from BaseDao , so you don't have to implement it again in every DAO class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM