SlideShare une entreprise Scribd logo
1  sur  19
Data Repositories
Corneil du Plessis
corneil.duplessis@gmail.com
https://about.me/corneil
@corneil
Data Repositories: Introduction
Persistence made easy with Spring Data,
DeltaSpike Data and QueryDSL
Data Repositories: Introduction
● Assumptions
● What are repositories?
● Spring Data
● DeltaSpike Data
● QueryDSL
● Testing
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...
Before Data Repositories
● EntityManager
● NamedQuery
● JPAQL
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;
}
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();
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
Data Repositories: Spring Data
● interface JpaRepository<T, ID> {
boolean exists(ID id);
long count();
<S extends T> S save(S entity);
<S extends T> Iterable<S> save(Iterable<S> entities);
<S extends T> List<S> save(Iterable<S> slst);
<S extends T> S saveAndFlush(S v);
void flush();
List<T> findAll();
List<T> findAll(Sort s);
List<T> findAll(Iterable<ID> idLst);
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
Iterable<T> findAll(Iterable<ID> ids);
T findOne(ID id);
T getOne(ID id);
void delete(ID id);
void delete(T entity);
void delete(Iterable<? extends T> entities);
void deleteAll();
void deleteInBatch(Iterable<T> v);
void deleteAllInBatch();
}
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);
}
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);
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
Data Repositories: DeltaSpike Data
● Finder methods
– Equal, NotEqual, Like, GreaterThan,
GreaterThanEquals, LessThan, LessThenEquals,
Between, IsNull, IsNotNull
● @Query
– Pretty much the same as Spring Data.
Data Repositories: QueryDSL
● SQL / JPAQL are not type-safe.
● Criteria Queries are not type-safe.
● QueryDSL APT
● Generate query classes.
● Create Predicate in code.
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
}
Data Repository: Similarities
● DeltaSpike Data and Spring Data
– Transaction annotations
– @Modifying
– @Query
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.
Data Repositories: Demo
● Examples at
https://github.com/Jozi-JUG/data-repositories
● Vaadin Application
● Unit Tests
– JPA
– DeltaSpike Data
– Spring Data
– QueryDSL Data
Data Repositories: Questions
● Collaborate
– http://www.slideshare.net/CorneilduPlessis
– http://www.meetup.com/Jozi-JUG
– https://www.facebook.com/groups/jozijug
● Resources
– http://projects.spring.io/spring-data/
– http://deltaspike.apache.org/documentation/data.html
– http://www.querydsl.com/

Contenu connexe

Tendances

FITC presents: Mobile & offline data synchronization in Angular JS
FITC presents: Mobile & offline data synchronization in Angular JSFITC presents: Mobile & offline data synchronization in Angular JS
FITC presents: Mobile & offline data synchronization in Angular JSFITC
 
2nd Proj. Update: Integrating SWI-Prolog for Semantic Reasoning in Bioclipse
2nd Proj. Update: Integrating SWI-Prolog for Semantic Reasoning in Bioclipse2nd Proj. Update: Integrating SWI-Prolog for Semantic Reasoning in Bioclipse
2nd Proj. Update: Integrating SWI-Prolog for Semantic Reasoning in BioclipseSamuel Lampa
 
Modware
ModwareModware
Modwarebosc
 
Sql Considered Harmful
Sql Considered HarmfulSql Considered Harmful
Sql Considered Harmfulcoderanger
 
Reactive Access to MongoDB from Java 8
Reactive Access to MongoDB from Java 8Reactive Access to MongoDB from Java 8
Reactive Access to MongoDB from Java 8Hermann Hueck
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring DataArturs Drozdovs
 
Biomart Update
Biomart UpdateBiomart Update
Biomart Updatebosc
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBTobias Trelle
 
Introduction to hibernate
Introduction to hibernateIntroduction to hibernate
Introduction to hibernatehr1383
 
Query DSL In Elasticsearch
Query DSL In ElasticsearchQuery DSL In Elasticsearch
Query DSL In ElasticsearchKnoldus Inc.
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Ramamohan Chokkam
 
Dicoding Developer Coaching #18: Android | Membuat Aplikasi Pengingat dengan ...
Dicoding Developer Coaching #18: Android | Membuat Aplikasi Pengingat dengan ...Dicoding Developer Coaching #18: Android | Membuat Aplikasi Pengingat dengan ...
Dicoding Developer Coaching #18: Android | Membuat Aplikasi Pengingat dengan ...DicodingEvent
 

