Hibernate的SessionFactory在每个DAO中还是仅在扩展类中?
我在 HibernateDaoSupport
上使用 getSession()
遇到了一些大问题,现在当我尝试修复它时,我想知道创建如下所示的抽象类是否正确并使所有 Dao 都扩展它,而不是在每个 Dao 中添加 SessionFactory ?
如果是的话,那么一旦其他 Dao 扩展了它,创建这个抽象 Dao 类的 bean 并将其传递给会话工厂是否可以工作?或者说这根本不可能?
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);
}
}
}
I run in few huge problems by using getSession()
on HibernateDaoSupport
and now when i try to fix it I was wondering if it is right to make a abstract class like this bellow and make all Dao's to extend it instead of adding SessionFactory
in each Dao ?
If it is, then would creating bean of this abstract Dao class and passing it the session factory then work once other Dao's extend it? Or that is not even possible?
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不使用 DI 框架,您可能需要保留 SessionFactory 的引用,并在创建 DAO 实例时自行传递它。
If you are not using a DI framework you may need to keep a reference for SessionFactory and pass it yourself when you create the DAO instance.
这正是人们使用 hibernate 实现 JPA 的原因。您只需要开始使用 JPA 的 EntityManager,它以最佳的设计模式利用 SessionFactory 本身。您不必在这里重新发明整个设计模式。您所需要做的只是在每个 DAO 中使用 EntityManager 的 CRUD 操作,如以下示例所示。祝您实施一切顺利。
http://www .myhomepageindia.com/index.php/2009/04/02/jpa-hibernate-with-oracle-on-eclipse.html
This is exactly why people use JPA implementation by hibernate. You just need to start using the JPA's EntityManager which leverages on SessionFactory by itself in the best possible design patterns. You dont have to reinvent the whole design patterns here. All you need to do is just use CRUD operations of EntityManager in each of your DAO as shown in the following example. All the best with your implementation.
http://www.myhomepageindia.com/index.php/2009/04/02/jpa-hibernate-with-oracle-on-eclipse.html