Hibernate related

 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.

猜你喜欢

转载自buralin.iteye.com/blog/2245101