简体   繁体   English

方法中的常见参数传递

[英]Common Argument Pass in Method

I have a method called makePersistent in my DAO class. 我的DAO类中有一个名为makePersistent的方法。 Currntly we have this method in all dao classes and what i need to do is convert this method to common format. 我们在所有dao类中都有这个方法,我需要做的是将此方法转换为通用格式。 So is there any way to do it? 那有什么办法吗?

Method in UserDao Class UserDao类中的方法

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 HolidayDao类中的方法

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. 使用类型参数为DAO创建超类,并使DAO类使用适当的类型参数扩展该超类。 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. UserDaoHolidayDaoBaseDao继承makePersistent方法,因此您不必在每个DAO类中再次实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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