Is it possible to use both Spring Data R2DBC and Spring Data JPA in a single Spring boot application? - spring-data-jpa

I have an application which is using Spring data JPA and hibernate envers for db auditing.
Since R2DBC doesn't support auditing yet, is it possible to use combination of both in a single application?
If yes, Plan is to use Spring Data JPA for insert, update and delete operations, so that all DB auditing will be handled by hibernate envers. And use R2DBC for reactive non-blocking API's to read data.
If no, Is there any suggestions on how to achieve both reactive API's and auditing?

Spring provided simple Auditing via #EnableR2dbcAuditing, check my example.
Mix JPA in a reactive application is also possible, I have an example to demo run JPA in a reactive application, but not added r2dbc in it.
For your plan, a better solution is applying the cqrs pattern on the database topology, use database cluster for your application.
JPA for applying changes, use the main/master database to accept the modification, and sync changes to the secondary/slave database.
r2dbc for queries as you expected, use the secondary/slave database to query.
use a gateway at the front for the query and command service.
Update: I have created a sample to demo JPA and R2dbc coexistence in a single webflux application. But I do not suggest using it in real-world applications. Consider the 3rd solution above if you really need it.

Yes it is possible however you will probably face two issues, one is that multiple repos modules handling needs to be explicited (e.g specify the paths of the respective modules).
Secondly, the JDBC/JPA Spring Boot autoconfiguration will be disabled and you need to import it back.
Those issues have been reported in Reactive and Non Reactive repository in a Spring Boot application with H2 database
And a solution to them and the thought process can be found in this issue:
https://github.com/spring-projects/spring-boot/issues/28025

Related

#SpringBootTest how to pre-populate embedded MongoDB?

Doing implementation of a microservice with a few endpoints based on Spring Boot and MongoDB and trying to write integration tests using #SpringBootTest annotation capabilities.
At the moment, I am facing an issue that I need to pre-populate an embedded MongoDB instance that instantiated only during 'test' phrase with some test data.
And I did not find any out-of-the-box option available in Spring Boot for this purpose.
Some people advice to use for test data pre-populating tools like mongobee or mongoprefill or nosql-unit but for me, it seems like overhead or workaround, do not want to introduce any new dependencies even in test scope.
So could you please advice: In the current Spring Boot ecosystem, what is the right way to pre-populate MongoDB for testing purpose, when we are talking about integration (end-to-end) testing with #SpringBootTest?
There are multiple ways to pre-populate data:
Use the JUnit lifecycle methods like #BeforeEach, #BeforeAll to fill in data
You could disable the Spring Boot autoconfiguration for the embedded MongoDB and do it on your own and insert data after creating the connection
You could somehow mirror the #Sql feature we have for testing relational databases and write something similar using the AsbtractTestExectuionListener. For this have a look at the Spring class SqlScriptsTestExecutionListener
Provide a class that implements the CommandLineRunner interface and only activate this bean for your integration test profile with #Profile("integration-test")

What are admin UIs for spring-boot with mongoDB, providing CRUD operation like django-admin?

I am implementing a new spring-boot application with mongoDB, earlier worked with djangoAdmin. Looking for a similar solution which can easily generate management UI for all classes decorated with #document.
Have you seen JHipster?
It uses an Angular frontend with a Spring Boot backend but comes with a ton of admin functionality right out of the box. This includes the turn-key CRUD operations like you mentioned.
There is http://lightadmin.org project. But it currently supports only Spring Data JPA.

Does JpaTokenStore have any downsides when compared to JdbcTokenStore for spring security oauth

I currently use Jpa via Hibernate in my application. Since spring security oauth2 provides JdbcTokenStore, I started using it. But the problem with that is, I cannot use cache (which all my entities in the application currently share).
It hits the database in a separate flow.
I am thinking implementing JpaTokenStore thats backed by Jpa & leverage the cache advantages that comes with it.
Did anyone try implementing this/see any downsides using this approach?
In one project I've implmented org.springframework.security.oauth2.client.token.ClientTokenServices with JPA and didn't notice any problems. I was able to use all standard features of JPA including #Transactional for JPAClientTokenServices#saveAccessToken
There is nothing stopping you from doing it, and plenty of people do use JPA for all sorts of things, but IMO JPA is not ideal for handling storage of identity data. JPA is designed and optimized for cacheing data for the duration of a JDBC connection (a transaction basically), while identity data have a typically different and much longer lifetime. If you store long lived data using JPA, you have to deal with the consequences of what happens when you access it outside its normal lifetime, e.g. use DTOs, which ends up negating the benefits of using it in the first place to some extent.

Using Hystrix with Spring Data Repositories

Given that one of the main benefits of Spring Data and the related REST repositories is that most of the time the developer doesn't have to worry about the underlying implementations, is there an out-of-the-box way to leverage the Spring Cloud Netflix libraries, specifically the Hystrix annotations in this case, without extending every call in the provided Repository interfaces or creating my own implementation?
Currently you need to wrap calls in another service whose methods are annotated with #HystrixCommand. Because of the way both Spring Data and the Hystrix Aspect work (they both create proxies), there would need to be specific integration in Spring Data for #HystrixCommand. #ccit-spence is right, you really want to put #HystrixCommand on the services calling into a Spring Data REST repository.

Spring Data JPA and SimpleJPA/SimpleDB

I'm wondering if it is possible to specify SimpleJPA as the persistence provider used by Spring-Data-JPA. I'm not a JPA expert so I hope this question isn't silly. Is it as easy as just specifying SimpleJPA's entity manager factory in the persistence.xml? I have yet to find anywhere online where someone has used Spring-Data-JPA to connect to Amazon's SimpleDB, I would love it if someone could point me in the right direction.
I just found this project and got it working for our Spring Data + SimpleDB needs:
https://github.com/3pillarlabs/spring-data-simpledb
The documentation is pretty good and getting things up and running was pretty straight-forward. I was accessing SimpleDB via Spring Data with about 10 minutes of work.
Generally setting up your persistence provider is just a matter of setting up the EntityManagerFactory through your Spring context (in case you use the Spring container). Thus have a look at how to configure EntityManagerFactory instances in Spring.
There might be the need to implement a custom JpaVendorAdapter to let Spring use the SimpleDB JPA implementation correctly. For some advanced functionality (e.g. using pagination with manually defined queries) we'd have to tweak the Spring Data JPA codebase a bit. If you'd like to see that supported feel free to open a ticket in our JIRA.