如何在自定义实现中访问 Spring Data 配置的实体管理器(工厂) [英] How to access Spring Data configured entity manager (factory) in custom implementation

查看:25
本文介绍了如何在自定义实现中访问 Spring Data 配置的实体管理器(工厂)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Spring Data JPA 存储库编写自定义实现.所以我有:

I'm writing a custom implementation for a Spring Data JPA repository. So I have:

  • MyEntityRepositoryCustom => 与自定义方法的接口
  • MyEntityRepositoryUmpl => 上述接口的实现
  • MyEntityRepository => 扩展 JpaRepositoryMyEntityRepositoryCustom
  • 的标准接口
  • MyEntityRepositoryCustom => interface with the custom methods
  • MyEntityRepositoryUmpl => implementation of the above interface
  • MyEntityRepository => standard interface which extends JpaRepository and MyEntityRepositoryCustom

我的问题是:在 MyEntityRepositoryUmpl 的实现中,我需要访问注入 Spring Data 的实体管理器.如何获得?我可以使用 @PersistenceContext 让它自动装配,但问题是这个存储库必须在设置了多个持久性单元的应用程序中工作.因此,要告诉 Spring 我需要哪一个,我必须使用 @PersistenceContext(unitName="myUnit").但是,由于我的存储库是在可重用的服务层中定义的,因此我无法知道更高级别的应用程序层将配置为注入到我的存储库中的持久性单元的名称是什么.

My problem is this: within the implementation of MyEntityRepositoryUmpl I need to access the entity manager that was injected into Spring Data. How to get it? I can use @PersistenceContext to get it autowired, but the problem is that this repository must work in an application that sets up more than one persistence units. So, to tell Spring which one I need, I would have to use @PersistenceContext(unitName="myUnit"). However, since my repositories are defined in a reusable service layer, I can't know at that point what will be the name of the persistence unit that the higher-level application layer will configure to be injected into my repositories.

换句话说,我需要做的是访问 Spring Data 本身正在使用的实体管理器,但在(不是那么快)查看 Spring Data JPA 文档后,我找不到任何相关内容.

In other words, what I would need to do is to access the entity manager that Spring Data itself is using, but after a (not so quick) look at Spring Data JPA documentation I couldn't find anything for this.

老实说,Impl 类完全不知道 Spring Data 的事实,虽然在 Spring Data 手册中被描述为一种优势,但实际上每当您需要访问通常由Spring Data 本身在您的自定义实现中(几乎总是,我会说......).

Honestly, the fact that the Impl classes are totally unaware of Spring Data, although described as a strength in Spring Data manual, is actually a complication whenever you need to access something that is usually provided by Spring Data itself in your custom implementation (almost always, I would say...).

推荐答案

我能找到的最好的办法是建立一个约定":我的存储库声明他们期望一个名为 myConventionalPU 的持久性单元来提供.然后,应用程序层将该别名分配给它设置并注入 Spring Data 的实体管理器工厂,因此我的自定义实现可以通过使用该别名通过自动装配接收正确的 EMF.这是我的应用程序上下文的摘录:

The best I could find is to set up a "convention": my repositories declare that they expect a persistence unit named myConventionalPU to be made available. The application layer then assigns that alias to the entity manager factory that it sets up and injects into Spring Data, so my custom implementations can receive the correct EMF with autowiring by using that alias. Here's an excerpt of my application context:

<bean id="myEntityManagerFactory" name="myConventionalPU" 
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  [...]
</bean>

<jpa:repositories base-package="com.example"
  entity-manager-factory-ref="myEntityManagerFactory"
  transaction-manager-ref="transactionManager" />

在我的自定义实现中:

@PersistenceContext(unitName = "myConventionalPU")
private EntityManager em;

我根据这个要求打开了 DATAJPA-669.

I opened DATAJPA-669 with this requirement.

这篇关于如何在自定义实现中访问 Spring Data 配置的实体管理器(工厂)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