为什么不建议使用HibernateTemplate?
我习惯于在hibernate 3中使用getHibernateTemplate(),现在我正在转向Hibernate 4,在这里我没有找到以下类:
org.springframework.orm.hibernate4.support.HibernateDaoSupport;
在这里我读到了它不再推荐使用
http://forum.springsource.org/showthread.php?117227-Missing-Hibernate-Classes-Interfaces-in-spring-orm-3.1.0.RC1
有人能解释我为什么吗? 现在我需要在hibernate 4中完成所有任务,如提交,关闭,刷新由getHibernateTemplate()方法自动管理的事务?
因为它的主要目标是将Hibernate会话绑定到当前的Spring事务,当时SessionFactory.getCurrentSession()
不存在。 因为它现在存在(并且很长一段时间:即使在hibernate3包中也不鼓励使用HibenateTemplate),没有理由使用这个特定于Spring的类而不是使用SessionFactory.getCurrentSession()
来获得与当前Spring绑定的会话交易。
如果你使用Spring,那么你应该使用它的声明式事务管理,它允许你避免打开,提交,关闭和刷新。 这一切都是由Spring自动完成的:
@Autowired private SessionFactory sessionFactory; @Transactional public void someMethod() { // get the session for the current transaction: Session session = sessionFactory.getCurrentSession(); // do things with the session (queries, merges, persists, etc.) }
在上面的示例中,将在方法调用之前启动事务(如果尚未启动); Spring将为事务创建一个会话,会话将在事务提交之前自动刷新,当方法返回时,Spring将自动完成该会话。
- 如何避免使用collection fetch指定的警告“firstResult / maxResults; 在内存中应用!“在使用Hibernate时?
- 如何为这个SQL查询编写Hibernate Criteria?
- 这个嵌套注释做什么/允许什么?
- JPA with Hibernate 5:以编程方式创建EntityManagerFactory
- 期望在模型属性@RequestBody或@RequestPart参数之后立即声明Errors / BindingResult参数
- 使用Hibernate @Index注释在DB上创建索引
- JBoss 5.1.0.GA中的Hibernate 3.5-Final
- hibernate插入到集合会导致删除,然后再次插入集合中的所有项目
- 连接超时是在不活动期后