Tendances (20)

Json
Json Json
Json
 
JS basics
JS basicsJS basics
JS basics
 
FITC presents: Mobile & offline data synchronization in Angular JS
FITC presents: Mobile & offline data synchronization in Angular JSFITC presents: Mobile & offline data synchronization in Angular JS
FITC presents: Mobile & offline data synchronization in Angular JS
 
2nd Proj. Update: Integrating SWI-Prolog for Semantic Reasoning in Bioclipse
2nd Proj. Update: Integrating SWI-Prolog for Semantic Reasoning in Bioclipse2nd Proj. Update: Integrating SWI-Prolog for Semantic Reasoning in Bioclipse
2nd Proj. Update: Integrating SWI-Prolog for Semantic Reasoning in Bioclipse
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Modware
ModwareModware
Modware
 
Sql Considered Harmful
Sql Considered HarmfulSql Considered Harmful
Sql Considered Harmful
 
Suggest.js
Suggest.jsSuggest.js
Suggest.js
 
Reactive Access to MongoDB from Java 8
Reactive Access to MongoDB from Java 8Reactive Access to MongoDB from Java 8
Reactive Access to MongoDB from Java 8
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring Data
 
Ajax
AjaxAjax
Ajax
 
Biomart Update
Biomart UpdateBiomart Update
Biomart Update
 
Indexed db
Indexed dbIndexed db
Indexed db
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
 
Introduction to hibernate
Introduction to hibernateIntroduction to hibernate
Introduction to hibernate
 
Querydsl overview 2014
Querydsl overview 2014Querydsl overview 2014
Querydsl overview 2014
 
Query DSL In Elasticsearch
Query DSL In ElasticsearchQuery DSL In Elasticsearch
Query DSL In Elasticsearch
 
Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268Automatically generating-json-from-java-objects-java-objects268
Automatically generating-json-from-java-objects-java-objects268
 
Dicoding Developer Coaching #18: Android | Membuat Aplikasi Pengingat dengan ...
Dicoding Developer Coaching #18: Android | Membuat Aplikasi Pengingat dengan ...Dicoding Developer Coaching #18: Android | Membuat Aplikasi Pengingat dengan ...
Dicoding Developer Coaching #18: Android | Membuat Aplikasi Pengingat dengan ...
 

Similaire à Data repositories

Linq 090701233237 Phpapp01
Linq 090701233237 Phpapp01Linq 090701233237 Phpapp01
Linq 090701233237 Phpapp01google
 
MIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresMIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresSteven Johnson
 
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05CHOOSE
 
Language Integrated Query By Nyros Developer
Language Integrated Query By Nyros DeveloperLanguage Integrated Query By Nyros Developer
Language Integrated Query By Nyros DeveloperNyros Technologies
 
