Best Way to Inject Hibernate Session by Spring 3

前端 未结 4 937
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 12:01

I am not sure whats the best way to inject Hibernate\'s session instance to DAO classes using Spring3. I am not using Spring\'s Hibernate Template support for this so here i

4条回答
  •  再見小時候
    2020-12-02 12:19

    A combination of skaffman's post and Sean's plus the use of annotations.

    Dao

    @Respository("productDao")
    public class ProductDaoImpl implements ProductDao {
    
        @Autowired
            private SessionFactory sessionFactory;
    
                public Collection loadProductsByCategory(String category) {
                        return this.sessionFactory.getCurrentSession()
                            .createQuery(
                                "from test.Product product where product.category=?")
                            .setParameter(0, category)
                            .list();
            }
    }
    

    xml

    
    
      
        
        
        
        
      
    
      
        
        
          
            product.hbm.xml
          
        
        
          
            hibernate.dialect=org.hibernate.dialect.HSQLDialect
          
        
      
    
    
    

提交回复
热议问题