What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?

JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository. Their main functions are: CrudRepository mainly provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sorting records. JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch. Because of the inheritance mentioned above, JpaRepository will have all the functions … Read more

How does the FetchMode work in Spring Data JPA

I think that Spring Data ignores the FetchMode. I always use the @NamedEntityGraph and @EntityGraph annotations when working with Spring Data @Entity @NamedEntityGraph(name = “GroupInfo.detail”, attributeNodes = @NamedAttributeNode(“members”)) public class GroupInfo { // default fetch mode is lazy. @ManyToMany List<GroupMember> members = new ArrayList<GroupMember>(); … } @Repository public interface GroupRepository extends CrudRepository<GroupInfo, String> { @EntityGraph(value … Read more

POSTing a @OneToMany sub-resource association in Spring Data REST

Assuming you already have discovered the post URI and thus the URI of the association resource (considered to be $association_uri in the following), it generally takes these steps: Discover the collection resource managing comments: curl -X GET http://localhost:8080 200 OK { _links : { comments : { href : “…” }, posts : { href … Read more

Spring Data JPA – ZonedDateTime format for json serialization

I guess that you are using Jackson for json serialization, Jackson now has a module for Java 8 new date time API, https://github.com/FasterXML/jackson-datatype-jsr310. Add this dependency into your pom.xml <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.6.0</version> </dependency> And this is its usage: public static void main(String[] args) throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); System.out.println(objectMapper.writeValueAsString(new … Read more

How to do bulk (multi row) inserts with JpaRepository?

To get a bulk insert with Spring Boot and Spring Data JPA you need only two things: set the option spring.jpa.properties.hibernate.jdbc.batch_size to appropriate value you need (for example: 20). use saveAll() method of your repo with the list of entities prepared for inserting. Working example is here. Regarding the transformation of the insert statement into … Read more

setMaxResults for Spring-Data-JPA annotation?

As of Spring Data JPA 1.7.0 (Evans release train). You can use the newly introduced Top and First keywords that allow you to define query methods like this: findTop10ByLastnameOrderByFirstnameAsc(String lastname); Spring Data will automatically limit the results to the number you defined (defaulting to 1 if omitted). Note that the ordering of the results becomes … Read more