Data Persistence Layer

Slides:



Advertisements
Similar presentations
V 6, Mats Strandberg ORM With Hibernate.
Advertisements

Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
Spring, Hibernate and Web Services 13 th September 2014.
Fast Track to ColdFusion 9. Getting Started with ColdFusion Understanding Dynamic Web Pages ColdFusion Benchmark Introducing the ColdFusion Language Introducing.
© 2005, Cornell University. Rapid Application Development using the Kuali Architecture (Struts, Spring and OJB) A Case Study Bryan Hutchinson
Session-01. Hibernate Framework ? Why we use Hibernate ?
An Introduction to Hibernate Matt Secoske
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
Hibernatification! Roadmap for Migrating from Plain Old SQL on JDBC to JPA on Hibernate Duke Banerjee Senior Developer, DrillingInfo.com.
Java Persistence API Maciej Adamiak. Agenda -Entity, -Entity Operations, -Query Language.
Rice KRAD Data Layer JPA Design Eric Westfall July 2013.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Training - Day 3 OJB. What is OR Mapping? OR Mapping is the mapping of relational database tables to objects (Java Objects in our case) Many OR Mapping.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Agenda What is Hibernate Spring Integration Questions Overview
Maven for building Java applications By Nalin De Zoysa
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
Entity Beans BMP Celsina Bignoli
Lecture 8 Advanced Topics in Enterprise JavaBeans.
IS-907 Java EE JPA: Simple Object-Relational Mapping.
Container-Managed Persistence (CMP) Entity Beans Lesson 3A / Slide 1 of 42J2EE Server Components Objectives In this lesson, you will learn to: Identify.
Distributed Systems 1 Master of Information System Management Distributed Systems Persistence.
JBoss Seam Presented by Andy Nguyen Truc Pham. What is JBoss Seam? Created by Gavin King Created by Gavin King A lightweight framework for Java EE 5.0.
Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools.
Topic : JPA Kaster Nurmukan. Overview of JPA EntityManager.
EJB 3.0 Persistence Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006.
1 Architectural Overview For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
Topic : Hibernate 3:Advanced ORM Kaster Nurmukan.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
The Java Persistence API ©SoftMoore ConsultingSlide 1.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
v110912Java Persistence: EntityManager2 Overview Earlier versions of EJB Specification defined the persistence layer –javax.ejb.EntityBean Java EE 5 moved.
Java Persistence API (JPA) Relationships. Kinds of relationships UML associations and aggregations (ER non- identifying relationships, shared semantics)
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Introduction – ORM, Helloworld Application
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
Singleton Academy, Pune. Course syllabus Singleton Academy Pune – Course Syllabus1.
14 Copyright © 2004, Oracle. All rights reserved. Achieving State Management in the Business Tier.
CS422 Principles of Database Systems Object-Relational Mapping (ORM) Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Hibernate Java Persistence API. What is Persistence Persistence: The continued or prolonged existence of something. Most Applications Achieve Persistence.
Hibernate Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan Java Annotation Lesson 1.
Object-Relational Mapping. Contents  Default Mapping Rules  Elementary Mapping  Embeddables  Relationship Mapping  Inheritance Mapping.
Don't Know Jack About Object-Relational Mapping?
DEPTT. OF COMP. SC & APPLICATIONS
Chengyu Sun California State University, Los Angeles
ATS Application Programming: Java Programming
Entity Bean Chuyên đề Lập trình Java & J2EE Chương 15
How to connect natively?
Database Applications (15-415) ORM - Part I Lecture 11, February 11, 2018 Mohammad Hammoud.
Objectives In this lesson, you will learn to:
Data Model.
Testing a persistence layer
Knowledge Byte In this section, you will learn about:
Understanding and Designing with EJB
Developing and testing enterprise Java applications
CS4961 Software Design Laboratory Understand Aquila Backend
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Data Persistence Layer Petr Adámek

Content: Persistence I. Introduction to data persistence Where to store data How to work with data (Persistence technologies in Java EE) Architecture of data persistence layer Introduction to ORM What is ORM Basic Principles JPA Introduction Entities in JPA JPA Components Entity Lifecycle Data Persistence

Content: Persistence II JPA Advanced Mapping Querying JPQL Criteria API Alternatives EJB 2.x JDO JDBC Embedded SQL Spring JDBC iBatis Best practices Data Persistence

Introduction into data persistence layer

Where to store data Data can be stored on different places Relational Database (RDBMS) Object Database XML Database DMS (Document Management System) CMS (Content Management System) Post-relational database (Caché) Temporary Hierarchical Spatial Key/Value (No-SQL) Database Another Information System (CRM, ERP, etc.) Data Persistence

