SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Downloaden Sie, um offline zu lesen
@jkubrynski / kubrynski.com
JPA - BEYOND COPY-PASTE
JAKUB KUBRYNSKI
jk@devskiller.com / @jkubrynski / http://kubrynski.com
WHOAMI
CO-FOUNDER OF DEVSKILLER / CODEARTE
TRAINER AT BOTTEGA
DEVOXX.PL PROGRAM COMMITTEE
FORMER CONFITURA.PL
@Entity
public class Product {
    @Id
    @GeneratedValue 
    private Long id; 
    @Column
    private String name; 
    // ...
}
@Entity
public class Product {
    @Id
    @GeneratedValue 
    private Long id; 
    @Column // it does exactly nothing 
    private String name; 
    // ...
}
@Transactional @Service 
public class ProductService { 
    public void updatePrice(Long productId, Money newPrice) { 
        Product product = productRepository.find(productId); 
        product.setPrice(newPrice); 
        productRepository.save(product); 
    }
}
@Transactional @Service 
public class ProductService { 
    public void updatePrice(Long productId, Money newPrice) { 
        Product product = productRepository.find(productId); 
        product.setPrice(newPrice); 
        productRepository.save(product); // it does exactly nothing 
    }
}
FIELDS VS GETTERS
@Entity
public class Product {
    private Long id; 
    private String name; 
    @Id
    public Long getId() {
        return id; 
    }
}
MIXED ACCESS
@Entity
@Access(AccessType.FIELD) 
public class Product {
    private Long id; 
    private String name; 
    @Id
    @Access(AccessType.PROPERTY) 
    public Long getId() {
        return id; 
    }
}
@Entity
public class Order {
}
@Entity(name = "orders") 
public class Order {
}
@Entity(name = "orders") 
public class Order {
}
insert into orders ... 
@Entity(name = "orders") 
public class Order {
}
em.createQuery("SELECT o FROM Order o") 
@Entity(name = "orders") 
public class Order {
}
em.createQuery("SELECT o FROM orders o") 
@Entity
@Table(name = "orders") 
public class Order {
}
ONE TO ONE
@Entity
public class Customer { 
    @OneToOne 
    private Address address; 
}
@Entity
public class Address {
    @OneToOne 
    private Customer customer; 
}
ONE TO ONE
@Entity
public class Customer { 
    @OneToOne 
    private Address address; 
}
@Entity
public class Address {
    @OneToOne(mappedBy = "address") 
    private Customer customer; 
}
ONE TO MANY
@Entity
public class Customer { 
    @OneToMany 
    private Set<Address> addresses; 
}
ONE TO MANY
@Entity
public class Customer { 
    @OneToMany 
    @JoinColumn(name = "customer_id") 
    private Set<Address> addresses; 
}
MANY TO MANY
@Entity
public class Customer { 
    @ManyToMany 
    private Collection<Address> addresses; 
}
@Entity
public class Address {
    @ManyToMany 
    private Collection<Customer> customers; 
}
LAZY LOADING
element is loaded only when required
proxy by subclassing, but not always needed
LAZY LOADING
Custom collections
PersistentSet
PersistentBag
PersistentList
SET, BAG OR LIST?
@OneToMany 
Set<Product> products; 
SET, BAG OR LIST?
@OneToMany 
List<Product> products; 
SET, BAG OR LIST?
@OneToMany 
@OrderColumn 
List<Product> products; 
N+1
@Entity
public class User {
    @OneToMany 
    private List<Address> addresses; 
}
List<User> users = em.createQuery("SELECT u FROM User u").getResultList(); 
for (User user : users) { 
    for (Address address : user.getAddresses()) { 
        ... 
    }
}
N+1
@Entity
public class User {
    @OneToMany 
    @BatchSize(size = 10) 
    private List<Address> addresses; 
}
N+1
SELECT DISTINCT u FROM User u 
    JOIN FETCH u.addresses 
