Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February

Slides:



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

Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
WaveMaker Visual AJAX Studio 4.0 Training
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Management Information Systems, Sixth Edition
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and.
Modelling classes Drawing a Class Diagram. Class diagram First pick the classes –Choose relevant nouns, which have attributes and operations. Find the.
Session-02.
An Introduction to Hibernate Matt Secoske
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Object Persistence and Object-Relational Mapping James Brucker.
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. –
Programming Languages and Paradigms Object-Oriented Programming.
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.
Data Access Patterns Some of the problems with data access from OO programs: 1.Data source and OO program use different data modelling concepts 2.Decoupling.
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
IS-907 Java EE JPA: Simple Object-Relational Mapping.
Database Technical Session By: Prof. Adarsh Patel.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS 157B: Database Management Systems II February 6 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS 157B: Database Management Systems II February 4 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Object Oriented Analysis and Design 1 Chapter 7 Database Design  UML Specification for Data Modeling  The Relational Data Model and Object Model  Persistence.
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.
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.
Topic : Hibernate 2: Object Persistence and ORM Kaster Nurmukan.
Hibernate Introduction - 1 Present by Eric Yu. BeanSoft | 2 Content Understanding object/relational persistence Introduction Architecture The core interfaces.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
Java Data Persistence Using Hibernate Jack Gardner October 2004.
Object-Relational Mapping with Hibernate Brian Sam-Bodden Principal Partner Integrallis Software, LLC. August 1 - 5, 2005.
IST 220 Introduction to Databases Course Wrap-up.
A U.S. Department of Energy Office of Science Laboratory Operated by The University of Chicago Argonne National Laboratory Office of Science U.S. Department.
JPA / HIBERNATE CSCI 6370 Nilayan Bhattacharya Sanket Sable.
Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
BSA206 Database Management Systems Lecture 2: Introduction to Oracle / Overview of Database Concepts.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
The Java Persistence API ©SoftMoore ConsultingSlide 1.
Java Persistence API part 1 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Java Persistence API (JPA) Relationships. Kinds of relationships UML associations and aggregations (ER non- identifying relationships, shared semantics)
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,
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
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.
Don't Know Jack About Object-Relational Mapping?
Chengyu Sun California State University, Los Angeles
Advanced Java Programming
Hibernate (JPA) Code First Entity Relations
Java Data Persistence Using Hibernate
Database Applications (15-415) ORM - Part I Lecture 11, February 11, 2018 Mohammad Hammoud.
CS4961 Software Design Laboratory Understand Aquila Backend
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Composants & Services Logiciels Software Components & Services Hibernate These slides and case studies can be downloaded from:

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Hibernate Hibernate is the most popular persistence framework in the Java world (NHibernate in the.NET world) It is an open source product which can be downloaded from: Hibernate Core is composed of the key libraries (.jar files) Hibernate EntityManager is the support for Java Persistence API (JPA) Hibernate is the default support for JPA in JBoss while TopLink is the default in GlassFish Hibernate eases the development of database-oriented applications (compared to JDBC for example) Hibernate promotes object-orientation (polymorphism, OO requests through HQL…) and removes the direct non-evolutive codification of SQL inside the Java code (compared to JDBC)

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Case study (UML model)

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Case study (database schema)

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Case study (persistent object dependencies)

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Hibernate configuration The hibernate.cfg.xml file defines the main properties of the application. It is usually installed in the Java default package This file is loaded and interpreted through an instance of the org.hibernate.cfg.Configuration class The key notion behind Hibernate is the notions of session factory and session: private org.hibernate.SessionFactory _session_factory = new org.hibernate.cfg.Configuration().configure().buildSessionFa ctory(); private org.hibernate.Session getSession() { return _session_factory.getCurrentSession(); }

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Example of a hibernate.cfg.xml file <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" " org.apache.derby.jdbc.ClientDriver org.apache.derby.jdbc.ClientDriver jdbc:derby://localhost:1527/Prison_de_Nantes jdbc:derby://localhost:1527/Prison_de_Nantes barbier barbier lena lena org.hibernate.dialect.DerbyDialect org.hibernate.dialect.DerbyDialect true true </session-factory></hibernate-configuration>

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année The notion of session Sessions are spaces in which objects get Hibernate- oriented states An object is included in a session as follows: getSession().persist(_detenu); When attached (Persistent state), an object is synchronized with the database. All modifications on this object will lead to appropriate modifications in the database When flushed (or closed), all objects in a session lose their Persistent state to recover a Transient state. Sessions are manually or automatically associated with database transactions

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Session-oriented actions find (or get, or load) -> it leads to the Persistent state and to a SELECT statement, for example: return (Detenu) session.load(Detenu.class, primary_key); persist (or save) -> it leads to an INSERT statement at flush/commit time merge (or update) -> it leads to an UPDATE statement evict -> it leads to the Detached state delete -> it leads to a DELETE statement refresh -> it leads to a SELECT statement

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Persistent objects A Hibernate persistent object (a.k.a. Plain Old Java Object or POJO) is a Java class whose instances are supposed to persist in a database A POJO is ruled by constraints: constructor without arguments, normalized getter and setter methods for persistent attributes, no use of the final Java modifier for classes, methods and attributes… A POJO implements the java.io.Serializable Java interface A class playing the role of primary composite key must also be subject to the above constraints

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Persistent object lifecycle Note: the Transient state is realized through the value of the unsaved-value attribute

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Configuring persistent objects The mapping between database elements (tables) and persistent objects are described in XML files, for instance: Detenu.hbm.xml Persistent fields are associated with table columns, for instance: … …

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Mapping several tables in a persistent object Example: incorporating the DETENU and INCARCERATION tables into the Detenu POJO: The mark optional="true" is based on the fact that one may have "missing" rows in the INCARCERATION table

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Setting up business identifiers Hibernate does not support so well business primary keys. However, one may have: …

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Overriding equals and hashCode public class Detenu implements java.io.Serializable { private String _n_ecrou; public String get_n_ecrou() { return _n_ecrou; } public void set_n_ecrou(String n_ecrou) { _n_ecrou = n_ecrou; } … // other persistent fields public boolean equals(Object object) { if (!(object instanceof Detenu)) { return false; } Detenu other = (Detenu) object; if (_n_ecrou != other._n_ecrou && (_n_ecrou == null || !_n_ecrou.equals(other._n_ecrou))) { return false; } return true; public int hashCode() { return _n_ecrou != null ? _n_ecrou.hashCode() : 0; } … // associations here

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Setting up composite identifiers Hibernate does not also support so well composite (business) primary keys, for instance: … As shown here, composite primary keys require dedicated classes, e.g., AffairePK

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Setting up technical (surrogate) identifiers Example: …

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année DBMS-oriented identifier generation Example: my_generator … Creating sequences in Oracle: CREATE SEQUENCE my_generator;

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Other modes for managing identifiers Example: Note: the unsaved-value attribute is set to undefined when identifiers are assigned by users (business primary keys) Other modes: uuid.hex or uuid.string (uses IP addresses to compute identifiers), foreign…

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année One-to-one associations An incarceration is concerned with one and only one prisoner (and vice-versa): constrained="true" is used when there is a foreign key between the two tables The foreign key of the Incarceration persistent object may be constrained by that of the Detenu persistent object: _detenu

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Many-to-many associations Example (Detenu.hbm.xml file): An extract from the Detenu class is as follows: private java.util.Collection _toutes; public java.util.Collection get_toutes() { return _toutes; } public void set_toutes(java.util.Collection toutes) { _toutes = toutes; }

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Bidirectionality Example (Affaire.hbm.xml file): An extract from the Affaire class is as follows: private java.util.Collection _participants; public java.util.Collection get_participants() { return _participants; } public void set_participants(java.util.Collection participants) { _participants = participants; }

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Bidirectionality consistency The inverse="true" mark in Affaire.hbm.xml forces Hibernate to maintain the DETENU_AFFAIRE table from the Detenu persistent object As a result, the following code is required (Affaire class): public void addTo_participants(Detenu detenu) { get_participants().add(detenu); detenu.get_toutes().add(this); } public void removeFrom_participants(Detenu detenu) { get_participants().remove(detenu); detenu.get_toutes().remove(this); }

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année One-to-many associations Example (Detenu.hbm.xml file): An extract from the Detenu class is as follows: private java.util.Collection _condamnation; public java.util.Collection get_condamnation() { return _condamnation; } public void set_condamnation(java.util.Collection condamnation) { _condamnation = condamnation; }

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Many-to-one associations Example (Incarceration.hbm.xml file): The not-null="true" mark forces Hibernate to respect the 1..* multiplicity compared to the 0..* multiplicity

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Lazy loading (1/2) Given a persistent object, a question is about the access to its linked (also persistent) objects. For instance, given d an instance of Detenu, one may want to access to its criminal cases: java.util.Collection all = d.get_toutes(); An extract of the mapping file is as follows:

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Lazy loading (2/2) By default, dependent objects are not loaded (when an object is itself loaded, d below) and thus becomes persistent. As a result, Hibernate executes a deferred SELECT request when one accesses the criminal cases of a prisoner: java.util.Collection all = d.get_toutes(); The way by which lazy loading may be inhibited, relies on the lazy and/or fetch parameter as follows: <set name="_toutes" table="DETENU_AFFAIRE" lazy="false" Note: fetch="join" (left outer join) prevails on lazy

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Cascading actions Insertions, deletions… may be cascaded from a persistent object to its dependent objects: Values save-update, persist, merge, delete, lock, evict and replicate are for one-to-one, many-to-one tags (outside collections) Values for collections are: save-update, persist, merge, delete, delete-orphan, lock, evict, replicate and all-delete-orphan

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Inheritance Hibernate supports three main modes for managing inheritance relationships (and thus polymorphism): 1.A column in the table corresponding to the root class plays the role of discriminator. There are no tables for the subclasses 2.Subclasses have corresponding tables and a foreign key on their ancestor table 3.No need of a table exists for the root class

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Inheritance, no tables for subclasses Example (Decision.hbm.xml): Disadvantages: some data in the DECISION table are with null values for specialized attributes, e.g., _duree

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Inheritance, tables for all classes

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année No table need for the root class </union-subclass> </union-subclass> </union-subclass>

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Sessions and transactions Hibernate supports a home-made transaction management mode: org.hibernate.Session session = _session_factory.openSession(); org.hibernate.Transaction utx = session.beginTransaction(); … utx.commit(); // or utx.rollback(); JTA-oriented transactions: private javax.transaction.UserTransaction _utx; … org.hibernate.Session session = session_factory.getCurrentSession(); _utx.begin(); … _utx.commit(); // or _utx.rollback();

Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February Master « Technologies de l’Internet » - UPPA 1ère année Working with JTA org.hibernate.transaction.SunONETransa ctionManagerLookup org.hibernate.transaction.SunONETransa ctionManagerLookup o rg.hibernate.transaction.JTATransactionFacto ry o rg.hibernate.transaction.JTATransactionFacto ry jta jta