我正在研究使用Hibernate进行模型持久化。看起来有两种方法可以实现。
第一种方法是使用SessionFactory
,例如:
public Collection loadProductsByCategory(String category) {
return this.sessionFactory.getCurrentSession()
.createQuery("from test.Product product where product.category=?")
.setParameter(0, category)
.list();
}
另一个方法使用扩展了
CrudRepository
的注释子类/接口:@Transactional
public interface UserDao extends CrudRepository<User, Long> {
public User findByEmail(String email);
}
除了使用@Transactional
注解之外,有时我也看到使用@Repository
注解的情况。它们之间有什么区别呢?
我还没有找到“我应该在什么时候使用前者或后者”的答案,所以可以给我解释一下吗?
SessionFactory
,CrudRepository
,@Transactional
,@Repository
。 - Dragan Bozanovic