HOW TO SAVE
EntityManager.persist()
EntityManager.merge()
OPTIMISTIC LOCKING
@Entity
public class User {
   @Version 
   private int version; 
}
OPTIMISTIC LOCKING IN REST
NEVER HEARD OF IT
IDENTITY
EQUALS() AND HASHCODE()
BASE CLASS
@MappedSuperclass 
public abstract class BaseEntity implements Serializable { 
    @Id @GeneratedValue 
    private Long id; 
    private String uuid = UUID.randomUUID().toString(); 
    public int hashCode() { 
        return Objects.hash(uuid); 
    }
    public boolean equals(Object that) { 
        return this == that || that instanceof BaseEntity 
                    && Objects.equals(uuid, ((BaseEntity) that).uuid); 
     }
}
CACHING
L1 - EntityManager cache / Session cache
L2 - EntityManagerFactory cache / SessionFactory cache
QueryCache
FLUSH MODES
MANUAL
COMMIT
AUTO
ALWAYS
LEVEL 1 CACHE PITFALLS
FLUSH() AND CLEAR()
LEVEL 2 CACHE PITFALLS
DISTRIBUTED ENVIRONMENT
HQL INJECTION
String hqlQuery = "SELECT p FROM Product p where p.category = '" + cat + "'"; 
List<Product> products = em.createQuery(hqlQuery, Product.class) 
                           .getResultList(); 
DYNAMIC UPDATES
@Entity
@DynamicUpdate 
public class MyEntity { 
// ...
}
DYNAMIC INSERTS
@Entity
@DynamicInsert 
public class MyEntity { 
// ...
}
QUESTIONS?
THANKS

Weitere ähnliche Inhalte

Was ist angesagt?

JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
Guillaume Laforge
 

Was ist angesagt? (20)

Cool Jvm Tools to Help you Test - Aylesbury Testers Version
Cool Jvm Tools to Help you Test - Aylesbury Testers VersionCool Jvm Tools to Help you Test - Aylesbury Testers Version
Cool Jvm Tools to Help you Test - Aylesbury Testers Version
 
Mobile Open Day: React Native: Crossplatform fast dive
Mobile Open Day: React Native: Crossplatform fast diveMobile Open Day: React Native: Crossplatform fast dive
Mobile Open Day: React Native: Crossplatform fast dive
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UK
 
GWT Extreme!
GWT Extreme!GWT Extreme!
GWT Extreme!
 
Gradle in 45min
Gradle in 45minGradle in 45min
Gradle in 45min
 
Groovy & Grails: Scripting for Modern Web Applications
Groovy & Grails: Scripting for Modern Web ApplicationsGroovy & Grails: Scripting for Modern Web Applications
Groovy & Grails: Scripting for Modern Web Applications
 
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
JavaOne 2008 - TS-5793 - Groovy and Grails, changing the landscape of Java EE...
 
Android antipatterns
Android antipatternsAndroid antipatterns
Android antipatterns
 
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
 
Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++
 
Doctrine ORM with eZ Platform REST API and GraphQL
Doctrine ORM with eZ Platform REST API and GraphQLDoctrine ORM with eZ Platform REST API and GraphQL
Doctrine ORM with eZ Platform REST API and GraphQL
 
Kotlin intro
Kotlin introKotlin intro
Kotlin intro
 
A Taste of Clojure
A Taste of ClojureA Taste of Clojure
A Taste of Clojure
 
JAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) BridgeJAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) Bridge
 
ABCs of docker
ABCs of dockerABCs of docker
ABCs of docker
 
Effective Doctrine2: Performance Tips for Symfony2 Developers
Effective Doctrine2: Performance Tips for Symfony2 DevelopersEffective Doctrine2: Performance Tips for Symfony2 Developers
Effective Doctrine2: Performance Tips for Symfony2 Developers
 
Angular beans
Angular beansAngular beans
Angular beans
 
CoffeeScript By Example
CoffeeScript By ExampleCoffeeScript By Example
CoffeeScript By Example
 
Apache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemApache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystem
 
groovy & grails - lecture 6
groovy & grails - lecture 6groovy & grails - lecture 6
groovy & grails - lecture 6
 

Ähnlich wie JPA - Beyond copy-paste

Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014
Antoine Sabot-Durand
 

Ähnlich wie JPA - Beyond copy-paste (20)

JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-pasteJDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
JDD 2016 - Jakub Kubrynski - Jpa - beyond copy-paste
 
