没有类型为“javax.persistence.EntityManagerFactory”的合格 bean;可将 SpringBoot 从 2.1.18.RELEASE 升级到 2.6.2

发布于 2025-01-10 01:19:10 字数 1290 浏览 0 评论 0原文

将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为一人 2025-01-17 01:19:10

您确定您的项目中已经实现了该接口吗?
如果您使用 Spring Boot,那么这个 Maven 依赖项可能会有所帮助:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

对于配置,请查看一些教程,例如 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:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

For configuration have a look at some tutorial, e.g. https://www.bezkoder.com/jpa-entitymanager-spring-boot/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文