Hibernate Basics 廖峻鋒 Sep 14,2004 NTU Dept. of CSIE.

Slides:



Advertisements
Similar presentations
V 6, Mats Strandberg ORM With Hibernate.
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.
NHibernate Object/Relational Persistence for.NET.
Spring, Hibernate and Web Services 13 th September 2014.
.NET Database Technologies: Open-Source Frameworks.
Hibernate 1. Introduction ORM goal: Take advantage of the things SQL databases do well, without leaving the Java language of objects and classes. ORM.
Object Relational Mapping. What does ORM do? Maps Object Model to Relational Model. Resolve impedance mismatch. Resolve mapping of scalar and non-scalar.
國立清華大學資訊工程學系 資訊工程系 2009/11/03P-1 Quiz & Solution 09810CS_ Computer Systems & Application Fall.
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.
Object-Oriented Methods: Database Technology An introduction.
Session-02.
Session-01. Hibernate Framework ? Why we use Hibernate ?
An Introduction to Hibernate Matt Secoske
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.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Rapid Persistence Layer Development with Hibernate Rapid Persistence Layer Development with Hibernate Tyler Mendenhall E-gineering,
UPortal Developers MIT August 2004 Persistence Strategy for uPortal 3 Mike DeSimone the r-smart group
Agenda What is Hibernate Spring Integration Questions Overview
Maven for building Java applications By Nalin De Zoysa
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
Object persistence with Hibernate in Decision Deck 1.1 Gilles Dodinet 2 nd Decision Deck Workshop 2008, February.
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.
Hibernate Query Language Make SQL be object oriented – Classes and properties instead of tables and columns – Polymorphism – Associations – Much less verbose.
.NET Database Technologies: Data Models and Patterns.
Object/Relational Mapping with Hibernate Practical ORM.
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
Object Oriented Analysis and Design 1 Chapter 7 Database Design  UML Specification for Data Modeling  The Relational Data Model and Object Model  Persistence.
Object Persistence Design Chapter 13. Key Definitions Object persistence involves the selection of a storage format and optimization for performance.
© 2011 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 14 Using Relational Databases to Provide Object Persistence (Overview) Modern Database.
Spring Database Access Ojitha Kumanayaka Copyright © Virtusa, 2007.
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.
Hibernate Introduction - 1 Present by Eric Yu. BeanSoft | 2 Content Understanding object/relational persistence Introduction Architecture The core interfaces.
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.
An Introduction to Object/Relational Persistence and Hibernate Yi Li
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.
February,2006 Advanced DB System Object Relational Model Prof: Dr Rahgozar Euhanna Ghadimi, Mostafa Keikha Discussion, Implementation.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Persistent State Service 1  Concept  Persistence is the ability of an object state to survive termination of the process in which the object executes.
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.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 5th Edition Copyright © 2015 John Wiley & Sons, Inc. All rights.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Introduction – ORM, Helloworld Application
Spring JDBC Dima Ionut Daniel. Contents What is Spring JDBC? Overview Spring JDBC Core SQL Exceptions Database Connection Batch Operations Handling BLOB/CLOB.
Sean Chambers.  ORM stands for Object Relational Mapper  Maps your POCO (plain old clr objects) to your relational model using XML config  Relieves.
Introduction to ORM Hibernate Hibernate vs JDBC. May 12, 2011 INTRODUCTION TO ORM ORM is a programming technique for converting data between relational.
 Who I am  What is NHibernate? ◦ Object Relational Mapper ◦ A framework for building advanced frameworks ◦ Ported from Java ◦ A wide NHibernate ecosystem.
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 Online Training. Introduction to Hibernate Hibernate is a high-performance Object-Relational persistence and query service which takes care.
Hibernate Java Persistence API. What is Persistence Persistence: The continued or prolonged existence of something. Most Applications Achieve Persistence.
1 Copyright © 2008, Oracle. All rights reserved. Repository Basics.
HIBERNATE/Java Overview of technology for Hibernate by محمد حسن کاظمی پوران Master : M.M.Nematollahi.
Don't Know Jack About Object-Relational Mapping?
Chengyu Sun California State University, Los Angeles
Persistence – Iteration 4 Vancouver Bootcamp
Object/Relational Mapping with Hibernate
Java Data Persistence Using Hibernate
Hibernate Bayu Priyambadha, S.Kom.
Developing and testing enterprise Java applications
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Hibernate Basics 廖峻鋒 Sep 14,2004 NTU Dept. of CSIE

國立政治大學電子計算機中心 What is Persistence ? Ability of an object to survive even current session or program terminate. Source : W.Keller “Persistence Options for Object-Oriented Programs”, JAOO 2003

國立政治大學電子計算機中心 Object-Relational Mapping Automated persistence of object to tables in RDBMS. Usually with the help of metadata that describes the mapping. SQL is auto-generated by the metadata description.

國立政治大學電子計算機中心 ORM Solution An ORM Solution consists of the following pieces: Persistence Manager with CRUD API. Query API Mapping metadata Other cross-cutting concerns : transaction, lazy fetching, catching …

國立政治大學電子計算機中心 ORM Quality [Fussel 97] Pure relational 整個應用程式以 table-oriented 方式來設計 Light object mapping Using DAO pattern to hide SQL/JDBC from object model. Medium object mapping Simple O-R mapping framework, ex: iBATIS SQLMap Full object mapping Supports sophisticated object modeling : composition, inheritance, persistence by reachability, ex : hibernate

