How to I tell a Springdata-repository’s delete method to not throw an exception if an entity does not exists?

Updated Answer (after downvotes) My original answer (below) is actually wrong: my understanding of the question was influenced also by the missing reference to the EmptyResultDataAccessException in the official JavaDoc (as reported by Adrian Baker in his comment). So a better solution to this issue could be the one suggested by Yamashiro Rion if (repository.existsById(entityId)) … Read more

Is there a way to use constants inside Spring Data @Query annotation value?

You have to use fully qualified class name like this: @Query(“SELECT u FROM UserModel u WHERE u.status = com.example.package.UserModel.STATUS_ACTIVE”) The bad thing about it though is that an IDE would not recognise this as an usage of the class UserModel. The only advantage is that you can keep the value in one place, which is … Read more

How do you use Spring Data JPA outside of a Spring Container?

The general principle behind the design of JpaRepositoryFactory and the according Spring integration JpaRepositoryFactory bean is the following: We’re assuming you run your application inside a managed JPA runtime environment, not caring about which one. That’s the reason we rely on injected EntityManager rather than an EntityManagerFactory. By definition the EntityManager is not thread safe. … Read more

Best way of handling entities inheritance in Spring Data JPA

I used the solution also described in this post from Netgloo’s blog. The idea is to create a generic repository class like the following: @NoRepositoryBean public interface ABaseRepository<T extends A> extends CrudRepository<T, Long> { // All methods in this repository will be available in the ARepository, // in the BRepository and in the CRepository. // … Read more

Spring Boot, Spring Data JPA with multiple DataSources

There is another way to have multiple dataSources by using @EnableAutoConfiguration and application.properties. Basically put multiple dataSource configuration info on application.properties and generate default setup (dataSource and entityManagerFactory) automatically for first dataSource by @EnableAutoConfiguration. But for next dataSource, create dataSource, entityManagerFactory and transactionManager all manually by the info from property file. Below is my example … Read more