Share
Embed
Spring Data JPA by Cedric Arnoult
SingaSUG
March 10, 2015
Transcript
11 slides
-
Spring Data JPA presentation Singapore Spring User group March 10th
2015 Cedric Arnoult [email protected] -
2 What are we going to talk about ? JPA
Spring Data JPA Demo Spring Data JPA in our Project Non JPA era Q&A Pros and cons How does it work -
3 Non JPA era JDBC : Lots of boilerplate
code Connection Open Close Statement Create ResultSet Loop through POJO mapping Lots and lots of try catch, including catch in the try or the finally (don’t forget the finally !!!) Spring JDBC templates Less code Still had to go through ResultSet -
4 JPA What is it ? Standard way
to access data layer providing a JPA provider Hibernate EclipseLink OpenJPA … JPA Query language (JPQL) Focus on mappings and relationships What is it not ? Adressing boilerplate code getters and setters on your entities EntityManagerFactory.getTransaction.begin(), persist(), commit() and close() Lots of try ctach … -
5 Spring Data JPA What is it not ?
a JPA provider a big huge thing with lots of things to learn What is it ? A promise : a way to improve the implementation of your data access layer, writing less code. A project built on top of Spring Data. Spring Data MongoDB Spring Data Rest Spring for Hadoop … What do you need ? 2 jars : spring-data-jpa (latest version is 1.7.2-RELEASE) spring-data-commons (latest version is 1.8.2-RELEASE) Spring JPA Entities and a JPA provider Introduction -
6 Spring Data JPA Key is : org.springframework.data.jpa.JpaRepository
One interface to extend : It will bring to you : Every basic CRUD methods covered at once Even the not so basic ones Sorting Pagination Lots of helpers based on reflection Yes, you can still have custom queries One magic line of code -
8 How does it work Proxy generated at bootstrap
time Scans your Spring configuration for your repositories Relies heavily on Spring Data JpaRepository extends PagingAndSortingRepository PagingAndSortingRepository extends CrudRepository CrudRepository extends Repository SimpleJpaRepository is the default implementation of JpaRepository It’s not magical … but almost -
9 Pros and cons Pros All basic queries
are done for you Abstraction allows you to change your relation model quite easily Pagination and sorting work out of the box Custom queries with @Query are validated at bootstrap time You cannot go live with a query that is grammatically wrong Cons Don’t forget about Spring Data if you don’t want to get tied up with JPA You might want to consider QueryDsl : Domain Specific Language. A new way way to do your queries -
10 Spring Data JPA in our Project Forget the
boilerplate code Learning curve is almost inexistant Easy Fast Helped us to structure the code 1 entity = 1 repository Coding standards (‘findxxx’ methods) ? Performances are JPA performances Hand made repositories are still possible Still had to go out of JPA for 2 specific repositories Did not use reflection based helpers Less tests All CRUD methods -
11 Q&A Spring data : http://projects.spring.io/spring-data/ Spring data
JPA : http://projects.spring.io/spring-data-jpa/ Contact me [email protected] [email protected]