java 如何为存储库设置特定的数据源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29397522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:11:48  来源:igfitidea点击:

How to set a specific DataSource for a Repository?

javaspringspring-data-jpa

提问by membersound

Is it possible to assign a specific DataSourceto a @Repository?

是否可以将特定分配DataSource给 a @Repository

I'd like to create a test environment where in general I want to use the test-datasource, but a few CrudRepositoryshould operate on a different DB (the production DB; read-only operations).

我想创建一个测试环境,通常我想使用测试数据源,但有一些CrudRepository应该在不同的数据库上运行(生产数据库;只读操作)。

Can I tell spring which datasource to use for a repository explicit?

我可以明确地告诉 spring 哪个数据源用于存储库吗?

public interface MyRepository extends CrudRepository<Customer, Long> {}

public interface MyRepository extends CrudRepository<Customer, Long> {}

采纳答案by Faraj Farook

@EnableJpaRepositoriesis the answer to your question. This should work with CrudRepositoryaccording to the informal documentations.

@EnableJpaRepositories是你问题的答案。这应该CrudRepository根据非正式文件使用。

Refer this detail tutorial on how to do this. I didn't put my effort to post the codes here as you may directly refer it much clearer in it.

请参阅有关如何执行此操作的详细教程。我没有努力在此处发布代码,因为您可以直接在其中更清楚地引用它。

link to the Tutorial...

链接到教程...

回答by manish

The DataSourceand JpaRepositoryare both tied to an EntityManager. You will have to segregate the repositories into separate packages for your requirement to work.

DataSourceJpaRepository都绑定到EntityManager。您必须将存储库隔离到单独的包中才能满足您的要求。

Here is an example:

下面是一个例子:

<bean id="emf1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource">
    <bean .../>
  </property>
  ...
</bean>
<jpa:repositories base-package="org.example.package1" entity-manager-factory-ref="emf1"/>

<bean id="emf2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource">
    <bean .../>
  </property>
  ...
</bean>
<jpa:repositories base-package="org.example.package2" entity-manager-factory-ref="emf2"/>

回答by AntJavaDev

well it depends on which is your design , cause there are different implementations that you can follow , f.e you can declare two beans for the two datasources and in your code specify to which one you want to hit , or else you could define two differents context and a shared context in which again you ll have to specify in your code which service you want to call. Here is an older questionthat might help you for the 1st approach

好吧,这取决于您的设计是哪个,因为您可以遵循不同的实现,fe 您可以为两个数据源声明两个 bean,并在您的代码中指定要命中的 bean,否则您可以定义两个不同的上下文以及一个共享上下文,您必须在代码中再次指定要调用的服务。这是一个较旧的问题,可能对您的第一种方法有所帮助

回答by Kikin-Sama

Just set the nameattribute of the @PersistenceContextannotation when declaring your EntityManager.

只需设置name的属性@PersistenceContext声明你的时候注释EntityManager

@PersistenceContext(name="persistence-unit-name")
private EntityManager em;