Architecture overview

Similar documents
JPA Entities. Course Multi Tier Business Applications with Java EE. Prof. Dr. Eric Dubuis Berner Fachhochschule Biel. Berner Fachhochschule

Module 8 The Java Persistence API

CO Java EE 6: Develop Database Applications with JPA

object/relational persistence What is persistence? 5

foreword to the first edition preface xxi acknowledgments xxiii about this book xxv about the cover illustration

find() method, 178 forclass() method, 162 getcurrentsession(), 16 getexecutablecriteria() method, 162 get() method, 17, 177 getreference() method, 178

Fast Track to EJB 3.0 and the JPA Using JBoss

Index. setmaxresults() method, 169 sorting, 170 SQL DISTINCT query, 171 uniqueresult() method, 169

Chapter 1 Introducing EJB 1. What is Java EE Introduction to EJB...5 Need of EJB...6 Types of Enterprise Beans...7

EJB 3 Entities. Course Multi Tier Business Applications with Java EE. Prof. Dr. Eric Dubuis Berner Fachhochschule Biel. Berner Fachhochschule

Object Persistence and Object-Relational Mapping. James Brucker

Pro JPA 2. Mastering the Java Persistence API. Apress* Mike Keith and Merrick Schnicariol

Session 13. Reading. A/SettingUpJPA.htm JPA Best Practices

O/R mapping with Hibernate

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Web Application Development Using JEE, Enterprise JavaBeans and JPA

"Charting the Course... Mastering EJB 3.0 Applications. Course Summary

Introduction to JPA. Fabio Falcinelli

indx.qxd 11/3/04 3:34 PM Page 339 Index

Java EE Application Assembly & Deployment Packaging Applications, Java EE modules. Model View Controller (MVC)2 Architecture & Packaging EJB Module

Enterprise JavaBeans, Version 3 (EJB3) Programming

Spring Persistence. with Hibernate PAUL TEPPER FISHER BRIAN D. MURPHY

CO Java EE 7: Back-End Server Application Development

What data persistence means? We manipulate data (represented as object state) that need to be stored

By Philip Japikse MVP, MCSD.NET, MCDBA, CSM, CSP Principal Consultant Pinnacle Solutions Group

Enterprise JavaBeans 3.1

Spring & Hibernate. Knowledge of database. And basic Knowledge of web application development. Module 1: Spring Basics

ADVANCED JAVA TRAINING IN BANGALORE

Java Persistence API. Patrick Linskey EJB Team Lead BEA Systems Oracle

5/2/2017. The entity manager. The entity manager. Entities lifecycle and manipulation

Migrating a Classic Hibernate Application to Use the WebSphere JPA 2.0 Feature Pack

Java Object/Relational Persistence with Hibernate. David Lucek 11 Jan 2005

Lightweight J2EE Framework

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

Java Persistence API (JPA)

The Object-Oriented Paradigm. Employee Application Object. The Reality of DBMS. Employee Database Table. From Database to Application.

Lightweight J2EE Framework

Entity LifeCycle Callback Methods Srikanth Technologies Page : 1

Hibernate Interview Questions

Thu 10/26/2017. Created RESTful Web Service, JavaDB, Java Persistence API, Glassfish server in NetBeans 8

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

Apache OpenJPA 2.1 User's Guide

Table of Contents. I. Pre-Requisites A. Audience B. Pre-Requisites. II. Introduction A. The Problem B. Overview C. History

CMP 436/774. Introduction to Java Enterprise Edition. Java Enterprise Edition

JPA The New Enterprise Persistence Standard

Java EE Architecture, Part Three. Java EE architecture, part three 1(57)

Dali Java Persistence Tools. User Guide Release 2.0 for Eclipse

Java EE Architecture, Part Three. Java EE architecture, part three 1(69)

Dali Java Persistence Tools

