HibernateTemplate vs HibernateDaoSupport vs SessionFactory注入 [英] HibernateTemplate vs HibernateDaoSupport vs SessionFactory Injection
问题描述
-
直接访问Hibernate
会话会话= HibernateUtil.getSessionFactory()。getCurrentSession();
session.beginTransaction();
-
使用HibernateTemplate
列表bb =(List )hibernateTemplate.find(from Entity);
-
使用HibernateDAOSupport类
列表bb = (List)getHibernateTemplate()。find(from Entity);
Accessing Hibernate directly
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction();
Using HibernateTemplate
List bb = (List)hibernateTemplate.find("from Entity");
Using HibernateDAOSupport classes
List bb =(List)getHibernateTemplate().find("from Entity");
你能帮我应该用什么?我从 CodeRanch 和另一个< link ,它告诉 Hibernate 3.x 之后,我们应该在DAO类中使用 @Repository
注入 SessionFactory
。
有人可以详细解释这一点吗?
问候,
Arun Kumar
Spring本身建议不要再使用HibernateTemplate,在该类的javadoc 。您可以声明会话工厂作为一个Spring bean ,将它作为其他Spring bean注入到你自己的组件中,并直接使用本地Hibernate API(使用 sessionFactory.getCurrentSession())。
I have seen in lot of forums and still in confusion. We are starting a new project with Spring 3.1 & Hibernate 4 and need to decide which strategy to use for Hibernate with Spring:
Can you please help what should i use? I have read from CodeRanch and one another link which tell that from Hibernate 3.x onwards we should inject SessionFactory
in our DAO Classes(using @Repository
).
Can someone explain this in detail?
Regards,
Arun Kumar
Spring itself recommends not using HibernateTemplate anymore, in the javadoc of the class. You can declare the session factory as a Spring bean directly, inject it as any other Spring bean in your own components, and use the native Hibernate API directly (using sessionFactory.getCurrentSession()).
这篇关于HibernateTemplate vs HibernateDaoSupport vs SessionFactory注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!