我在getSession()
上使用HibernateDaoSupport
遇到了一些大问题,现在当我尝试修复它时,我想知道是否正确制作一个像这样的抽象类,并将所有Dao改为扩展它而不是在每个Dao中添加SessionFactory
?
如果是,那么会创建这个抽象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);
}
}
}
答案 0 :(得分: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实例时自行传递。
答案 1 :(得分: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