Efficient Object-Relational Mapping for JAVA and J2EE Applications or the impact of J2EE on RDB. Marc Stampfli Oracle Software (Switzerland) Ltd.

Apache OpenJPA 2.2 User's Guide

Entities are classes that need to be persisted, usually in a relational database. In this chapter we cover the following topics:

Web Application Development Using Spring, Hibernate and JPA

com Spring + Spring-MVC + Spring-Boot + Design Pattern + XML + JMS Hibernate + Struts + Web Services = 8000/-

EJB 3 Entity Relationships

Spring Professional v5.0 Exam

High-Performance Hibernate VLAD MIHALCEA

Hibernate OGM Architecture

Struts: Struts 1.x. Introduction. Enterprise Application

Java EE 7: Back-End Server Application Development

Setting Schema Name For Native Queries In. Hibernate >>>CLICK HERE<<<

Oracle Exam 1z0-898 Java EE 6 Java Persistence API Developer Certified Expert Exam Version: 8.0 [ Total Questions: 33 ]

Apache OpenJPA User's Guide

Java Persistence Query Language (JPQL)

Middleware for Heterogeneous and Distributed Information Systems Sample Solution Exercise Sheet 5

Building Java Persistence API Applications with Dali 1.0 Shaun Smith

EJB 3 Entity Relationships

Generating A Hibernate Mapping File And Java Classes From The Sql Schema

Dynamic Datasource Routing

Hibernate Overview. By Khader Shaik

JVA-163. Enterprise JavaBeans

International Journal of Advance Research in Engineering, Science & Technology HIBERNATE FRAMEWORK FOR ENTERPRISE APPLICATION

Java- EE Web Application Development with Enterprise JavaBeans and Web Services

Java Persistence API (JPA) Entities

HIBERNATE MOCK TEST HIBERNATE MOCK TEST IV

Introduction to Java Enterprise Edition For Database Application Developer

TopLink Grid: Scaling JPA applications with Coherence

Page 1

Apache OpenJPA User's Guide

The Good, the Bad and the Ugly

Dali Java Persistence Tools. User Guide Release for Eclipse

Complete Java Contents

Web Application Development Using Spring, Hibernate and JPA

Web Application Development Using Spring, Hibernate and JPA

JPA and CDI JPA and EJB

Extracts from Intro to Db - Jdbc - JPA SpringData

Get Back in Control of your SQL

Refactoring to Seam. NetBeans. Brian Leonard Sun Microsystems, Inc. 14o

Object-Relational Mapping

JPA. Java persistence API

1 Copyright 2011, Oracle and/or its affiliates. All rights reserved.

Java Persistence Advanced Concepts

CHAPTER 3 ENTITY MANAGERS

How to Develop a Simple Crud Application Using Ejb3 and Web Dynpro

Injection Of Entitymanager

Understanding Impact of J2EE Applications On Relational Databases. Dennis Leung, VP Development Oracle9iAS TopLink Oracle Corporation

Holon Platform JPA Datastore Module - Reference manual. Version 5.2.1

Project # 1: Database Programming


JAVA SYLLABUS FOR 6 MONTHS

Transcription:

JPA MARTIN MUDRA

Architecture overview API API API API Business logic Business logic Business logic Business logic Data layer Data layer Data layer Data layer Database

JPA Java Persistence API Application - OOP Database Tables and relations Invoice Invoice Line

Why do we need ORM OOP works with classes and their instances There are object databases that work with OOP natively Performance problems Standartization problems Problematic scalability Commercial solutions are usually based on RDB

Native code JDBC ADO.NET Different connectivity handling JDBC - Example try (Statement stmt = conn.createstatement(); ResultSet rs = stmt.executequery("select * FROM MyTable")) while ( rs.next() ) { int numcolumns = rs.getmetadata().getcolumncount(); myclass result = new myclass(); result.setpropertya((int) rs.getobject(1)); result.setpropertyb((string) rs.getobject(2)); result.setpropertyc((int) rs.getobject(3)); result.setpropertyd((int) rs.getobject(4)); result.setpropertye((int) rs.getobject(5));... results.add(result); }

