Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Data - DAOs von Zauberhand

Spring Data - DAOs von Zauberhand

Welcher Entwickler hat sich nicht schon mal über den vielen redundanten Code in Data Access Objects (DAOs) geärgert? Spring Data bietet eine komfortable Alternative: Die gängigen CRUD-Methoden werden durch die Verwendung des passenden Java-Interfaces bereitgestellt und auch ausgefallene Queries lassen sich ohne großen Aufwand realisieren. Anhand eines praktischen Beispiels demonstrieren wir die Verwendung dieses mächtigen Werkzeugs.

Die Präsentation wurde im Rahmen der cosee TechTalks gehalten. Den Code und alle weiteren Talks findest du auf https://talks.cosee.biz/

Dirk Kroehan

May 19, 2016
Tweet

More Decks by Dirk Kroehan

Other Decks in Programming

Transcript

  1. Was ist Spring Data? „Spring Data’s mission is to provide

    a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store. „
  2. JPA MongoDB Redis Solr Gemfire KeyValue REST Aerospike* Cassandra* Couchbase*

    DynamoDB* Elasticsearch* Neo4J* * = Community modules
  3. @Repository public class CandyDAO { @PersistenceContext private EntityManager em; @Transactional

    public Candy save(Candy candy) { if (null == candy.getId()) { em.persist(candy); return candy; } else { return em.merge(candy); } } public Candy findById(long id) { TypedQuery<Candy> query = em.createQuery("select c from Candy c where c.id = :id", Candy.class); query.setParameter("id", id); return query.getSingleResult(); } } Plain JPA
  4. The power of CrudRepository public interface CrudRepository<T, ID extends Serializable>

    extends Repository<T, ID> { <S extends T> S save(S entity); <S extends T> Iterable<S> save(Iterable<S> entities); T findOne(ID id); boolean exists(ID id); Iterable<T> findAll(); Iterable<T> findAll(Iterable<ID> ids); long count(); ….
  5. The power of CrudRepository … void delete(ID id); void delete(T

    entity); void delete(Iterable<? extends T> entities); void deleteAll(); }
  6. JpaRepository public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID>

    { List<T> findAll(); List<T> findAll(Sort sort); List<T> findAll(Iterable<ID> ids); <S extends T> List<S> save(Iterable<S> entities); void flush(); <S extends T> S saveAndFlush(S entity); void deleteInBatch(Iterable<T> entities); void deleteAllInBatch(); T getOne(ID id); }
  7. Neues in Spring Data 1.10 • Support for Projections in

    repository query methods. • Support for Query by Example. • The following annotations have been enabled to build own, composed annotations: @EntityGraph, @Lock, @Modifying, @Query, @QueryHints and @Procedure. • Support for Contains keyword on collection expressions. • AttributeConverters for ZoneId of JSR-310 and ThreeTenBP. • Upgrade to Querydsl 4, Hibernate 5, OpenJPA 2.4 and EclipseLink 2.6.1.