V 6, 2006-09-28 Mats Strandberg ORM With Hibernate.

Slides:



Advertisements
Similar presentations
Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
Advertisements

An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain.
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
NHibernate Object/Relational Persistence for.NET.
Spring, Hibernate and Web Services 13 th September 2014.
.NET Database Technologies: Open-Source Frameworks.
Object Relational Mapping. What does ORM do? Maps Object Model to Relational Model. Resolve impedance mismatch. Resolve mapping of scalar and non-scalar.
ORM Object-Relational Mapping 1. Object Persistence Persistence is the ability of an object to survive the lifecycle of the process, in which it resides.
Session-02.
Session-01. Hibernate Framework ? Why we use Hibernate ?
Emmanuel Cecchet et al.  Performance Scalability of J2EE application servers.  Test effect of: ◦ Application Implementation Methods ◦ Container Design.
Using Third-Party Frameworks in Building Blocks™ David Ashman Principal Architect, Product Development.
EER vs. UML Terminology EER Diagram Entity Type Entity Attribute
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.
Hibernatification! Roadmap for Migrating from Plain Old SQL on JDBC to JPA on Hibernate Duke Banerjee Senior Developer, DrillingInfo.com.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Object and object-relational databases 1. Object databases vs. Object-relational databases Object databases Stores complex objects – Data + functions.
XML, distributed databases, and OLAP/warehousing The semantic web and a lot more.
Entity Framework Code First End to End
LINQ Boot Camp ADO.Net Entity Framework Presenter : Date : Mahesh Moily Nov 26, 2009.
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
Entity Beans BMP Celsina Bignoli
Object persistence with Hibernate in Decision Deck 1.1 Gilles Dodinet 2 nd Decision Deck Workshop 2008, February.
JBoss Cache. Cache A place to temporarily store data that is expensive or difficult to compute or retrieve. Caches should be fast to access. May or may.
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
Enterprise JavaBeans. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports application development.
Object-Oriented Database Management Systems (ODBMS)
Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools.
Object/Relational Mapping with Hibernate Practical ORM.
Object Oriented Analysis and Design 1 Chapter 7 Database Design  UML Specification for Data Modeling  The Relational Data Model and Object Model  Persistence.
© 2011 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 14 Using Relational Databases to Provide Object Persistence (Overview) Modern Database.
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.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
An Introduction to Object/Relational Persistence and Hibernate Yi Li
Entity Framework Code First – Beyond the Basics Sergey Barskiy, Magenic Microsoft MVP – Data Platform Principal Consultant.
Oct * Brad Tutterow. VS 2008.NET 3.5LINQ Entity Framework  The ADO.NET Entity Framework is part of Microsoft’s next generation of.NET technologies.
Ganymede Simultaneous Release | © 2008 by Springsite B.V., The Netherlands made available under the EPL v1.0 Teneo Ganymede Simultaneous Release.
1 CS457 Object-Oriented Databases Chapters as reference.
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.
Intoduction to NHibernate. Agenda Overview of NHibernate Models and Mappings Configuration Sessions and Transactions Queries.
Fall CIS 764 Database Systems Engineering L18.2 : Object Relational Mapping … ….Object persistence.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
By Srinivas Mahakud Java Persistence API JPA. Review Topics: Introduction what is the JPA? What is the JPA contains of? Why developers should Create the.
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Performance, Profiling, & Optimization Tools for Enterprise Java Applications S. Ray Holder Michael J. Donahoo.
Sean Chambers.  ORM stands for Object Relational Mapper  Maps your POCO (plain old clr objects) to your relational model using XML config  Relieves.
 Who I am  What is NHibernate? ◦ Object Relational Mapper ◦ A framework for building advanced frameworks ◦ Ported from Java ◦ A wide NHibernate ecosystem.
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.
Hibernate Java Persistence API. What is Persistence Persistence: The continued or prolonged existence of something. Most Applications Achieve Persistence.
Fall CIS 764 Database Systems Engineering L11: Object Relational Mapping … (a) ORM, Object persistence (b) Pets sequence.
New Technology: Why, What ,How
Don't Know Jack About Object-Relational Mapping?
Chengyu Sun California State University, Los Angeles
Entity Framework By: Casey Griffin.
ADO.NET Entity Framework
Database Applications (15-415) ORM - Part I Lecture 11, February 11, 2018 Mohammad Hammoud.
.NET vs. J2EE Architecture
Developing and testing enterprise Java applications
Teneo Ganymede Mini Deck
Object Relational Mapping Tools
Chengyu Sun California State University, Los Angeles
Presentation transcript:

