Java Data Persistence Using Hibernate Jack Gardner October 2004.

Slides:



Advertisements
Similar presentations
TPTP Data Model 1 Database support for TPTP EMF based data models using Elver/Hibernate Guru Nagarajan July 14, 2006.
Advertisements

Rapid Persistence Layer Development with Hibernate Rapid Persistence Layer Development with Hibernate Tyler Mendenhall E-gineering,
ODBC and JDBC O/R Mapping David Rabinowitz. June 2nd, 2004 Object Oriented Design Course 2 Objects And Databases Most of the applications today are written.
ODBC and JDBC O/R Mapping Amit Shabtay. June 2nd, 2004 Object Oriented Design Course 2 Objects And Databases Most of the applications today are written.
Session-02.
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.
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.
The Easiest Way To Learn Hibernate: YouTube Videos and the Online Tutorial
Rapid Persistence Layer Development with Hibernate Rapid Persistence Layer Development with Hibernate Tyler Mendenhall E-gineering,
Object Persistence using Hibernate
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.
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
CS 157B: Database Management Systems II January 30 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
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.
CHAPTER 8: MANAGING DATA RESOURCES. File Organization Terms Field: group of characters that represent something Record: group of related fields File:
Robert Greiner CSE7330 Southern Methodist University Getting Things Done with Hibernate.
Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools.
Object/Relational Mapping with Hibernate Practical ORM.
CS 160: Software Engineering November 5 Class Meeting Department of Computer Science San Jose State University Fall 2014 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.
A Simple Java Relational Database Thomas A. Bullinger March 20, 2001
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.
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
Jozef Kuper.  Describe a Database  Entities  Atributes  Relationships.
In this session, you will learn to: Map an ER diagram to a table Objectives.
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)
Hibernate Thuy, Le Huu Pentalog HAN. What is Hibernate? Hibernate is a free, open source Java package that makes it easy to work with relational databases.
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.
March 1, 2004CS WPI1 CS 509 Design of Software Systems Lecture #6 Monday, March 1, 2004.
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
Hibernate 4.3. Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieve the developer from common data persistence.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Introduction – ORM, Helloworld Application
Sean Chambers.  ORM stands for Object Relational Mapper  Maps your POCO (plain old clr objects) to your relational model using XML config  Relieves.
MANY-TO-MANY MAPPING. May 12, 2011  Implemented using a Set java collection  Mediator table is mandatory – Join table  Join table contains foreign.
Hibernate Basics 廖峻鋒 Sep 14,2004 NTU Dept. of CSIE.
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 4.3. Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieves the developer from common data persistence.
Hibernate Java Persistence API. What is Persistence Persistence: The continued or prolonged existence of something. Most Applications Achieve Persistence.
Современные технологии баз данных Лекция 7. Объектно-реляционное отображение (ORM) Технология отображения объектов какого- либо объектно-ориентированного.
HIBERNATE/Java Overview of technology for Hibernate by محمد حسن کاظمی پوران Master : M.M.Nematollahi.
Don't Know Jack About Object-Relational Mapping?
CS6320 – Java Persistence API
Chengyu Sun California State University, Los Angeles
Object/Relational Mapping with Hibernate
Java Data Persistence Using Hibernate
Hibernate Bayu Priyambadha, S.Kom.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Java Data Persistence Using Hibernate Jack Gardner October 2004

2 Overview What is Hibernate What is ORM Create Example Read Examples Persistable Classes Database Table Mapping Class to Table Configuration Configuration Properties Session Transaction Object Lifecycle Query Options Associations References

3 What is Hibernate? An object/relational mapping (ORM) implementation Open source Development started late 2001

4 What is Object/Relational Mapping? Provides a transparent bridge between objects and database tables Allows source code to work with objects and their attributes vs. tables and columns Eliminates need for most/all SQL Eliminates use of query result sets

5 Create Example // create a new object Widget w = new Widget(); w.setName(“WidgetA”); w.setValue(10); // persist it session.save(w);

6 Read Examples // get a known widget Widget w = (Widget) session.load(Widget.class, “WidgetA”); // get all widgets List widgets = session.find(“from Widget”);

7 Persistable Classes Classes are simply JavaBeans public class Widget { private String name; private int value; public Widget() {} public String getName() {return name;} public void setName(String s) {name = s;} public int getValue() {return value;} public void setValue(int i) {value = i;} }

8 Database Table Persistable classes have an associated table

9 Mapping Class to Table widget.hbm.xml

10 Configuration import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.SessionFactory; // build configuration based on properties Configuration config = new Configuration(); // add persistable classes to configuration config.addClass(Widget.class); // build a session factory based on configuration SessionFactory sessionFactory = config.buildSessionFactory();

11 Configuration Properties hibernate.properties hibernate.dialect=net.sf.hibernate.dialect.Oracle9Dialect hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver hibernate.connection.username= hibernate.connection.password=

12 Session Lightweight and inexpensive to create/destroy Not threadsafe – each thread needs its own Obtained from SessionFactory instance Session session = sessionFactory.openSession(); // perform persistence operations session.close();

13 Transaction Set of operations that are all committed or all rolled back Obtained from Session instance Can perform multiple transactions within session Transaction tx = session.beginTransaction(); // perform persistence operations tx.commit();

14 Object Lifecycle Transient Newly created object Persistent New object has been “saved” Previously saved object has been “read” Detached Session closed Persistent object serialized Can be reattached later to become persistent

15 Query Options Hibernate Query Language (HQL) Query q = session.createQuery(“from Widget w where w.value > :value”); q.setParameter(“value”, someValue); List widgets = q.list(); Criteria API Criteria c = session.createCriteria(Widget.class); c.add(Expression.gt(“value”, someValue); List widgets = c.list(); SQL Query q = session.createSQLQuery(“select {w.*} from WIDGET {w} where VALUE > :value”, “w”, Widget.class); q.setParameter(“value”, someValue); List widgets = q.list();

16 Associations Supports all association types one-to-one one-to-many many-to-many Inherently unidirectional Supports bidirectional

17 One-to-Many Example: Unidirectional Option 1: Group  Member public class Group public class Member{ private String name; private String name; private Collection members; … …} } Option 2: Group  Member public class Grouppublic class Member{ private String name; private String name; … private Group group; } … }

18 One-to-Many Example: Bidirectional Group  Member public class Group public class Member{ private String name; private String name; private Collection members; private Group group; …} Application responsible for maintaining each end of association

19 One-to-Many Table Structure Underlying table structure not affected by directionality Standard implementation Foreign key in many-side table Alternate implementation Use join table, with no-duplicate constraint on many-side foreign key

20 One-to-Many Table Structure

21 Many-to-Many Example: Unidirectional Option 1: Group  Member public class Group public class Member{ private String name; private String name; private Collection members; … …} } Option 2: Group  Member public class Grouppublic class Member{ private String name; private String name; … private Collection groups; } … }

22 Many-to-Many Example: Bidirectional Group  Member public class Group public class Member{ private String name; private String name; private Collection members; private Collection groups; … …} Application responsible for maintaining each end of association

23 Many-to-Many Table Structure Underlying table structure not affected by directionality Implemented using join table

24 Many-to-Many Table Structure

25 References Bauer, Christian and Gaven King, Hibernate in Action, Manning, Hibernate Reference Documentation, Version 2.1.6, Hibernate, 2004.