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...
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<Contact> query = entityManager.createNamedQuery(
"Contact.findByCompanyName", Contact.class);
query.setParameter("company", "BBD");
List<Contact> 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
10. Data Repositories: Spring Data
● Basic repository:
public interface ContactRepository extends
JpaRepository<Contact, Long> {
}
● Add behaviour:
public interface ContactRepository extends
JpaRepository<Contact, Long> {
public Long countByCompany_Name(String name);
public List<Contact> findByCompany_Name(String name);
}
11. Data Repositories: Spring Data
● Finder methods
– And, Or, Is, Equals, Between, LessThan, LessThanEqual,
GreaterThan, GreaterThanEqual, After, Before, IsNull,
IsNotNull, NotNull, Like, NotLike, StartingWith, EndingWith,
Containing, Not, In, NotIn, True, False, IgnoreCase
● @Query("select c from Contact c where c.company.name = ?1")
public List<Contact> findByCompanyName(String name);
● @Query("select c from Contact c where c.company.name =
:companyName")
public List<Contact>
findByCompanyName( @Param("companyName") String name);
12. Data Repositories: DeltaSpike Data
● Slightly different:
@Repository(forEntity = Contact.class)
public interface ContactRepository extends
EntityRepository<Contact, Long> {
QueryResult<Contact> findByCompany_name(
String companyName);
}
● QueryResult can be used for finer control of result manipulation like:
– LockMode
– Ordering
– Count
– First, Max
– Paging
13. Data Repositories: DeltaSpike Data
● Finder methods
– Equal, NotEqual, Like, GreaterThan,
GreaterThanEquals, LessThan, LessThenEquals,
Between, IsNull, IsNotNull
● @Query
– Pretty much the same as Spring Data.
14. Data Repositories: QueryDSL
● SQL / JPAQL are not type-safe.
● Criteria Queries are not type-safe.
● QueryDSL APT
● Generate query classes.
● Create Predicate in code.
15. Data Repositories: QueryDSL
● Add QueryDslPredicateExecutor:
public interface ContactRepository extends
JpaRepository<Contact, Long>,
QueryDslPredicateExecutor<Contact> {
}
● Configure QueryDSL APT in build
● Use Q<classes>:
Predicate p = contact.company.name.eq("BBD");
long count = contactRepository.count(p);
// OR
for(Contact contact : contactRepository.findAll(p)) {
// use contact
}
17. Data Repository: Differences
● DeltaSpike
– QueryResult
– Fine control of EntityManager
– Mapping
● Spring Data JPA
– More options on finder methods.
● QueryDSL
– Can be used with Spring Data and DeltaSpike Data
– Type safety.
18. Data Repositories: Demo
● Examples at
https://github.com/Jozi-JUG/data-repositories
● Vaadin Application
● Unit Tests
– JPA
– DeltaSpike Data
– Spring Data
– QueryDSL Data