Documente Academic
Documente Profesional
Documente Cultură
Spring-Data JPA
Miya W. Longwe, Tech Lead, Staples Inc.
January 07, 2014
Java Meetup Group, Cambridge, MA , USA
Agenda
Java DB Access Ordeal
Enter Spring-Data
Q and A
Java DB Access
The Developers Holy Pilgrim!
Application Domain
Domain driven design has become a
ubiquitous approach to tackle
complex problem domains and build
a rich object model.
Implementing a data access layer of
an application has been cumbersome
for quite a while.
Too much boilerplate code has to be
written.
Code to execute simple queries as
well as perform pagination, auditing,
etc
Java DB Access
The Boilerplate Code
/**
* Plain JPA implementation of {@link CustomerService}.
*
* @author Miya W Longwe
*/
@Repository
@Transactional(readOnly = true)
public class CustomerServiceImpl implements CustomerService {
@PersistenceContext
private EntityManager em;
@Override
public Customer findById(Long id) {
return em.find(Customer.class, id);
}
@Override
public List<Customer> findAll() {
return em.createQuery("select c from Customer c", Customer.class).getResultList();
}
Spring-Data
Uses the Repository abstraction for
data access
Repository Interface
* Specifi
es m ethods used to obtain and
m odify person related inform ation
* w hich is stored in the database.
* @ au th or M iya W . Lon g w e
*/
p u b lic in terface P erson R ep ository
exten d s Jp aR ep ository< P erson , Lon g >
{
}
Spring-Data Features
Provided Repositories
Spring-Data JPA provides two
repositories
CrudRepository
Long list of standard CRUD operations
provided
findOne(), findAll(), save(), delete(),
exists(), etc
PagingAndSortingRepository
Derived from CrudRepository
Spring-Data
Method Name-to-JPQL Mapping
Method Name
Generated JPQL
findByXxxAndYyy(aaa,
bbb)
findByXxxOrYyy(aaa,
bbb)
findByXxxStartingWith(a
aa)
findByXxxNot(aaa)
findByXxxIn(Collection<
E>aaa)
...where t.xxx in ?1
Spring-Data
Further Property Parsing Features
-Traversal can reach into nested properties
-Will do best effort using camelCase
-You can delineate properties using _
}
@ Entity
public class U ser < Long> {
private ZiC ode zicode;
--}
}
public interface U serRepository extends
CrudRepository< U ser, Long> {
...
U ser fi
n dByAddress_ZipCode(ZipCode
zipCode);
...
Result Pagination
Seamlessly provides support for result set pagination
via Pageable Interface
Define repository method with Pageable
Call method with PageRequest class (or define your
own)
public interface ProductRepository extends CrudRepository< U ser,
Long> {
...
Page< Product> fi
ndAll(Pageable pageable);
...
}
class ProductService {
Pageable pageable = new PageRequest(1, 20);
Page<Product> page = repository.findByDescriptionContaining(pageable);
}
Custom Repositories
When Spring-Data JPA derived queries are not
enough or you need additional logic
Provide your own repository implementation
A bean that lives in Spring Context
interface U serRepositoryCustom {
List< U ser> m yCustom BatchO peration();
}
class U serRepositoryIm plim plem ents U serRepositoryCustom {
@ PersistenceC ontext
private EntityM anager em ;
public List< U ser> m yC ustom BatchO peration() {
CriteriaQ uery< U ser> criteriaQ uery =
em .getCriteriaBuilder().createQ uery(U ser.class);
return em .createQ uery(criteriaQ uery).getResultList();
}
}
Transaction Support
Repository classes are transactional by default
Reads are made readOnly
Other methods are @Transactional by default
Ability to override by providing your own
@Transactional demarcation
public interface U serRepository extends CrudRepository< U ser,
Long> {
...
@ Transactional(tim eout= 10)
@ M odifying
@ Q uery("update U ser u set u.fi
rstnam e = ?1 w here
u.lastnam e = ?2")
U ser fi
xFirstN am eByLastN am e(String fi
rstnam e, String
lastnam e);
...
}
Code Demo