Stop Spring JPA Implementation for few Interface - spring

I want to implement few jpa repository for dynamo DB but at the same time i want to use few default spring data implementation (for mysql DB):-
class DynamoImpl implements SomeJPARepository {}
interface SomeJPARepository extends JPARepository{}
But i am getting error in log "found 2 implementation of SomeJPARepository" ,first DynamoImpl second
SomeJPARepository(Provided by spring data proxy) ?
How to stop JPA proxy impementation ?
I don't want to use #Qualifier due to high number of occurrence of this class in project .

Name them accordingly and use #Qualifier

Related

Is it possible to use Spring Data JPA projections together with QueryDSL?

Is there a way in Spring Data JPA to combine out of box its support for QueryDSL and Projections (projections being inferred from the defined classes/interfaces).
Even though I read here and there that the support was added in 2.6 (the version I am using in the project) I am not able to make it work with various attempts like:
Using projection type as QuerydslPredicateExecutor type parameter.
Problem: the returned instances are of the entity class type not of the projection class type.
Using findAllProjectedBy. Problem: java.lang.IllegalArgumentException: At least 1 parameter(s) provided but only 0 parameter(s) present in query. on startup (repo creation phase).
public interface FooRepo extends JpaRepository<Foo, Long>, QuerydslPredicateExecutor<FooProjection> { //(1)
List<FooProjection> findAllProjectedBy(Predicate p); //(2)
}
Spring Data JPA version - 2.6.1.
I saw also some SO answers with custom solutions but I am not interested in that approach (some even use deprecated Spring Data JPA implementation components).

Spring Boot : how auto configure works and #JsonTest

I've read some stuff about how auto-configuration works behind the scene (configuration classes with #Conditional, spring.factories inside /META-INF etc...)
Now I'm trying to understand with an example : #JsonTest
I can see this annotation is annotated with things like #AutoConfigureJson
What this #AutoConfigureJson does exactly ? Does it import some configuration classes with beans inside ? How Spring know how to use this annotation (basically this annotation is almost empty and doesn't say which classes to scan)
#AutoConfigure... (like #AutoConfigureJson) annotations are the way to allow tests with multiple "slices".
Slices load into your tests only a subset of the application, making them run faster. Let's say you need to test a component that uses the Jackson Object Mapper, then you would need the #JsonTest slice. (here is the list of all available slices.)
But you may also need some other part of the framework in your test not just tha single slice; let's say the JPA layer. You may want to annotate the test with both #JsonTest and #DataJpaTest to load both slices. According to the docs, this is not supported.
What you should do instead is choose one of the#...Test annotation, and include the other with an #AutoConfigure... annotation.
#JsonTest
#AutoConfigureDataJpa
class MyTests {
// tests
}
Update:
at a certain point while evaluating the annotation, Spring Boot will hit this line and will pass to the method SpringFactoriesLoader.loadFactoryNames() a source, that is the fully qualified name of the annotation (like interface org.springframework.boot.test.autoconfigure.json.AutoConfigureJson for example).
The loadFactoryNames method will do its magic and read the necessary information from here.
If more details are needed, the best thing is to use a debugger and just follow along all the steps.

Filter entities in spring repository

It is possible to apply a filter to results with annotations instead of extending method name?
For instance:
#Repository
public interface JobRepository extends JpaRepository<Job, Long> {
List<Job> findAllByUserAndEnabledIsTrue(User u);
}
Here I apply filter 'enabled == true'. But assume we have a lot of methods. Writing them with extended names is inconvenient. Could I apply this filter to whole repository?
I found
#FilterDef but I don't know how to use and also if spring support this annotation.
As far as I know Spring Data JPA is not Hibernate dependent, and it can work with any JPA implementation. Hibernate's #Filters is not a JPA standard, so the simple answer is no! Spring JPA does not support #Filters.
But you can apply #Filters using AOP, and simply applying aspects on your repository methods.
By the way I believe the better solution is to have hand written queries using Spring Data JPA's #Query annotation. Because this way you can name methods after their context meaning, and not about their internal implementation.
For example you can name your method findActiveJobsForUser which could be more meaningful and readable.

DataRepository and mybatis support

I have followed the guide react-js-and-spring-data-rest.
https://spring.io/blog/2015/10/28/react-js-and-spring-data-rest-part-5-security
This tutorial use JPA hibernate, I do really like the React/Api design, but I don't wan't to use JPA hibernate DAO, I would like to use Mybatis.
Is there a way to use spring DataRepository with mybatis ?
As far as I know there is not possible that way. Of course, you can use MyBatis-Spring-Boot-Starter integration which is not far different from Spring Data Repositories. It's not simply than DataRepository can be but not to more... For example one mapper should be:
#Mapper
public interface CityMapper {
#Select("SELECT * FROM CITY WHERE state = #{state}")
City findByState(#Param("state") String state);
}
Then you can inject it as a Bean Repository without implement:
#Autowired
private CityMapper cityMapper;
Unfortunately, you should do all Crud Operations in the entities you need... this could be tedious, so in de Data Repositories of Spring Data is not need.
The examples are in MyBatis Reference Documentation, and is much more explained than here.

inserting records in two databases using jpa + hibernate + spring

We have the requirement to insert records into two data sources using JPA.
What is the correct approach to this problem?
How can we declare different persistence units with different data sources and manage two entity managers and inject with default persistence context for default unit name without mentioning the unit name in the generic dao for first datasource and overriding the context with the unit name in a separate dao for other tables in second datasource.
The spring documentation is very limited and has no example.
Examples would be of great help.
Use
<bean class="..." primary="true" />
or #Primary if you are using annotations.
This will indicated that one instance is the "default" instance of a type that you define multiple different instances of (like your EntityManagerFactory).
If you follow this tutorial, http://javacodegeeks.blogspot.com/2010/05/jboss-42x-spring-3-jpa-hibernate.html you can make the following changes to access two different databases:
persistence.xml, define a second pesristence-unit for your second database.
spring.xml, define a second entityManagerFactory bean under a different name, lets say "entityManagerFactoryDB2" and configure it to use the persistent unit for the second database.
for every DAO you want to access the second database include the following :
#Autowired
private EntityManagerFactory entityManagerFactoryDB2;
#PostConstruct
public void init() {
super.setEntityManagerFactory(entityManagerFactoryDB2);
}
Thats all!
On spring service classes, use the DAOs as usual!

Resources