Multiple Repositories for the Same Entity in Spring Data Rest

The terrible part is not only that you can only have 1 spring data rest repository (@RepositoryRestResource) per Entity but also that if you have a regular JPA @Repository (like CrudRepository or PagingAndSorting) it will also interact with the spring data rest one (as the key in the map is the Entity itself). Lost quite … Read more

Spring – Multiple Spring Data modules found, entering strict repository configuration mode

Sorry for answering too late, but I think answers that present already does not explain the actual, deep reason why is it happening. Let me explain everything in order to let you understand the guts (be ready, it will be quite long and comprehensive). If you are looking for simple solution, go to the bottom … Read more

Spring io @Autowired: The blank final field may not have been initialized

Having @Autowired and final on a field are contradictory. The latter says: this variable has one and only one value, and it’s initialized at construction time. The former says: Spring will construct the object, leaving this field as null (its default value). Then Spring will use reflection to initialize this field with a bean of … Read more

Spring-Data JPA: save new entity referencing existing one

I had a similar issue where I was trying to save an new entity object with an already saved entity object inside. What I did was implemented Persistable< T > and implemented isNew() accordingly. public class MyEntity implements Persistable<Long> { public boolean isNew() { return null == getId() && subEntity.getId() == null; } Or you … Read more

Spring Retry with Transactional

Found the answer here: https://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/transaction.html#tx-decl-explained Table 2 indicates that the advice for the Transactional annotation has an order of Ordered.LOWEST_PRECEDENCE, which means that it is safe to combine Retryable with Transactional as long as you aren’t overriding the order of the advice for either of those annotations. In other words, you can safely use this … Read more

java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;

This error occurs because JPA 1 API is brought in, but method getValidationMode exists only since JPA 2. Instead of following <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0.2</version> </dependency> for example one offered by Hibernate can be used: <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency>

Spring MongoRepository is updating or upserting instead of inserting

Save, by definition, is supposed to update an object in the upsert style, update if present and insert if not. Read the save operation documentation on the MongoDb website The insert operation in mongodb has the behavior you expect, but from the MongoRepository documentation it appears that insert is delegated to save so it won’t … Read more

Spring Data JPA Projection selected fields from the DB

If you want use the annotation @Query with Spring Data Projections you have to use field alias and you need to make sure you alias the projects matching the projection fields. The following code should work for question 1: interface PersonRepository extends CrudRepository<Person, Long> { @Query(“select p.firstName as firstname, p.address as address from Person p … Read more