spring事务问题求解

faith.huan faith.huan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下 DataDao:
public class DataDao {

public void getMkmDetail(){
persistentDetail();
}

@Transactional
public void persistentDetail(){
//获取current session
System.out.println(hibernateUtil.getSession("current"));
}
}

调用方法及结果:
调用方法一:
public class DataLoader {
public static void main(String[] args) throws InterruptedException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
DataDao dd = (DataDao) ctx.getBean("dataDao");
dd.persistentDetail();
}

结果:可以打印session对象
SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@1fbb60ad updates=org.hibernate.engine.spi.ExecutableList@46c80e21 deletions=org.hibernate.engine.spi.ExecutableList@7817bcd4 orphanRemovals=org.hibernate.engine.spi.ExecutableList@5e6e3fe4 collectionCreations=org.hibernate.engine.spi.ExecutableList@1824c22b collectionRemovals=org.hibernate.engine.spi.ExecutableList@1547a16f collectionUpdates=org.hibernate.engine.spi.ExecutableList@11b78953 collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@2898b13b unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])

调用方法一:
public class DataLoader {
public static void main(String[] args) throws InterruptedException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
DataDao dd = (DataDao) ctx.getBean("dataDao");
dd.getMkmDetail();
}

结果:Exception in thread "main" org.hibernate.HibernateException: No Session found for current thread


求解 为什么直接调用可以,用方法间接调用就不行了

...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
faith.huan
Java EE 67,535 成员 225,843 内容