Entity Manager Takes care of work with entities, connections Resource Injection @PersistenceContext (unitname="school_persistence_unit") private EntityManager em; // follows work with em Instance resolving Factory method javax.persistence.entitymanager em; em = javax.persistence.persistence.createentitymanagerfactory("school_persistence_unit").createentitymanager(); // follows work with em

Entity Manager - Configuration Configuration is stored in file persistence.xml <?xml version="1.0" encoding="utf-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="school_persistence_unit" transaction-type="jta > <provider>oracle.toplink.essentials.persistenceprovider</provider> <jta-data-source>jdbc/school</jta-data-source> <properties> <property name="toplink.ddl-generation" value="drop-and-create-tables"/> </properties> </persistence-unit> </persistence> Name of PU userd for DI JPA provider - imlementation JNDI Java Naming and Directory Interface Stragy for table generation More PU can be defined in one file Provider - implementation Hibernate, Eclipselink (Oracle TopLink), OpenJPA, DataNucleus

Entity manager - methods persist(object entity) saves entity into DB refresh(object entity) reloads the entity from DB flush() makes sure to write to DB create*query(string sql, ) query to DB merge(t entity) merges / connects an object to a persistent context remove(object entity) deletes from DB find(classentityclass, Object primarykey) finds an entity of T type using primary key

Code example - entity @Entity public class TeacherEntity implements Serializable { private static final long serialversionuid = 1L; POJO entity Primary key } @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String firstname; private String lastname; @OneToMany(mappedBy = "supervisor") private Collection students; getters and setters Collection of related entities Properties getters and setters

Code example EJB @Stateless public class SchoolSessionBean implements SchoolSessionBean { @PersistenceContext(unitName = "school_persistence_unit") private EntityManager em; } public StudentEntity addstudent(final String firstname, final String lastname) { StudentEntity newstudent = new StudentEntity(); newstudent.setfirstname(firstname); newstudent.setlastname(lastname); } em.persist(newstudent); return newstudent; Session EJB DI Entity Manager Create new instance Persist to DB

Hibernate / NHibernate Implementation of JPA Mature ORM Frameworks Hibernate since 2001 NHibernate 2005 (version 1.0 mirrored functionality of hibernate 2.1) Schema generation DDL Entity manager internally using Hibernate sessions

Mapping Entity public / protected constructor (empty parameters) Non final / virtual methods (all) Public / protected getters and setters for fields (Properties) Attributes / Annotations @LOB BLOB type @Transient not persisted Fluent Refactorable POJO / POCO to RDB XML *.hbm.xml Property / fields

XML Mapping example <?xml version="1.0" encoding="utf-8"?> <hibernate-mapping assembly="itjakub.dataentities" namespace="itjakub.dataentities.database.entities" xmlns="urn:nhibernate-mapping-2.2"> <class name="author" table="[author]"> <id name="id" column="id"> <generator class="identity" /> </id> <property name="name"/> Namespace / package Class Id generation strategy M ku N relationship <bag name="bookversions" table="bookversion_author" inverse="true" lazy="true"> <key column="author" /> <many-to-many class="bookversion" column="bookversion" /> </bag> </class> </hibernate-mapping>

Inheritance IS-a relations Concrete table per class Select all for base class multiple queries Subclass (Table per hierarchy) Discriminator string, number Discriminator-value attribute Joined-subclass (Table per type) Left outer joins Union-subclass Impossible to use identity (Hi-Lo etc.) Parties +Id Companies +CompanyName People +FirstName +LastName

Primary key generation Static generation Identity Auto Table HI-LO Hi synchronized generation Lo - seed Hi-lo algorithm Identifiers given: hi 1 incrementsize + 1, hi IncrementSize + 1ሻ ۦ Lo ሻ, 0 ۦ increment Size Statistically unique UUID / GUID