VRaptor 4 - JavaOne
VRaptor 4 - JavaOneVRaptor 4 - JavaOne
VRaptor 4 - JavaOne
 
1/3 : introduction to CDI - Antoine Sabot-Durand
1/3 : introduction to CDI - Antoine Sabot-Durand1/3 : introduction to CDI - Antoine Sabot-Durand
1/3 : introduction to CDI - Antoine Sabot-Durand
 
CDI e as ideias pro futuro do VRaptor
CDI e as ideias pro futuro do VRaptorCDI e as ideias pro futuro do VRaptor
CDI e as ideias pro futuro do VRaptor
 
Jakarta EE: The First Parts
Jakarta EE: The First PartsJakarta EE: The First Parts
Jakarta EE: The First Parts
 
Jakarta EE : The First Parts
Jakarta EE : The First PartsJakarta EE : The First Parts
Jakarta EE : The First Parts
 
Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014
 
C# 6.0 - What?! C# is being updated?
C# 6.0 - What?! C# is being updated?C# 6.0 - What?! C# is being updated?
C# 6.0 - What?! C# is being updated?
 
CDI in JEE6
CDI in JEE6CDI in JEE6
CDI in JEE6
 
CDI @javaonehyderabad
CDI @javaonehyderabadCDI @javaonehyderabad
CDI @javaonehyderabad
 
Boost Your Development With Proper API Design
Boost Your Development With Proper API DesignBoost Your Development With Proper API Design
Boost Your Development With Proper API Design
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmers
 
Introduction to CDI
Introduction to CDIIntroduction to CDI
Introduction to CDI
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
 
Creating a Facebook Clone - Part XX - Transcript.pdf
Creating a Facebook Clone - Part XX - Transcript.pdfCreating a Facebook Clone - Part XX - Transcript.pdf
Creating a Facebook Clone - Part XX - Transcript.pdf
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
當ZK遇見Front-End
當ZK遇見Front-End當ZK遇見Front-End
當ZK遇見Front-End
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
 

Mehr von Jakub Kubrynski

Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
Jakub Kubrynski
 
Warsjawa profiling tools
Warsjawa profiling toolsWarsjawa profiling tools
Warsjawa profiling tools
Jakub Kubrynski
 
JOOX - Java Object Oriented XML
JOOX - Java Object Oriented XMLJOOX - Java Object Oriented XML
JOOX - Java Object Oriented XML
Jakub Kubrynski
 

Mehr von Jakub Kubrynski (14)

Work sample coding tests
Work sample coding testsWork sample coding tests
Work sample coding tests
 
6 key tips for conducting an effective skill assessment interview
6 key tips for conducting an effective skill assessment interview6 key tips for conducting an effective skill assessment interview
6 key tips for conducting an effective skill assessment interview
 
-XX:+UseG1GC
-XX:+UseG1GC-XX:+UseG1GC
-XX:+UseG1GC
 
Consumer Driven Contracts - 4Developers 2015
Consumer Driven Contracts - 4Developers 2015Consumer Driven Contracts - 4Developers 2015
Consumer Driven Contracts - 4Developers 2015
 
REST - the good and the bad parts
REST - the good and the bad partsREST - the good and the bad parts
REST - the good and the bad parts
 
REST - the good and the bad parts
REST - the good and the bad partsREST - the good and the bad parts
REST - the good and the bad parts
 
What you won't read in books about RESTful services
What you won't read in books about RESTful servicesWhat you won't read in books about RESTful services
What you won't read in books about RESTful services
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Warsjawa profiling tools
Warsjawa profiling toolsWarsjawa profiling tools
Warsjawa profiling tools
 
Warsjawa profiling
Warsjawa profilingWarsjawa profiling
Warsjawa profiling
 
JOOX - Java Object Oriented XML
JOOX - Java Object Oriented XMLJOOX - Java Object Oriented XML
JOOX - Java Object Oriented XML
 
Arquillian
ArquillianArquillian
Arquillian
 
Spring Data
Spring DataSpring Data
Spring Data
 
Profiling
ProfilingProfiling
Profiling
 

Kürzlich hochgeladen

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 

Kürzlich hochgeladen (20)

WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 

JPA - Beyond copy-paste