國立政治大學電子計算機中心 Generic ORM Problems (1) How persistence classes look like ? 需要 extends 特殊界面嗎 ? 需要 implements serializable 嗎 ? 建構子能不能有參數 ? How mapping metadata defined ? How to map inheritance hierarchies ? How to map object identity, equality as well as database identity.

國立政治大學電子計算機中心 Generic ORM Problems (2) How business logic interact with persistence logic ? How to manage lifecycle of entities ? How to provide sorting, searching and aggregating ? How to retrieve data efficiently ?

國立政治大學電子計算機中心 “Modern” ORM Solutions Transparent Persistence (POJO/JavaBeans) Persistent/transient instances Automatic Dirty Checking Transitive Persistence Lazy Fetching Outer Join Fetching Runtime SQL Generation Three Basic Inheritance Mapping Strategies

國立政治大學電子計算機中心 Hibernate Open Source (LGPL) Popular ( downloads/month) Persistence for JavaBeans Support for very fine-grained, richly typed object models Powerful queries (Criteria and HQL)

國立政治大學電子計算機中心 Hibernate Architecture

國立政治大學電子計算機中心 Four types of Interfaces Perform or support CRUD and query : Session,Transaction, Query. Config : SessionFactory, Configuration. Callback interface : Interceptor, Lifecycle, Validatable. (not covered in this slide) Extension point : UserType, IdentifierGenerator. (not covered in this slide) Please refer to Hibernate in Action chap 8 for details of callback and extension interfaces

國立政治大學電子計算機中心 Core Interfaces Session SessionFactory Configuration Query and Criteria

國立政治大學電子計算機中心 Session Persistence Manager of hibernate. One for each thread – can not be shared, not thread safe. Light weight – inexpensive to create / destroy. A cache of loaded objects related to a single unit of work (I.e. a transaction).

國立政治大學電子計算機中心 SessionFactory Heavy weight, intended to be shared among threads – typically single SessionFactory for whole application. One SessionFacotry per Database.

國立政治大學電子計算機中心 Configuration Specify the location of mapping document. Store hibernate properties. Create SessionFactory. Configuration cfg = new Configuration(); cfg.addClacc(UserInfo.class); SessionFactory factory = cfg.buildSessionFactory();

國立政治大學電子計算機中心 Hibernate Mapping metadata One mapping file per class. Naming convention : (entity class name).hbm.xml Usually put in the same directory with class files Otherwise you have to specify metadata files in Configuration using addRecourse() method.

國立政治大學電子計算機中心 Implementation (for new Application) Write mapping document. Generate class files and create tables (using Ant) Set hibernate.properties. Implement access code with DAO pattern.

國立政治大學電子計算機中心 Implementation (if tables pre-exist) Write class files. Write mapping document. Set hibernate.properties. Implement access code with DAO pattern.

國立政治大學電子計算機中心 Persistence Class in Hibernate JavaBean specification (or POJOs) No-arg constructor Accessor methods for properties

國立政治大學電子計算機中心 A quick example public class PersistableMessage { private Integer id; private String text; private PersistableMessage next; … (getter and setter methods) … }

國立政治大學電子計算機中心 Hibernate.properties Should be put in the classpath. Connection pool -hibernate 內建 support 三種 connection pool : C3p0, apache DBCP, and Proxool

國立政治大學電子計算機中心 Writing Metadata

國立政治大學電子計算機中心 ID generating strategies

國立政治大學電子計算機中心 Retrieving Objects Hibernate Query Language (HQL) “Minimal” OO dialect of ANSI SQL Criteria Queries Extensible framework for expressing query criteria as objects Includes “query by example” Native SQL Queries (NamedQuery)

國立政治大學電子計算機中心 Hibernate Query Language Example: select item from AuctionItem item join item.bids bid where item.description like ‘hib%’ and bid.amount > 100 i.e. get all the AuctionItem s with a Bid worth > 100 and description that begins with “hib”

國立政治大學電子計算機中心 Criteria Queries Criteria criteria = session.createCriteria(PersistableMessage.class); criteria.add(Expression.eq("id", Integer.valueOf("1"))); PersistableMessage result = (PersistableMessage) criteria.uniqueResult();

國立政治大學電子計算機中心 demo Code generation with hbm2java Auto-import with hbm2ddl Client code Test add message with transaction

國立政治大學電子計算機中心 Fine-grained Persistence “More classes than tables” Fine-grained object models are good Greater code reuse More typesafe Better encapsulation

國立政治大學電子計算機中心 Dependent Mapping Entity has its own database identity (primary key). Value is owned by an entity, its lifecycle dependents on owning entity. Hibernate use to map value type properties.

國立政治大學電子計算機中心 Inheritance Mapping

國立政治大學電子計算機中心 Three ways to map Inheritance Single Table Inheritance Concrete Table Inheritance Class Table Inheritance

國立政治大學電子計算機中心 Single Table Inheritance Map all the classes of an inheritance hierarchy to a single table

國立政治大學電子計算機中心 Single Table Inheritance in Hibernate 以 discriminator 欄位區分是那一個子類別 在父類別中定義 discriminator 在子類別中定義 discriminator value

國立政治大學電子計算機中心 Fine-grained object model : An example A more complex example : CACS membership management system.

國立政治大學電子計算機中心 Topics not covered Advanced associations mapping Transaction / concurrency

國立政治大學電子計算機中心 Mapping Association One-to-one Many-to-one One-to-many Unidirectional / bidirectional

國立政治大學電子計算機中心 Q&A