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
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
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)
Related
I am having a need where in a same spring boot application I need to use repo of Spring Data Mongo and Spring data jpa (postgres), during my initial testing I got runtime errors. While I debug more just wanted to check in same Spring Data Application can it have both repos for Postgres and MongoDB ?
Yes, you can have, make sure to add all dependency and create separate repositories for each data connection. I used MongoDB and MySql database it is working fine.
I am trying to write Spring Boot application to connect to a Teiid database, I want to use JPA layer on it. I have configured the JDBC Data Source, but since this not well-known database in Spring JPA libraries do not autodetect this source. I have manually setup "spring.jpa.*" properties too. I do have a Hibernate dialect for this database, and it is on the classpath.
So, how does one need to configure JPA layer for a not well-known database in Spring Boot? Thank you for your time.
Ramesh..
This is fairly well defined in the Spring Boot documentation.
You can set this explicitly in the application.properties file
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
I'm writing a plain spring jdbc application using spring boot after a long time without using jpa or hibernate or any other ORM solution. I have need to print the runtime SQL for debugging purposes. I researched a little bit but could not find anything. I know in a jpa/hibernate application we can say following in application.properties
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
But not sure what is the name name of property in spring jdbc applications. Any help is appreciated.
I've seen you can use JaVers to serialize changes to a database, but I can't find a good example about Spring or Hibernate integration. I would also know if I can change the generated table names and columns.
Thank!
In Javers doc, there is the example you are looking for:
http://javers.org/documentation/spring-integration/#spring-jpa-example
If you are using Spring Boot, examples for JaVers Spring Boot starter for SQL
http://javers.org/documentation/spring-boot-integration/
We recommend using the second option - JaVers Spring Boot starter
I would like to use Flyway as preferred way of database migration handling in my Spring Boot project (using current V1.2.1.RELEASE).
This is working fine so far however the integration with Spring Security using a JDBC DataSource seems to override the Flyway mechanism.
Following simple scenario:
Spring Boot 1.2.1
PostgreSQL 9.4.1
Flyway migration scripts for users, groups and authorities according to Spring Security documentation
Problem:
The Flyway migration scripts are not executed at startup as expected.
Maybe cause: It seems that Flyway is only executed at startup if the using Spring Boot project is also using JPA at least. Since Spring Security is based on plain JDBC, I've tried to temporary use the JDBC based database initialization scheme as described in Spring Boot docs (Chapter 68.3) which works, but (as documented) this way is like the 'poor man approach' and I'd really like to use Flyway also for these tables containing the User/Group/Authorities information.
Ok, after some further investigation I've found the problem:
Indeed, in a standard Spring Boot project the security context is initialized before any Flyway based migration takes place.
Normally this is not a big issue, but I've also used the AuthenticationManagerBuilder for creating a default admin user. This seems to be the wrong approach for creating such an initial user account.