我的Spring + Hibernate配置文件很小且超级紧凑。我使用自动扫描来查找我的模型实体/道数。使用注解书写Spring Spring Hibernate的DAO
我不想为我的层次结构中的每个实体都编写一个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 is garbage coll.
dao.save(lease);
这是不切实际的?
我喜欢这个想法,但它的工作?我期望类型擦除会导致重复的bean定义,虽然没有检查这个虽然... – seanhodges
搜索周围,它看起来像你可以解决使用子接口擦除:http:// stackoverflow。com/questions/502994/spring-ioc-and-generic-interface-type – seanhodges
为了澄清一下,SessionFactory上的Autowired会在Spring中自动调用Hibernate的SessionFactory,对吗?但是如果我的CustomHibernateDaoSupport使用调用setSessionFactory(sessionFactory)的Autowired方法来扩展org.springframework.orm.hibernate3.support.HibernateDaoSupport。所以默认情况下,其他dao的会话被配置为由Spring自动管理......这些多个“会话”是否会带来性能问题? – sloven