v 6, Mats Strandberg ORM With Hibernate

v 6, Mats Strandberg Presentation Impedance mismatch What is ORM When/why use ORM Understand Hibernate basics See Java using Hibernate API See SQL Understand Hibernate features Ask questions

v 6, Mats Strandberg Crisp Utbildningsdag (Crisp RD) Crisp RD Every second Friday, usually by employee Some presentations at KTH Mats Strandberg Worked with several OODBs since 1990 Used RDBs in several OO projects Used Hibernate in one commercial project Worked with Java since 1996

v 6, Mats Strandberg Simple Domain Model Event Participant Venue Address 0..n 1 1

v 6, Mats Strandberg Simple Domain Model Event Participant Venue Address 0..n 1 1 name street city name 0..n date

v 6, Mats Strandberg Object Diagram hibernateRd: Event mats : Participant theVenue : Venue theAddress : Address name=rum 1537 street=Osquars Backe 2 city=Stockholm name=Mats Strandberg name=Hibernate RD date=

v 6, Mats Strandberg Example Code and Mapping File

v 6, Mats Strandberg Relational Schema Events Participants Venues Addresses * VenueId * AddressId street * ParticipantId * EventId name EventParticipations * ParticipantId * EventIdname date EventVenue name VenueAddress city

v 6, Mats Strandberg Example execution

v 6, Mats Strandberg On Root Objects In an object graph usually theres roots, where navigation starts. Root

v 6, Mats Strandberg Navigation event.getVenue().getAddress().getStreet(); Event Participant Venue Address name street city name date SELECT street FROM Addresses WHERE AddressId= (SELECT VenueAddress FROM Venues WHERE VenueId= (SELECT EventVenue FROM Events WHERE EventId=1));

v 6, Mats Strandberg Query List list = // get events for (Iterator iter = list.iterator(); iter.hasNext(); ) { Event event = (Event) iter.next(); Address address = event.getVenue().getAddress(); if ("Stockholm".equals(address.getCity())) { System.out.println("'" + address.getStreet() + "' is in Stockholm"); } Event Participant Venue Address name street city name date Get streets in Stockholm SELECT street FROM Addresses WHERE city="Stockholm";

v 6, Mats Strandberg Object Queries Roots may not be enough for search We need at least class extension or OQL

v 6, Mats Strandberg HQL List list = session.createQuery( "select street from Address where city='Stockholm'").list(); for (Iterator iter = list.iterator(); iter.hasNext(); ) { System.out.println("'" + iter.next() + "' is in Stockholm"); } Event Participant Venue Address name street city name date Get street in Stockholm SELECT street FROM Addresses WHERE city="Stockholm";

v 6, Mats Strandberg Example: Hibernate + Domain model Take a look at: Java SQL

v 6, Mats Strandberg Why a Database? Need for persitent state Support for transactions Large data sets Multiple concurrent applications share data Data distribution Usually disk based (persistence with single node) Only add complexity to solve a real problem

v 6, Mats Strandberg Reasons for Object Persistence vs RDB Reasons to use OODB or ORM: Use of OO in design and programming (avoid impedance mismatch) Domain Model Intense solution Hierarchic data Navigational access

v 6, Mats Strandberg Reasons For ORM vs OODB Legacy RDB (Relational Database) (RDB) vendor independence Vendor stability(?) Schema migration Tools

