0
在傳統項目中,我添加一個新的方法的DAO,然後我寫了一個單元測試來測試新方法當加@Transactional無法初始化DAO豆
FooDAO dao = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}).getBean("FooDAO");
@Test
public void test_getUnenabledArtisanIdsByReverseTime(){
String reverseTimeStr = "2016-02-18 19:00";
List<String> artisanIds = new ArrayList<>();
artisanIds.add("1");
artisanIds.add("2");
dao.getUnenabledArtisanIdsByReverseTime(artisanIds, reverseTimeStr);
}
失敗,拋出異常
下方org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
然後,添加以下配置在applicationContext.xml
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
並添加@Transactional
以上的新方法再次
@Transactional
public List<String> getUnenabledArtisanIdsByReverseTime(List<String> artisanIds, String reverseTimeStr)
然後執行單元測試,失敗過,但它的另一個異常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'FooDAO' defined in file [/home/zhuguowei/workspace/myapp/WebContent/WEB-INF/classes/com/foo/artisan/repository/FooDAO.class]:
Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException:
Could not generate CGLIB subclass of class [class com.foo.artisan.repository.FooDAO]:
Common causes of this problem include using a final class or a non-visible class; nested exception is net.sf.cglib.core.CodeGenerationException: java.lang.ClassCastException-->java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
很奇怪的道是公開的,而不是最後一類
@Component
public class FooDAO extends BaseDaoHiber4Impl<Foo>
public BaseDaoHiber4Impl() {
this.entryClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
那麼這個問題的原因是什麼?如何解決這個問題?
謝謝!但可以有更好的方式解決這個問題 – zhuguowei
謝謝!但你知道這是一個遺留項目 – zhuguowei
你確實存在某種方式可以讓dao方法不必在交易中,例如, ' false ' –
zhuguowei