Relational Databases The most frequent data storage for enterprise applications Relational data model is simple but very powerful Suitable and sufficient for most of common applications Good theoretical model (Relational Algebra, Relational Calculus) Simplicity => High Performance (eg. due simple optimizations) Proven and well established technology (40 years of development, tools, standards, reliability, high penetration, lots of experts, etc.) Data are separated from application and can be easily shared between different applications Independent on concrete platform or programming language Data Persistence

Persistence Technologies With Relational Model JDBC (low-level API, cumbersome for direct use) Commons DB Utils Spring JDBC iBatis/MyBatis Embedded SQL With Object Model (ORM or other model conversion) Legacy EJB 2.x Hibernate JPA JDO Data Persistence

Architecture of persistence layer Presentation Tier DTO Business Logic Entity Persistence/DAO Tier JDBC / SQL DB Data Persistence

Architecture of persistence layer DAO (Data Access Object) design pattern Separation persistence and data access from business logic Allows to modify persistence implementation or replace persistence technology at all without affecting application logic. DTO (Data Transfer Object) design pattern Business logic API is independent of entities Data model can be changed without affecting Business logic API and/or presentation tier DTO granularity is independent on entity size (single DTO could contain only subset of entity attributes or could agregate information from mulitple entities) Data Persistence

Transaction management Transaction are not controlled on DAO level Why? Single transaction can contain more operations provided by different DAO components Transaction management should be independent on persistent technology Transaction management will be discussed later Lokální/globální transakce, izolovanost transakcí Data Persistence

Introduction to ORM Data Persistence

What is ORM Object-relational mapping (ORM) Technique for automatic conversion between object model and relational data model You work with objects, but they are stored in a traditional relational database. INSERT INTO people (id, name) VALUES (1, "Pepa"); Person p = new Person(1, "Pepa"); em.persist(p); em.getTransaction().commit(); UPDATE people SET name = "Honza" WHERE id = 2; Person p = em.find(Person.class,2); p.setName("Honza"); Data Persistence

Why ORM As we already discussed, the most frequent storage is RDBMS But we usually want to work with Object Model Why Object Model? It is natural for object oriented programming language Working with Object Data Model is straightforward, friendly and easy See example Data Persistence