Designing and Building a Graph Database Application - Ian Robinson (Neo Techn...
Designing and Building a Graph Database Application - Ian Robinson (Neo Techn...Designing and Building a Graph Database Application - Ian Robinson (Neo Techn...
Designing and Building a Graph Database Application - Ian Robinson (Neo Techn...jaxLondonConference
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreIMC Institute
 
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoHasnain Iqbal
 
Rethinking Data-Intensive Science Using Scalable Analytics Systems
Rethinking Data-Intensive Science Using Scalable Analytics Systems Rethinking Data-Intensive Science Using Scalable Analytics Systems
Rethinking Data-Intensive Science Using Scalable Analytics Systems fnothaft
 
Spring Data JPA in detail with spring boot
Spring Data JPA in detail with spring bootSpring Data JPA in detail with spring boot
Spring Data JPA in detail with spring bootrinky1234
 
Etl Overview (Extract, Transform, And Load)
Etl Overview (Extract, Transform, And Load)Etl Overview (Extract, Transform, And Load)
Etl Overview (Extract, Transform, And Load)LizLavaveshkul
 
Reflected intelligence evolving self-learning data systems
Reflected intelligence  evolving self-learning data systemsReflected intelligence  evolving self-learning data systems
Reflected intelligence evolving self-learning data systemsTrey Grainger
 
Querydsl fin jug - june 2012
Querydsl   fin jug - june 2012Querydsl   fin jug - june 2012
Querydsl fin jug - june 2012Timo Westkämper
 
Linq Sanjay Vyas
Linq   Sanjay VyasLinq   Sanjay Vyas
Linq Sanjay Vyasrsnarayanan
 
Microsoft Ado
Microsoft AdoMicrosoft Ado
Microsoft Adooswchavez
 
From discovering to trusting data
From discovering to trusting dataFrom discovering to trusting data
From discovering to trusting datamarkgrover
 
Saket Saurabh at AI Frontiers: Data Operations or: How I Learned to Stop Data...
Saket Saurabh at AI Frontiers: Data Operations or: How I Learned to Stop Data...Saket Saurabh at AI Frontiers: Data Operations or: How I Learned to Stop Data...
Saket Saurabh at AI Frontiers: Data Operations or: How I Learned to Stop Data...AI Frontiers
 

Similaire à Data repositories (20)

LINQ
LINQLINQ
LINQ
 
Linq 090701233237 Phpapp01
Linq 090701233237 Phpapp01Linq 090701233237 Phpapp01
Linq 090701233237 Phpapp01
 
ORM JPA
ORM JPAORM JPA
ORM JPA
 
MIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresMIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome Measures
 
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
 
Language Integrated Query By Nyros Developer
Language Integrated Query By Nyros DeveloperLanguage Integrated Query By Nyros Developer
Language Integrated Query By Nyros Developer
 
Designing and Building a Graph Database Application - Ian Robinson (Neo Techn...
Designing and Building a Graph Database Application - Ian Robinson (Neo Techn...Designing and Building a Graph Database Application - Ian Robinson (Neo Techn...
Designing and Building a Graph Database Application - Ian Robinson (Neo Techn...
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : Datastore
 
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
 
Rethinking Data-Intensive Science Using Scalable Analytics Systems
Rethinking Data-Intensive Science Using Scalable Analytics Systems Rethinking Data-Intensive Science Using Scalable Analytics Systems
Rethinking Data-Intensive Science Using Scalable Analytics Systems
 
Spring Data JPA in detail with spring boot
Spring Data JPA in detail with spring bootSpring Data JPA in detail with spring boot
Spring Data JPA in detail with spring boot
 
Etl Overview (Extract, Transform, And Load)
Etl Overview (Extract, Transform, And Load)Etl Overview (Extract, Transform, And Load)
Etl Overview (Extract, Transform, And Load)
 
Reflected intelligence evolving self-learning data systems
Reflected intelligence  evolving self-learning data systemsReflected intelligence  evolving self-learning data systems
Reflected intelligence evolving self-learning data systems
 
Introduction to Datastore
Introduction to DatastoreIntroduction to Datastore
Introduction to Datastore
 
Querydsl fin jug - june 2012
Querydsl   fin jug - june 2012Querydsl   fin jug - june 2012
Querydsl fin jug - june 2012
 
Linq
LinqLinq
Linq
 
Linq Sanjay Vyas
Linq   Sanjay VyasLinq   Sanjay Vyas
Linq Sanjay Vyas
 
Microsoft Ado
Microsoft AdoMicrosoft Ado
Microsoft Ado
 
From discovering to trusting data
From discovering to trusting dataFrom discovering to trusting data
From discovering to trusting data
 
Saket Saurabh at AI Frontiers: Data Operations or: How I Learned to Stop Data...
Saket Saurabh at AI Frontiers: Data Operations or: How I Learned to Stop Data...Saket Saurabh at AI Frontiers: Data Operations or: How I Learned to Stop Data...
Saket Saurabh at AI Frontiers: Data Operations or: How I Learned to Stop Data...
 

Plus de Corneil du Plessis

Sweet Streams (Are made of this)
Sweet Streams (Are made of this)Sweet Streams (Are made of this)
Sweet Streams (Are made of this)Corneil du Plessis
 
Cloud Native Applications for Cloud Foundry using Spring Cloud : A Workshop
Cloud Native Applications for Cloud Foundry using Spring Cloud : A WorkshopCloud Native Applications for Cloud Foundry using Spring Cloud : A Workshop
Cloud Native Applications for Cloud Foundry using Spring Cloud : A WorkshopCorneil du Plessis
 
Enhancements in Java 9 Streams
Enhancements in Java 9 StreamsEnhancements in Java 9 Streams
Enhancements in Java 9 StreamsCorneil du Plessis
 
Performance Comparison JVM Languages
Performance Comparison JVM LanguagesPerformance Comparison JVM Languages
Performance Comparison JVM LanguagesCorneil du Plessis
 
Microservices Patterns and Anti-Patterns
Microservices Patterns and Anti-PatternsMicroservices Patterns and Anti-Patterns
Microservices Patterns and Anti-PatternsCorneil du Plessis
 
Gradle: The Build System you have been waiting for!
Gradle: The Build System you have been waiting for!Gradle: The Build System you have been waiting for!
Gradle: The Build System you have been waiting for!Corneil du Plessis
 
Gradle: The Build system you have been waiting for
Gradle: The Build system you have been waiting forGradle: The Build system you have been waiting for
Gradle: The Build system you have been waiting forCorneil du Plessis
 
Dependency Injection in Spring in 10min
Dependency Injection in Spring in 10minDependency Injection in Spring in 10min
Dependency Injection in Spring in 10minCorneil du Plessis
 

Plus de Corneil du Plessis (12)

Sweet Streams (Are made of this)
Sweet Streams (Are made of this)Sweet Streams (Are made of this)
Sweet Streams (Are made of this)
 
Cloud Native Applications for Cloud Foundry using Spring Cloud : A Workshop
Cloud Native Applications for Cloud Foundry using Spring Cloud : A WorkshopCloud Native Applications for Cloud Foundry using Spring Cloud : A Workshop
Cloud Native Applications for Cloud Foundry using Spring Cloud : A Workshop
 
QueryDSL - Lightning Talk
QueryDSL - Lightning TalkQueryDSL - Lightning Talk
QueryDSL - Lightning Talk
 
Enhancements in Java 9 Streams
Enhancements in Java 9 StreamsEnhancements in Java 9 Streams
Enhancements in Java 9 Streams
 
Reactive Spring 5
Reactive Spring 5Reactive Spring 5
Reactive Spring 5
 
Empathic API-Design
Empathic API-DesignEmpathic API-Design
Empathic API-Design
 
Performance Comparison JVM Languages
Performance Comparison JVM LanguagesPerformance Comparison JVM Languages
Performance Comparison JVM Languages
 
Microservices Patterns and Anti-Patterns
Microservices Patterns and Anti-PatternsMicroservices Patterns and Anti-Patterns
Microservices Patterns and Anti-Patterns
 
The Evolution of Java
The Evolution of JavaThe Evolution of Java
The Evolution of Java
 
Gradle: The Build System you have been waiting for!
Gradle: The Build System you have been waiting for!Gradle: The Build System you have been waiting for!
Gradle: The Build System you have been waiting for!
 
Gradle: The Build system you have been waiting for
Gradle: The Build system you have been waiting forGradle: The Build system you have been waiting for
Gradle: The Build system you have been waiting for
 
Dependency Injection in Spring in 10min
Dependency Injection in Spring in 10minDependency Injection in Spring in 10min
Dependency Injection in Spring in 10min
 

Dernier

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 

Dernier (20)

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

Data repositories

  • 1. Data Repositories Corneil du Plessis corneil.duplessis@gmail.com 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<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
  • 9. Data Repositories: Spring Data ● interface JpaRepository<T, ID> { boolean exists(ID id); long count(); <S extends T> S save(S entity); <S extends T> Iterable<S> save(Iterable<S> entities); <S extends T> List<S> save(Iterable<S> slst); <S extends T> S saveAndFlush(S v); void flush(); List<T> findAll(); List<T> findAll(Sort s); List<T> findAll(Iterable<ID> idLst); Iterable<T> findAll(Sort sort); Page<T> findAll(Pageable pageable); Iterable<T> findAll(Iterable<ID> ids); T findOne(ID id); T getOne(ID id); void delete(ID id); void delete(T entity); void delete(Iterable<? extends T> entities); void deleteAll(); void deleteInBatch(Iterable<T> v); void deleteAllInBatch(); }
  • 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 }
  • 16. Data Repository: Similarities ● DeltaSpike Data and Spring Data – Transaction annotations – @Modifying – @Query
  • 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
  • 19. Data Repositories: Questions ● Collaborate – http://www.slideshare.net/CorneilduPlessis – http://www.meetup.com/Jozi-JUG – https://www.facebook.com/groups/jozijug ● Resources – http://projects.spring.io/spring-data/ – http://deltaspike.apache.org/documentation/data.html – http://www.querydsl.com/