Spring JPA PostgreSQL + MongoDB

Spring JPA PostgreSQL + MongoDB

问题描述:

从Spring示例开始,使用REST访问MongoDB数据(

Starting from the Spring example Accessing MongoDB Data with REST (https://spring.io/guides/gs/accessing-mongodb-data-rest/) I'd like to integrate a PostgreSQL data source and link it to the MongoDB repository.
By switching from MongoRepository to JpaRepository and accordingly changing the application.properties file I've been able to pass from MongoDB to PostgreSQL and viceversa, but basically having only one active data source at time.

application.properties when using MongoDB

spring.data.mongodb.port=27017
spring.data.mongodb.uri=mongodb://localhost/
spring.data.mongodb.database=myMongoDB_DB
spring.data.mongodb.repositories.enabled=true


使用PostgreSQL时 application.properties


application.properties when using PostgreSQL

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/myPostgreSQL_DB
spring.datasource.username=me
spring.datasource.password=mySuperSecretPassword

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

有没有一种配置Spring的方法(仅使用 Annotation 方式)将两个数据源链接到同一个存储库,这样当我通过HTTP访问我的REST Web服务时,MongoDB和PostgreSQL都是是否以完全相同的方式进行更改?

我在Google周围搜索,发现了一些有关Spring跨商店支持的信息(

Is there a way to configure Spring (with an Annotation-only way) to link two data sources to the same Repository so that when I access my REST web service via HTTP both MongoDB and PostgreSQL are changed in exactly the same way?

I googled around and found something about Spring cross-store support (http://docs.spring.io/spring-data/mongodb/docs/1.5.5.RELEASE/reference/html/mongo.cross.store.html) but it uses xml for the application configuration and AspectJ, is there a simpler way to accomplish this?

在本章中,您可以找到答案-

In this chapter you can find an answer - (Spring-boot manual - Use Spring Data JPA and Mongo repositories)