Data repositories

download Data repositories

If you can't read please download the document

Transcript of Data repositories

1. Data Repositories Corneil du Plessis [email protected] https://about.me/corneil @corneil 2. Data Repositories: Introduction Persistence made easy with Spring Data, DeltaSpike Data and QueryDSL 3. Data Repositories: Introduction Assumptions What are repositories? Spring Data DeltaSpike Data QueryDSL Testing 4. Data Repositories: Assumptions You understand relational databases. You have worked with JDBC. You have worked with JPA. You know some SQL. We are going to limit the discussion to JPA... 5. Before Data Repositories EntityManager NamedQuery JPAQL 6. JPA Example @Entity class Company { String name; } @Entity @NamedQueries({ @NamedQuery( name = "Contact.findByCompanyName", query = "select c from Contact c where c.company.name = :company") }) class Contact { String name; String phone; String email; Company company; } 7. JPA Example Query query = entityManager.createQuery( "select count(c) from Contact c where " + "c.company.name = 'BBD'"); Number result = (Number) query.getSingleResult(); TypedQuery query = entityManager.createNamedQuery( "Contact.findByCompanyName", Contact.class); query.setParameter("company", "BBD"); List results = query.getResultList(); 8. Data Repositories: What are repositories? CRUD? Create, Read, Update, Delete Do you like? Data Access Components Named Queries Why write the boilerplate code over and over? Why write implementation if it is obvious? What should a DAO / DAC look like? Repositories Consistent methods. Consistent behaviour. Consistent transactions. Readable Efficient 9. Data Repositories: Spring Data interface JpaRepository { boolean exists(ID id); long count(); S save(S entity); Iterable save(Iterable entities); List save(Iterable slst); S saveAndFlush(S v); void flush(); List findAll(); List findAll(Sort s); List findAll(Iterable idLst); Iterable findAll(Sort sort); Page findAll(Pageable pageable); Iterable findAll(Iterable ids); T findOne(ID id); T getOne(ID id); void delete(ID id); void delete(T entity); void delete(Iterable