CS422 Principles of Database Systems Object-Relational Mapping (ORM) Chengyu Sun California State University, Los Angeles.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 10: Designing Databases
.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.
Data Management Design
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
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 ?
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Object Persistence and Object-Relational Mapping James Brucker.
Object and object-relational databases 1. Object databases vs. Object-relational databases Object databases Stores complex objects – Data + functions.
Embedded SQL Host Language (record-oriented) DBMS (set-oriented) 1. Query 3. Process a tuple at a time 4. Close Cursor 2. Evaluate query. Provide cursor.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
Database System Concepts and Architecture Lecture # 3 22 June 2012 National University of Computer and Emerging Sciences.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
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.
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006.
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.
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.
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.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
Database Basics BCIS 3680 Enterprise Programming.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
1 Nov 29, 2005 Object Relational Mapping Frameworks Wiene Höweler.
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
Persistence – Iteration 4 Vancouver Bootcamp Aaron Zeckoski
Basics of JDBC Session 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
Sean Chambers.  ORM stands for Object Relational Mapper  Maps your POCO (plain old clr objects) to your relational model using XML config  Relieves.
1 10 Systems Analysis and Design in a Changing World, 2 nd Edition, Satzinger, Jackson, & Burd Chapter 10 Designing Databases.
Introduction to ORM Hibernate Hibernate vs JDBC. May 12, 2011 INTRODUCTION TO ORM ORM is a programming technique for converting data between relational.
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.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Hibernate Online Training. Introduction to Hibernate Hibernate is a high-performance Object-Relational persistence and query service which takes care.
CS422 Principles of Database Systems JDBC and Embedded SQL 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.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
CS422 Principles of Database Systems Object-Oriented Features in DBMS Chengyu Sun California State University, Los Angeles.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
HIBERNATE/Java Overview of technology for Hibernate by محمد حسن کاظمی پوران Master : M.M.Nematollahi.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA Chengyu Sun California State University, Los Angeles.
Don't Know Jack About Object-Relational Mapping?
CS3220 Web and Internet Programming Database Access with JDBC
Chengyu Sun California State University, Los Angeles
CS320 Web and Internet Programming SQL and MySQL
A very brief introduction
Persistence – Iteration 4 Vancouver Bootcamp
Monitoring Java Applications with JAMon
Hibernate Bayu Priyambadha, S.Kom.
Java Database Connectivity
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

CS422 Principles of Database Systems Object-Relational Mapping (ORM) Chengyu Sun California State University, Los Angeles

The Object-Oriented Paradigm The world consists of objects So we use object-oriented languages to write applications We want to store some of the application objects (a.k.a. persistent objects), e.g. accounts, customers, employees So we use a Object Database?

The Reality of DBMS Relational DBMS are still predominant Best performance Most reliable Widest support Bridge between OO applications and relational databases CLI and embedded SQL Object-Relational Mapping (ORM) tools

Employee – Application Object public class Employee { Integerid; Stringname; Employeesupervisor; }

Employee – Database Table create table employees ( idinteger primary key, namevarchar(255), supervisor integer references employees(id) );

From Database to Application So how do we construct an Employee object based on the data from the database? public class Employee { Integerid; Stringname; Employeesupervisor; public Employee( Integer id ) { // access database to get name and supervisor … … }

Problems with CLI and Embedded SQL … SQL statements are hard-coded in applications public Employee( Integer id ) { … PreparedStatment p; p = connection.prepareStatment( “select * from employees where id = ?” ); … }

… Problems with CLI and Embedded SQL … Tedious translation between application objects and database tables public Employee( Integer id ) { … ResultSet rs = p.executeQuery(); if( rs.next() ) { name = rs.getString(“name”); … }

… Problems with CLI and Embedded SQL Application design has to work around the limitations of relational DBMS public Employee( Integer id ) { … ResultSet rs = p.executeQuery(); if( rs.next() ) { … supervisor = ?? }

The ORM Approach customer employee account Application Persistent Data Store ORM tool Oracle, MySQL, SQL Server … Flat files, XML …

Advantages of ORM Make RDBMS look like ODBMS Data are accessed as objects, not rows and columns Simplify many common operations. E.g. System.out.println(e.supervisor.name) Improve portability Use an object-oriented query language (OQL) Separate DB specific SQL statements from application code Caching

Common ORM Tools Java Data Object (JDO) One of the Java specifications Flexible persistence options: RDBMS, OODBMS, files etc. Hibernate Most popular Java ORM tool right now Persistence by RDBMS only Others

Hibernate Application Architecture hibernate

Setup Hibernate Download hibernate core from Add the following jar files to CLASSPATH hibernate-3.0\hibernate3.jar All the jar files under hibernate-3.0\lib The JDBC driver of your DBMS

A Sample Hibernate Application Java classes Employee.java Code to access the persistent objects EmployeeTest.java O/R Mapping files Employee.hbm.xml Hibernate configuration file hibernate.cfg.xml (Optional) Logging configuration files Log4j.properties

Java Classes Plain Java classes (POJOs) except that There must be an identity field Each persistent field must has a pair of getter and setter The identity field is used to uniquely identify an object The getter and setter for a property xxx follows the naming convention: getXxx() setXxx()

O/R Mapping Files Describe how class fields are mapped to table columns Three important types of elements in a a mapping file - when the field is of simple type Association – when the field is of a class type 

Hibernate Configuration Files Tell hibernate about the DBMS and other configuration parameters Either hibernate.properties or hibernate.cfg.xml or both Sample files under hibernate-3.0/etc

Log4j Configuration File Log4j is a logging tool for Java Log levels DEBUG, INFO, WARN, ERROR, FATAL Log output (appender) File Stdout

Access Persistent Objects SessionFactory Configuration Session QueryTransaction Retrieve ObjectsUpdate Objects

Hibernate Query Language (HQL) A query language that looks like SQL, but for accessing objects Automatically translated to DB-specific SQL statements select e from Employee e where e.id = :id From all the Employee objects, find the one whose id matches the given value

Hibernate Query Language (HQL) A query language that looks like SQL, but deals with objects instead of tables E.g. from all the Employee objects, find the one whose id matches the given value OO language-like syntax, for example e.supervisor.name Support named query parameters Automatically translated into DB-specific SQL statement select e from Employee e where e.id = :id

hbm2ddl Generate DDL statements from Java classes and mapping files See hbm2ddl.bat in the sample code

More About Mapping Map collections Map subclasses Table per concrete class Table per class hierarchy Table per subclass

O/R Mapping vs. ER-Relational Conversion O/R MappingER-Relational Conversion ClassEntity Set Attribute AssociationRelationship Subclass table per concrete class table per class hierarchy table per subclass Subclass OO method NULL method ER method

More Hibernate Resource Hibernate in Action by Christian Bauer and Gavin King Hibernate documentation at