Java 公共参数传入方法

Java 公共参数传入方法,java,hibernate,methods,Java,Hibernate,Methods,在我的DAO类中有一个名为makePersistent的方法。 目前,我们在所有dao类中都有此方法,我需要做的是将此方法转换为通用格式。那么有没有办法呢 UserDao类中的方法 public void makePersistent(User model) throws InfrastructureException { try { getSession().saveOrUpdate(model); getSession().fl

在我的DAO类中有一个名为makePersistent的方法。 目前,我们在所有dao类中都有此方法,我需要做的是将此方法转换为通用格式。那么有没有办法呢

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

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);
        }
    }
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);
        }
    }
请帮我去掉这个多余的编码。
谢谢。

使用类型参数为DAO创建一个超类,并使用适当的类型参数使DAO类扩展该超类。例如:

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> {
    // ...
}
公共类BaseDao{
公共void makePersistent(T模型)引发InfrastructureException{
试一试{
getSession().saveOrUpdate(模型);
getSession().flush();
getSession().clear();
}catch(org.hibernate.StaleObjectStateException ex){
抛出新的InfrastructureException(Labels.getString(“com.tran.msg.ObjectDeleteDorUpdate”);
}捕获(HibernateeException例外){
抛出新的InfrastructureException(ex);
}
}
}
公共类UserDao扩展了BaseDao{
// ...
}
公共类HolidayDao扩展了BaseDao{
// ...
}

UserDao
HolidayDao
继承了
BaseDao
中的
makePersistent
方法,因此您不必在每个DAO类中再次实现它。

完美这就是我想要的。非常感谢。
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);
        }
    }