Why ORM: JDBC example public String getPersonName(long personId) throws SQLException { PreparedStatement st = null; try { st = connection.prepareStatement( "SELECT name FROM people WHERE id = ?"); st.setLong(1, personId); ResultSet rs = st.executeQuery(); if (rs.next()) { String result = rs.getString("name"); assert !rs.next(); return result; } else { return null; } } finally { if (st != null) { st.close(); } Data Persistence

Why ORM: JPA Example public String getPersonName(long personId) { Person p = em.find(Person.class,personId); return p.getName(); } Data Persistence

ORM: What We Get and Lost ORM Benefits Possibility to work with natural object model Portability between different RDBMS with different SQL Dialect Type checking at compile phase No runtime error in SQL statements Simplifies testing Simpler and clearer code More effective development (auto complete, access to JavaDoc, etc.) ORM drawbacks Less control over SQL statements sent to database Less performance in some cases (ORM has some overhead). No access to advantages of relational model and features of RDBMS (e.g. storage procedures) Data Persistence

Basic terms What we should already know JDBC, SQL, Transaction New terms Entity – domain object representing data stored into database (eg. Person, Invoice, Course). DTO (Data Transfer Object) – object for data encapsulation and transfering between components. POJO (Plain Old Java Object) – simple class without any special requirements. It may not implement any special interface, it may not extend any given class, it may not dependent on any other class, package or framework. Data Persistence

Standards and Approaches Entity EJB (EJB 2.1/JSR 153; J2EE 1.4) Application server with EJB container required. Entity is heavyweight component, instances are located in EJB container and accessed through remote calls Problem with latences (reason for introducing DTO and DAO patterns). CMP or BMP JPA is preferred since EJB 3.0 JDO (JDO 3.0/JSR 243) General and universal standard for data persistence in Java. Not limited to RDBMS, arbitrary storage can be used for storing objects JPA (JPA 2.0/JSR 317; Java EE 6) Java EE standard for ORM (inspired with Hibernate) Entity is lightweight POJO which can be freely passed between components, locally or remotely. Data Persistence

EJB 2.x CMP Entity public abstract class PersonBean implements EntityBean { private EntityContext context; public abstract Long getId(); public abstract void setId(Long id); public abstract String getName(); public abstract void setName(String name); public Long ejbCreate (Long id, String name) throws CreateException { setId(id); setName(name); return id; } public void ejbPostCreate (Long id, String name) throws CreateException {} public void setEntityContext(EntityContext ctx) { context = ctx; } public void unsetEntityContext() { context = null; } public void ejbRemove() {} public void ejbLoad() {} public void ejbStore() {} public void ejbPassivate() {} public void ejbActivate() {} public PersonDTO getPersonDTO() { return new PersonDTO(getId(),getName()); Data Persistence

POJO Entity public class Person { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean equals(Object o) { if (this == o) { return true; } if (getId() == null) { return false; } if (o instanceof Person) { return getId().equals(((Person) o).getId()); } else { return false; } public int hashCode() { return id==null?0:id.hashCode(); } Data Persistence

Mapping Definition With annotations Object model definition and its mapping are on the same place. Clear and straightforward Easier development and maintenance With external file (usualy XML) Entities are independant on particular ORM technology Mapping could be changed without modification of code With special JavaDoc comments For Java 1.4 and older (without annotations support) See XDoclet. Data Persistence

Mapping and DB schema definition Generování schématu databáze na základě definice mapování Máme vytvořené entity a definici mapování a chceme si ušetřit práci s vytvářením schématu databáze. Je možné automaticky vytvářet tabulky při prvním spuštění aplikace. Výhodné zejména při vývoji, kdy dochází ke změnám datového modelu. Vhodné, pokud je datový model zcela pod kontrolou naší aplikace. Problém, pokud se mění datový model a již máme v databázi existující data. Generování entit a definice mapování na základě schématu databáze Máme vytvořené schéma databáze a chceme si ušetřit práci s vytvářením entit a definicí mapování (např. vyvíjíme aplikaci pro přístup k již existujícím datům). Obvykle je nutné vygenerované soubory ručně opravit. Data Persistence

Java Persistence API Data Persistence

JPA Introduction Java Persistence API POJO Entities, inspired by ORM tool Hibernate API implemented by various ORM tools from different vendors. Just basic functionality, implementations could provide other features and functions through its proprietary API Versions and specifications JPA 1.0 – part of Java EE 5; created as part of EJB 3.0 (JSR 220), but independent. JPA 2.0 – součást Java EE 6; JSR 317 JPA 2.1 – part of Java EE 7; JSR 338 ORM tools implementing JPA Hibernate, Open JPA TopLink, TopLink Essentials, Eclipse Link Data Persistence

Entity in JPA Entity Represent domain object Simple POJO class Attributes represents domain object properties Attributes accessible with set/get methods Mandatory parameterless constructor It is useful (but not mandatory) to implement Serializable Mapping definition With annotations or xml file Convention-over-configuration principle. Co má vyšší prioritu? Data Persistence

JPA Entity @Entity public class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean equals(Object o) { if (this == o) { return true; } if (getId() == null) { return false; } if (o instanceof Person) { return getId().equals(((Person) o).getId()); } else { return false; } public int hashCode() { return id==null?0:id.hashCode(); } Data Persistence

Example Working With Entity Data Persistence

Configuration Configuration Stored in persistence.xml Contains one or more Persistence Units. Persistence Unit List of classes managed by given Persistence Unit Database connection configuration JNDI name of DataSource JDBC url, name, password Transaction control configuration (RESOURCE_LOCAL or JTA) Table creation strategy Configuration parameter names Vendor specific for JPA 1.0, standardized for JPA 2.0 Parameters could be also set when creating EntityManagerFactory or EntityManager Data Persistence

EntityManagerFactory JPA Architecture Instance of entity A Class of entity A Instance entity B Třída entity B Instance entity C Třída entity C Entity instances management Configuration, mapping definition Transaction PersistenceContext PersistenceUnit 0..1 1 0..n 1 1 1 Configures Manages 1..n 1 EntityManager EntityManagerFactory 0..n 1 Allows manipulation with entities Reads PU Configuration Creates EntityManager Data Persistence

Leaving Persistence Context Entity Lifecycle em.detach(e) em.clear() Serialization and deserialization Persistence Context Expiration (eg. due em.close() operation) Clone or copy Does not exist Entity e = new Entity(); New em.persist(e); Leaving Persistence Context em.remove(e) Detached Managed Deleted em.merge(e) em.persist(e); All changes are automatically stored into DB when current transaction is commited em.refresh(e); Data Persistence

Quiz Entity Lifecycle Data Persistence

Hands-on lab: Databáze kontaktů Data Persistence

Mapping Definition Data Persistence

Supported types Primitive types: byte, int, short, long, boolean, char, float, double Wrapper types: Byte, Integer, Short, Long, Boolean, Character, Float, Double Byte and character array types: byte[], Byte[], char[], Character[] Large numeric types: java.math.BigInteger, java.math.BigDecimal Strings: java.lang.String Java a JDBC temporal types: java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp Enumerated types: Any system or user-defined enumerated type Serializable objects: Any system or user-defined serializable type Any object with appropriate convertor (JPA 2.1) Data Persistence

Field mapping configuration Data Acces Field access Property access Mixed access (@Access, JPA 2.0) @Column @Basic @Lob @Transient or transient Data Persistence

Primary key Supported types ID generation Composite primary key byte, int, short, long, char Byte, Integer, Short, Long, Character, java.lang.String , java.math.BigInteger java.util.Date, java.sql.Date Floating point types supported but strongly not recommended ID generation Sequence, identity, table, Auto Composite primary key Key class required Data Persistence

Field mapping Data Acces Enums Temporal types (Date, Time) Boolean Field access Property access Mixed access (@Access, JPA 2.0) Enums Temporal types (Date, Time) Boolean Data Persistence

Relationships Directionality Cardinality Owning side Uniderctional Bidirectional Cardinality OneToOne OneToMany ManyToOne ManyToMany Owning side @JoinColumn, mappedBy Cascade Operations @OneToMany bez mappedBy Data Persistence

Lazy fetching @Basic, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany Useful, but use with care Just performance hint, not necessary supported Problem with detached entities Not universal solution for dependency problem Data Persistence

Embedded object @Embeddable, @Embedded @AttributeOverrides, @AttributeOverride Multiple occurences of the same embedded class in the entity is not allowed in JPA 1.0 Data Persistence

Element Collections In addition to relationships, also collection of these objects are supported (since JPA 2.0) Simple types Embeddable classes @ElementCollection, @CollectionTable Data Persistence

Collection Types Set, Collection List Map No aditional information required List @OrderBy @OrderColumn (since JPA 2.0) Map In JPA 1.0, Map is allowed only in relationships (Value is entity and key is attribute of given entity) In JPA 2.0, any SimpleType, Collection or Embeddable can be key or value @MapKeyColumn, @MapKeyEnumerated Data Persistence

Inheritance @MappedSuperclass Entity inheritance Single table strategy Multiple table strategy Join table strategy Data Persistence

Querying Data Persistence

Querying JPQL Criteria API Native queries Query language similar to SQL Supports scalar values, tuples, entities or constructed objects Criteria API From JPA 2.0 Allows to build query programatically Native queries Queries in SQL, non portable, not integrated with JPA infrastructure Use only in exceptional cases Data Persistence

Calling JPQL Data Persistence

Example JPQL Criteria API Executing simple query Named queries Returning scalar values Returning tuples Constructing objects Criteria API Simple example Data Persistence

JPQL Examples Data Persistence

Other Technologies Data Persistence

EJB 2.x Incompatible with DAO Design Pattern Actually, DAO Pattern was designed as replacement for EJB 2.x Entities Requires Java EE Application server with EJB Container Entity is heavyweight component, instances are located in EJB Container and accessed remotely Problem with latencies (reason for introducing DAO and DTO design patterns) CMP versus BMP JPA is preferred from EJB 3.0 Data Persistence

EJB 2.x Entity public abstract class PersonBean implements EntityBean { private EntityContext context; public abstract Long getId(); public abstract void setId(Long id); public abstract String getName(); public abstract void setName(String name); public Long ejbCreate (Long id, String name) throws CreateException { setId(id); setName(name); return id; } public void ejbPostCreate (Long id, String name) throws CreateException {} public void setEntityContext(EntityContext ctx) { context = ctx; } public void unsetEntityContext() { context = null; } public void ejbRemove() {} public void ejbLoad() {} public void ejbStore() {} public void ejbPassivate() {} public void ejbActivate() {} public PersonDTO getPersonDTO() { return new PersonDTO(getId(),getName()); Data Persistence

Embedded SQL SQL written directly in the code Code is processed with special preprocessor before compiling Preprocessor process SQL expressions, checks their validity, performs type checking a translates them into expression of used programming language. Preprocessor requires database connection public String getPersonName(long personId) { String name; #sql { SELECT name INTO :name FROM people WHERE id = :personId }; return name; } Data Persistence

Spring JDBC Spring library implementing Template Method design pattern Cleaner code, faster development, easier maintenance Unlike ORM or Embedded SQL does not solve the problem with errors in SQL expressions, that become apparent until runtime JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); public String getPersonName(long personId) { return jdbcTemplate.queryForObject( "SELECT name FROM people WHERE id = ?", String.class,personId); } Data Persistence

Apache Commons DBUtils Another library implementing Template Method design pattern. QueryRunner queryRunner = new QueryRunner(dataSource); ResultSetHandler<String> stringHandler = new ScalarHandler<String>(); public String getPersonName(long personId) { return queryRunner.query( "SELECT name FROM people WHERE id = ?", stringHandler,personId); } Data Persistence

iBatis/MyBatis Originally Apache project, retired, currently developed as MyBatis SQL Queries are separated from code In XML file In annotations (in new versions) More powerful that simple libraries like Spring JDBC, more lightweight than ORM Data Persistence

Transaction Management Data Persistence

Transactions Rollback in JPA and persistence context Transaction scoped Persistence Context Data Persistence

Questions ? Data Persistence