v 6, Mats Strandberg Alternatives to ORM Hand coded persistance layer Serialization EJB/CMP OODB

v 6, Mats Strandberg Hibernate Open Source LGPL Licence

v 6, Mats Strandberg Hibernate is non-intrusive This means: Persistence is orthogonal to class Persisting a instance is a run-time decision

v 6, Mats Strandberg Requirements for a Persistent Class Hibernate is said to non-intrusive, however: Classes must have a no-arg constructor Classes should have a private Long id; Classes may have private database attributes

v 6, Mats Strandberg Understand ORM to use it The effective use of ORM technology in all but the simplest of enterprise environments requires understanding and configuring how the mediation between relational data and objects is performed Linda DeMichiel, Lead Architect EJB, Sun

v 6, Mats Strandberg Impedance Mismatch Identity Granularity Object navigation Subtypes Polymorphic associations

v 6, Mats Strandberg Inheritance Table per concrete class A a1 B b1 C c1 B a1 C * id a1 * id A a1 b1 c1 A * id B C a1 b1c1 Table per class hierarchy Table per class

v 6, Mats Strandberg Application Transactions A.k.a. long running transactions An object graph can be detached Updates can be done while detached The object graph can later be attached to a session session.close() detaches the objects session.update(object) attaches the object NOTE: Other updates may be clobbered!

v 6, Mats Strandberg Detached objects + automatic versioning Handle concurrent updates by versioning A version attribute must be added to classes involved Detach object by session.close() Updates can be done while detached Attach with session.update(object) Exception thrown by Hibernate at flush (commit) if version mismatch

v 6, Mats Strandberg Lazy vs Eager fetch EventVenueAddress namestreet city name date Venue$ name isA

v 6, Mats Strandberg Consider Performance Iterating over a Class (Event) that has a n to m association List list = session.createQuery("from Event").list(); for (Iterator i = list.iterator(); i.hasNext(); ) { Event event = (Event) i.next(); out.println("EVENT name:" + event.getName()); }

v 6, Mats Strandberg Lazy initialization A Proxy is used Getters are overridden, e.g. Event.getName(); The Proxy is a subclass of your persistent class, e.g extends Event Requires build-time bytecode instrumentation Beware of explicit comparison of runtime class, passing of class objects etc.

v 6, Mats Strandberg Caching First-level Cache Session Second-level Cache Query CacheCache Concurrency Strategy Cache Provider

v 6, Mats Strandberg Testing without database? Testing business logic: Hibernate is non-intrusive -> Use POJOs in a transient way Think of transaction demarcation Beware of embedding HQL as it requires a DB.

v 6, Mats Strandberg Hibernate Product Suite Hibernate Core Hibernate Annotations Hibernate EntityManager Hibernate Tools NHibernate JBoss Seam

v 6, Mats Strandberg Roadmap Production: Hibernate 3.1 NHibernate 1.0 for :NET (Hibernate 2.1) Development: Hibernate 3.2 (EJB 3.0) NHibernate 1.2 (.NET Framework 2.0) NHibernate 3.x? (Hibernate 3)

v 6, Mats Strandberg Entity Manager Entity Manager implements a complete EJB3 persistence provider (together with Hibernate Annotations) EJB-QL based on HQL Automatic Versioning Detached Entities Non-managed set-up is rather different EJB3 has a large number of persistence contexts

v 6, Mats Strandberg Features Two Level Cache Locking strategies (e.g. Optimistic Locking) Application Transactions Composition Inheritance Polymorphism Persistence by reachability Fetching strategies: Lazy vs Eager Lazy initialization

v 6, Mats Strandberg Books on Hibernate Etc.

v 6, Mats Strandberg Things to Mention Locking JPA – Java Persistence API Index Hibernate vs Manual ORM: When using Hibernate there is a standard for how the mapping has been done. This is good for maintenance Bidirectional relations are handled at code level