您的位置:首页 > 其它

Hibernate related

2015-09-21 20:37 127 查看

 1. Cannot get .currentSession()

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
 at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
 at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:622) 

 

@Override
public void refreshObject() {

Test test1;
test1 = testDaoLocator.findById("1");
System.out.println("C1: " + test1.getC2());

test1.setC2("ddd");
}

public Test findById(String id) {

Test instance = (Test) this.getSessionFactory().getCurrentSession().get(Test.class, id);

  
      return instance;
}
 

 

Cause:
There is no any session.
Solution 1 is using "this.getSessionFactory().openSession()" intead.
Solution 2 is adding transaction between the method like below:

  

<aop:config>
<aop:advisor pointcut="execution(* lin.service..refreshObject(..))" advice-ref="requiresNewTxAdvice" />
</aop:config>

<tx:advice id="requiresNewTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRES_NEW" />
</tx:attributes>
</tx:advice>

 

2.

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: