本文介绍了@PostConstruct中没有可用的事务性实体管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 代码: @Component public class HibernateEventWiring { $ b $ @Autowired private ViewListener listener; @PersistenceContext(unitName =config-punit) private EntityManager entityManager; @PostConstruct public void registerListeners(){ SessionFactory sessionFactory = getSessionFactory(); EventListenerRegistry registry =((SessionFactoryImpl)sessionFactory).getServiceRegistry()。getService( EventListenerRegistry.class); registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(listener); $ b @Transactional private SessionFactory getSessionFactory(){ // EXCEPTION:无事务型实体管理器可用返回entityManager.unwrap(SessionImplementor.class) .getFactory(); $ div $解析方案 这个极好的答案: As I see, you do not need a transaction nor an entity manager bean, but rather an entity manager factory bean. I think you should simply autowire the EntityManagerFactory and then unwrap the Hibernate SessionFactory from it.@Autowiredprivate EntityManagerFactory entityManagerFactory;@PostConstructpublic void registerListeners() { SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class); ...} 这篇关于@PostConstruct中没有可用的事务性实体管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 20:40