没有类型为“javax.persistence.EntityManagerFactory”的合格 bean;可将 SpringBoot 从 2.1.18.RELEASE 升级到 2.6.2
将 SpringBoot 从 2.1.18.RELEASE 升级到 2.6.2 时,没有可用的“javax.persistence.EntityManagerFactory”类型的合格 bean
@ConditionalOnProperty("spring.datasource.url")
@Configuration("databaseConfiguration")
@Service
public class DatabaseConfiguration {
/*Used both @Autowired and @PersistenceUnit but still giving same exception*/
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
@Autowired
ApplicationContext context;
@PostConstruct
public void initSessionFactory() {
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry()
.getService(EventListenerRegistry.class);
registry.getEventListenerGroup(EventType.PERSIST).prependListener(new EntityListener());
registry.getEventListenerGroup(EventType.SAVE).prependListener(new EntityListener());
registry.getEventListenerGroup(EventType.SAVE_UPDATE).prependListener(new EntityListener());
registry.getEventListenerGroup(EventType.PERSIST_ONFLUSH).prependListener(new EntityListener());
ConfigurableListableBeanFactory beanFactory =
((ConfigurableApplicationContext) context).getBeanFactory();
beanFactory.registerSingleton("sessionFactory",
sessionFactory);
}
}
有人可以帮助我解决此问题吗?
谢谢。
No qualifying bean of type 'javax.persistence.EntityManagerFactory' available whike upgrading SpringBoot from 2.1.18.RELEASE to 2.6.2
@ConditionalOnProperty("spring.datasource.url")
@Configuration("databaseConfiguration")
@Service
public class DatabaseConfiguration {
/*Used both @Autowired and @PersistenceUnit but still giving same exception*/
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
@Autowired
ApplicationContext context;
@PostConstruct
public void initSessionFactory() {
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry()
.getService(EventListenerRegistry.class);
registry.getEventListenerGroup(EventType.PERSIST).prependListener(new EntityListener());
registry.getEventListenerGroup(EventType.SAVE).prependListener(new EntityListener());
registry.getEventListenerGroup(EventType.SAVE_UPDATE).prependListener(new EntityListener());
registry.getEventListenerGroup(EventType.PERSIST_ONFLUSH).prependListener(new EntityListener());
ConfigurableListableBeanFactory beanFactory =
((ConfigurableApplicationContext) context).getBeanFactory();
beanFactory.registerSingleton("sessionFactory",
sessionFactory);
}
}
Could someone help me to resolve this issue.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定您的项目中已经实现了该接口吗?
如果您使用 Spring Boot,那么这个 Maven 依赖项可能会有所帮助:
对于配置,请查看一些教程,例如 https://www.bezkoder.com/jpa-entitymanager-spring-boot/
Are you sure that you have an implementation of that interface in your project?
If you're using Spring Boot then maybe this Maven dependency could help:
For configuration have a look at some tutorial, e.g. https://www.bezkoder.com/jpa-entitymanager-spring-boot/