public int getCount1(Company company,Object param)
{
int count = 0;
if (company != null) {
Session session = (Session) em.getDelegate();
Criteria criteria = session.createCriteria(Company.class);
if (company.getCompanyname() != null && company.getCompanyname().trim().length() > 0)
criteria.add(Restrictions.like("companyname", company.getCompanyname(),
MatchMode.ANYWHERE));

count = ((Number) criteria.setProjection(Projections.rowCount())
.uniqueResult()).intValue();
}
return count;
}
割---------
Session session = (Session) em.getDelegate();
这里用EntityManager.getDelegate得到session.
当得到结果count后,要不要手动将session关闭?
service层中,用了spring的@Transactional注解

解决方案 »

  1.   

    这个要!你要调用spring框架下的hibernateTemple,spring才能管理session
      

  2.   

    我用的是hibernate 的jpa,用EntityManager getDelegate()得到 的session. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    用上面这个管理事务的。
      

  3.   

    自己获取session来操作,session是要自己关闭的;
    不过如果你非要获取session也可以这么做:
    getHibernateTemplate().executeFind(new HibernateCallback() {
    public Object doInHibernate(Session session) throws HibernateException {
        这个session可以被spring关闭
       
    }
      

  4.   

    自己获取session来操作,session是要自己关闭的