用Spring Hibernate使用Annotations写入更少的DAO [英] Write Less DAOs with Spring Hibernate using Annotations
问题描述
我不想为我的层次结构中的每个实体编写DAO + DAOImpl。
>有些人可能有资格拥有自己的个人资料,例如他们与其他实体之间存在复杂的关系,并且需要的不仅仅是基本的CRUD功能。但是对于其余的......
有没有办法绕过事实上的标准?
就像一个通用的DAO,例如:
http://www.ibm.com/developerworks/java/library/j-genericdao/index.html
然后我可以做类似
GenericDao dao = appContext.getBean(genericDao);
dao.save(car);
dao.save(lease);
注释可以吗?我不想在xml中配置任何东西。如果我不能在上面做,是否有可能有一个GenericDaoImpl.java类似于:
@Repository(carDao )
@Repository(leaseDao)
class GenericDaoImpl extends CustomHibernateDaoSupport implements GenericDao {
...
}
然后
GenericDao dao = appContext.getBean(carDao );
dao.save(car);
dao = appContext.getBean(leaseDao); // carDao是垃圾桶。
dao.save(lease);
这是实用的吗?
使用泛型,您可以尝试如下所示:
@Repository
@Transactional
public class GenericDAOImpl< T>实现GenericDAO< T> {
@Autowired
私人SessionFactory工厂;
public void persist(T entity){
Session session = factory.getCurrentSession();
session.persist(entity);
$ b $ @SuppressWarnings(unchecked)
public T merge(T entity){
Session session = factory.getCurrentSession();
return(T)session.merge(entity);
}
public void saveOrUpdate(T entity){
Session session = factory.getCurrentSession();
session.saveOrUpdate(entity);
}
public void delete(T entity){
Session session = factory.getCurrentSession();
session.delete(entity);
}
}
内容可能不同,但总体思路是适用的。
然后,您应该可以使用
自动装载控制器和服务类中的DAO
@Autowired
private GenericDAO< Car> carDao;
My Spring+Hibernate configuration files are small and super tight. I use auto scanning to find my model entities/daos.
I don't want to have to write a DAO + DAOImpl for EVERY Entity in my hierarchy.
Some may qualify to have their own, like if they have complex relationships with other entities and require more than basic CRUD functionality. But for the rest...
Is there any way to circumvent the defacto standard?
Say, something like a generic DAO, ex:
http://www.ibm.com/developerworks/java/library/j-genericdao/index.html
Then I can do something like
GenericDao dao = appContext.getBean("genericDao");
dao.save(car);
dao.save(lease);
Is this possible with annotations? I don't want to have to configure anything in xml. If I cannot do above, is it still possible to have one GenericDaoImpl.java with something like:
@Repository("carDao")
@Repository("leaseDao")
class GenericDaoImpl extends CustomHibernateDaoSupport implements GenericDao {
...
}
and then
GenericDao dao = appContext.getBean("carDao");
dao.save(car);
dao = appContext.getBean("leaseDao"); //carDao is garbage coll.
dao.save(lease);
Is this practical at all?
Using generics, you might try something like this:
@Repository
@Transactional
public class GenericDAOImpl<T> implements GenericDAO<T> {
@Autowired
private SessionFactory factory;
public void persist(T entity) {
Session session = factory.getCurrentSession();
session.persist(entity);
}
@SuppressWarnings("unchecked")
public T merge(T entity) {
Session session = factory.getCurrentSession();
return (T) session.merge(entity);
}
public void saveOrUpdate(T entity) {
Session session = factory.getCurrentSession();
session.saveOrUpdate(entity);
}
public void delete(T entity) {
Session session = factory.getCurrentSession();
session.delete(entity);
}
}
The content may be different, but the general idea is applicable.
You should be able to then autowire the DAO in your controller and service classes by using
@Autowired
private GenericDAO<Car> carDao;
这篇关于用Spring Hibernate使用Annotations写入更少的DAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!