Query language (H + NH) Java Persistence query language based on HQL Dialect independent Simmilar to SQL but on objects Supports Selectors, updates, deletes Parameters Joins Aggregation function AVG, MAX etc. Custom extensions for pagination Take Skip Query query = session.createquery("from Cat c where c = :id "); query.setparameter("id", "7277"); List list = query.list(); DELETE Cat c WHERE c.id = 151 FROM Cat c INNER JOIN c.mate m LEFT OUTER JOIN c.kittens k ORDER BY k.name

Criteria (H + NH) Elimination of magic strings Properties? Selectors only Parameters Subqueries, ordering. Usefull tools Restrictions Transformers (N)HibernateUtil IList<Cat> cats = sess.createcriteria<cat>().add( Expression.Like("Name", "Fritz%") ).Add( Expression.Or( Expression.Eq( "Age", 0 ), Expression.IsNull("Age") ) ).List<Cat>(); List cats = sess.createcriteria(cat.class).add( Restrictions.in( "name", new String[] { "Fritz", "Izi", "Pk" } ) ).add( Restrictions.disjunction().add( Restrictions.isNull("age") ).add( Restrictions.eq("age", new Integer(0) ) ).add( Restrictions.eq("age", new Integer(1) ) ).add( Restrictions.eq("age", new Integer(2) ) ) ) ).list();

QueryOver (NH) Selectors only Elimination of magic strings Refactorable Supports Joins Detached queryover subqueries Ordering Pagination etc. session.queryover<book>().select(book => book.id).list<long>(); session.queryover<author>().where(author => author.name == name).singleordefault<author>();

Other query methods Native SQL Stored methods Named queries SQL Mappings / annotations / config files Parameters naming problems LINQ to SQL @NamedQueries( { @NamedQuery(name = "Teacherentity.findAll", query = "SELECT t FROM Teacherentity t"), @NamedQuery(name = "Teacherentity.findById", query = "SELECT t FROM Teacherentity t WHERE t.id = :id"), @NamedQuery(name = "Teacherentity.findByFirstname", query = "SELECT t FROM Teacherentity t WHERE t.firstname = = :firstname") })

Cascade None Do not cascade Save-update Check associations and save/update any object that require it even Many-to-many Delete Delete all the objects in associations Delete-orphan Delete and check other objects that reference me and delete orphans JPA orphanremoval All Save/update/delete All-delete-orphan House Rooms Save/update/delete-orphan 0..n 1

Lazy Fetch True/ false/ proxy Default - true Fetch-mode Select Join limited to 1 relationship

N+1 Problem Select / delete / update Cascade problems Lazy initialization problems Batch-size Futures Load all at one roundtrip Transaction Concrete house Living room Amelie Leif Caterina Neema Taj Mina Kapono House Room Person Bathroom Hall Cruz Gabi

Isolations Config settings default value Per-transaction settings Deadlocks Serializable RangeLock multiversion lock Usually loss of concurrency Repeatable reads Phantom reads Two same queries in one transaction returns different result Keep read/write lock to selected data Read committed Keeps write-locks until transaction end Relase read-lock immidately after SELECT Read uncommitted Dirty reads

Phantom reads Time Transaction 1 Transaction2 1 2 3 Select range SELECT * FROM users WHERE age BETWEEN 10 AND 30 Select range & Commit SELECT * FROM users WHERE age BETWEEN 10 AND 30; COMMIT; --Get different values than prev read Insert & commit INSERT INTO users( ) VALUES (, John, 27); COMMIT;

Dirty reads Time Transaction 1 Transaction 2 1 Select Select * from users where id= 1; --NAME = JACK 2 3 4 Select Select * from users where id= 1;--name = JOHN Update Update users set name= John where id = 1 Rollback Rollback;