0

我通过使用getSession()on遇到了一些大问题HibernateDaoSupport,现在当我尝试修复它时,我想知道创建一个像下面这样的抽象类并让所有 Dao 扩展它而不是添加SessionFactory每个 Dao 是否正确?

如果是,那么创建这个抽象 Dao 类的 bean 并将其传递给会话工厂,然后一旦其他 Dao 扩展它就可以工作吗?或者那是不可能的?

public abstract class AbstractDAOImpl<T> implements
        AbstractDAO<T> {

    private static Logger   _logger = LoggerFactory
                                    .getLogger(AbstractDAOImpl.class);
    private SessionFactory  factory;

    @Override
    public void refresh(final T object) {
        try {
            factory.getCurrentSession().refresh(object);
        } catch (Exception e) {
            _logger.error("Cannot refresh object " + object, e);
        }
    }

    @Override
    public void remove(final T object) {
        try {
            factory.getCurrentSession().delete(object);
        } catch (Exception e) {
            _logger.error("Cannot remove object " + object, e);
        }
    }

    @Override
    public void save(final T object) {
        try {
            factory.getCurrentSession().saveOrUpdate(object);
        } catch (Exception e) {
            _logger.error("Cannot save or update object " + object, e);
        }
    }

}
4

2 回答 2

2
public interface RootDAO<T> extends Serializable {

    public List<T> loadAll();

    public T save(T entity);

    public void delete(T entity);

    public void markAsDeleted(T entity);

    public T get(Serializable id);

    public T load(Serializable id);

    public void saveOrUpdate(T entity);

    public void deleteAll(Collection<T> entities);

    public void saveOrUpdateAll(Collection<T> entities);

    public List<T> find(String hql);

    public void update(T entity);

    public T getByExampleUnique(T entity);

    public List<T> getByExampleList(T entity);

    public List<T> listAll();

    public Object execute(HibernateCallback action);

    public List<T> findByNamedParam(String queryString, String paramName,Object value);

    public List<T> findByNamedParam(String queryString, String[] paramNames,Object[] values);
    .
    .
    .
    .

}

@Component
public abstract class RootDAOImpl<T> extends HibernateDaoSupport implements RootDAO<T> {


    protected Logger logger = LoggerFactory.getLogger(getClass());
    private Class<T> clazz;

    @Autowired
    public void init(SessionFactory factory) {
        setSessionFactory(factory);
    }

    public RootDAOImpl(Class<T> clazz) {
        this.clazz = clazz;
    }

    public void delete(T entity) {
        getHibernateTemplate().delete(entity);
    }

    public void delete(String id) {
        getHibernateTemplate().delete(new FbUser(id));
    }

    public void markAsDeleted(T entity) {
        // Mark entity as deleted
        try {
            Method setDeletedMethod = clazz.getDeclaredMethod("setDeleted", Boolean.class);
            setDeletedMethod.invoke(entity, true);
            getHibernateTemplate().saveOrUpdate(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // actually delete
        // getHibernateTemplate().delete(entity);
    }

    @Override
    public void deleteAll(Collection<T> entities) {
        getHibernateTemplate().deleteAll(entities);
    }

    @Override
    public void saveOrUpdateAll(Collection<T> entities) {
        getHibernateTemplate().saveOrUpdateAll(entities);
    }

    @SuppressWarnings("unchecked")
    @Override
    public T get(Serializable id) {
        return (T) getHibernateTemplate().get(clazz, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public T load(Serializable id) {
        return (T) getHibernateTemplate().load(clazz, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<T> find(String hql) {
        return (List<T>) getHibernateTemplate().find(hql);
    }

    @Override
    public Object execute(HibernateCallback action) {
        return getHibernateTemplate().execute(action);
    }


    .
    .
    .

}

@Repository
public class UserDAOImpl extends RootDAOImpl<User> implements UserDAO{


    public UserDAOImpl() {
        super(User.class);
    }
}

如果您不使用 DI 框架,则可能需要保留 SessionFactory 的引用并在创建 DAO 实例时自行传递。

于 2011-05-12T08:51:38.763 回答
1

这正是人们通过hibernate使用JPA实现的原因。您只需要开始使用 JPA 的 EntityManager,它在可能的最佳设计模式中单独利用 SessionFactory。您不必在这里重新发明整个设计模式。您只需在每个 DAO 中使用 EntityManager 的 CRUD 操作,如下例所示。祝您实施一切顺利。

http://www.myhomepageindia.com/index.php/2009/04/02/jpa-hibernate-with-oracle-on-eclipse.html

于 2011-05-12T00:05:57.160 回答