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

How to enable LockModeType.PESSIMISTIC_WRITE when looking up entities with Spring Data JPA?

@Lock is supported on CRUD methods as of version 1.6 of Spring Data JPA (in fact, there’s already a milestone available). See this ticket for more details. With that version you simply declare the following: interface WidgetRepository extends Repository<Widget, Long> { @Lock(LockModeType.PESSIMISTIC_WRITE) Widget findOne(Long id); } This will cause the CRUD implementation part of the … Read more

Pagination in Spring Data JPA (limit and offset)

Below code should do it. I am using in my own project and tested for most cases. usage: Pageable pageable = new OffsetBasedPageRequest(offset, limit); return this.dataServices.findAllInclusive(pageable); and the source code: import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; import org.springframework.data.domain.AbstractPageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import java.io.Serializable; /** * Created by Ergin **/ public class OffsetBasedPageRequest implements Pageable, … Read more

Missing CrudRepository#findOne method

Please see DATACMNS-944 which is associated to this commit which has the following renames ╔═════════════════════╦═══════════════════════╗ ║ Old name ║ New name ║ ╠═════════════════════╬═══════════════════════╣ ║ findOne(…) ║ findById(…) ║ ╠═════════════════════╬═══════════════════════╣ ║ save(Iterable) ║ saveAll(Iterable) ║ ╠═════════════════════╬═══════════════════════╣ ║ findAll(Iterable) ║ findAllById(…) ║ ╠═════════════════════╬═══════════════════════╣ ║ delete(ID) ║ deleteById(ID) ║ ╠═════════════════════╬═══════════════════════╣ ║ delete(Iterable) ║ deleteAll(Iterable) ║ ╠═════════════════════╬═══════════════════════╣ ║ exists() … Read more

Configure Multiple MongoDB repositories with Spring Data Mongo

The base idea is to separate the package hierarchy that contains your repositories into two different paths: com.whatever.repositories.main package for the main db repository interfaces com.whatever.repositories.secondary package for the other db repository interfaces Your XML configuration should be something such as: <mongo:repositories base-package=”com.whatever.repositories.main” mongo-template-ref=”mongoTemplate”/> <mongo:repositories base-package=”com.whatever.repositories.secondary” mongo-template-ref=”mongoAppTemplate”/> EDIT @EnableMongoRepositories annotation is not @Repeatable, but you … Read more