gpt4 book ai didi

java - Hibernate的SessionFactory是在每个DAO中还是只在扩展类中?

转载 作者:行者123 更新时间:2023-12-02 00:42:25 30 4
gpt4 key购买 nike

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

如果是,那么一旦其他 Dao 扩展了它,创建这个抽象 Dao 类的 bean 并将其传递给 session 工厂是否可以工作?或者说这根本不可能?

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);
}
}

}

最佳答案

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 实例时自行传递它。

关于java - Hibernate的SessionFactory是在每个DAO中还是只在扩展类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5971615/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com