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.

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

Connecting to Databases. connecting to DB DB server typically a standalone application Server runs on localhost for smaller sites –i.e. Same machine as.
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Copyright  Oracle Corporation, All rights reserved. 2 Java and Databases: An Overview.
Chapter 10 ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a programming interface to access data in a database.
Distributed Application Development B. Ramamurthy.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 32 Java Database.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
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.
CSE 190: Internet E-Commerce Lecture 13: Database code.
JDBC. In This Class We Will Cover: What SQL is What ODBC is What JDBC is JDBC basics Introduction to advanced JDBC topics.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Session-02.
Session-01. Hibernate Framework ? Why we use Hibernate ?
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
An Introduction to Hibernate Matt Secoske
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Agenda What is Hibernate Spring Integration Questions Overview
Interacting With Data Databases.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 8 Object Oriented Programming in Java Advanced Topics Java Database.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
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.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
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.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
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)
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
DataBases and SQL INFSY 547 Spring Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Database Connectivity What is ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a Microsoft Active-X component.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
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.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Introduction to ORM Hibernate Hibernate vs JDBC. May 12, 2011 INTRODUCTION TO ORM ORM is a programming technique for converting data between relational.
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 (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.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
HIBERNATE/Java Overview of technology for Hibernate by محمد حسن کاظمی پوران Master : M.M.Nematollahi.
Chengyu Sun California State University, Los Angeles
Java Data Persistence Using Hibernate
Interacting with Database
Hibernate Bayu Priyambadha, S.Kom.
Data Model.
Java Database Connectivity
Chapter 10 ADO.
Java API for Database Connectivity
Chengyu Sun California State University, Los Angeles
Presentation transcript:

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 using object oriented techniques and languages. Persistence is the key of saving data Many of today’s applications use databases Dynamic websites Management applications (ERP, CRM, etc.) Many many more… How do we easily combine the two? Or: How do we easily persist objects?

June 2nd, 2004 Object Oriented Design Course 3 A Brief Introduction To Databases (1) We want to represent pieces of data Same meta data Student – first name, last name, … Course – number, name, … We want to relate between data Which courses does a student take?

June 2nd, 2004 Object Oriented Design Course 4 A Brief Introduction To Databases (2) We define our data as entities We define their connections as relationships And then define the Entity- Relationship Diagram More on that in the databases course Very recommended!

June 2nd, 2004 Object Oriented Design Course 5 Example – The Relations Student Id FirstName LastName Course Number Name Description Course Participation Id StudentId CourseNumber Grade Table Relations (Constraints)

June 2nd, 2004 Object Oriented Design Course 6 Example – The Tables (1) IdFirstNameLastName BartSimpson LisaSimpson MilhouseVan Houten NumberNameDescription 67615oodObject Oriented Design 67506dbDatabases 67808osOperating Systems Courses Students

June 2nd, 2004 Object Oriented Design Course 7 Example – The Tables (2) IdStudentIdCourseNumberGrade How do we use all this data ?

June 2nd, 2004 Object Oriented Design Course 8 CRUD Operations (I) Create INSERT INTO Students (Id, FirstName, LastName) VALUES (55, “Maggie”, “Simpson”) Read (or Retrieve) SELECT FirstName,LastName FROM Studnets WHERE FirstName=“Maggie”

June 2nd, 2004 Object Oriented Design Course 9 CRUD Operations (II) Update UPDATE Students SET FirstName=“Mag” WHERE Id=55 Delete DELETE FROM Students WHERE FirstName=“Mag”

June 2nd, 2004 Object Oriented Design Course 10 The Objects

June 2nd, 2004 Object Oriented Design Course 11 Database Connection SQL is an embedded language (first introduced commercially in 1979 by Oracle) Inserted as part of the code How to connect to the database? Special libraries Special network protocol General interface libraries – ODBC, JDBC, etc.

June 2nd, 2004 Object Oriented Design Course 12 ODBC (I) Open Database Connectivity (1992) Access any data from any application No matter what the database is! The data can also be in Excel spreadsheets or text files.

June 2nd, 2004 Object Oriented Design Course 13 ODBC (II) Information is transferred from relations to objects and vice-versa databases are optimized for searching/indexing objects are optimized for engineering/flexibility The basic idea – introduce a general database API to the application Each database has different implementation

June 2nd, 2004 Object Oriented Design Course 14 ODBC API Driver The basic hook to the system Connection Holds the connection to the database RecordSet Wraps a result of database query Command Used to send commands (insert, delete) to the database

June 2nd, 2004 Object Oriented Design Course 15 Example Set conn = new ADODB.Connection conn.Open “ ” Set rs = new ADODB.RecordSet rs.Open “SELECT * From Students”, _ conn For Each f in rs.Fields Response.Write “ ” & f.Name & _ “ ” Next

June 2nd, 2004 Object Oriented Design Course 16 Example (II) While Not rs.EOF Response.Write “ ” For Each f in rs.Fields Response.Write “ ” Response.Write rs(f.Name) Response.Write “ ” Next Response.Write “ ” rs.MoveNext Wend

June 2nd, 2004 Object Oriented Design Course 17 JDBC Java Database Connectivity From the first version of Java Was modeled on ODBC Driver, Connection and ResultSet ODBC lack Statements PreparedStatment, CreateStatment Extensive use of the factory pattern

June 2nd, 2004 Object Oriented Design Course 18 CRUD Problems Has to be written and maintained for each object Tedious and error prone work Most of the code is similar Only table name and fields change

June 2nd, 2004 Object Oriented Design Course 19 Object/Relational (O/R) Mapping Tools Specifications EJB CMP (Container Managed Persistence) JDO Commercial Products TopLink Open Source Tools Hibernate OJB Many many more …

June 2nd, 2004 Object Oriented Design Course 20 Hibernate High performance, object/relational persistence and query service in Java. provides a bridge between the object and relational worlds Hibernate is an open source project Created by Gavin King Now belongs to the JBoss group

June 2nd, 2004 Object Oriented Design Course 21 How does hibernate works? First, one must create all java objects which reflect the DB structure: Class Course{ public Course(int id, String name, String Description){... } public int getID(){... } public void setID(int id){... }.. Note that there is no JDBC or SQL in here.

June 2nd, 2004 Object Oriented Design Course 22 How to use the structure? // 1. Build a Course Course c = new Course(67615, "ood", "Object Oriented Course"); // 2. Fire up Hibernate Configuration cfg = new Configuration(); cfg.addClass(Course.class); SessionFactory sf = cfg.buildSessionFactory(); // 3. Open Session Session session = sf.openSession(); // 4. Save Product and close Session Transaction tx = sess.beginTransaction(); session.save(c); tx.commit(); session.close();

June 2nd, 2004 Object Oriented Design Course 23 Behind The Curtains How does Hibernates knows how to relate the class Product to the table? XML definition file for each class Defines its mapping to the table in the database Defines its relations to other classes

June 2nd, 2004 Object Oriented Design Course 24 Course.hbm.xml <column name="name" sql-type="char(255)" not-null="true"/> <column name=" description" sql-type="char(255)" not-null="true"/>

June 2nd, 2004 Object Oriented Design Course 25 HQL - Hibernate Query Language We are working on objects, not tables SQL cannot be used HQL is similar to SQL, but works on objects

June 2nd, 2004 Object Oriented Design Course 26 HQL Example Query q = session.createQuery(“select student from student in class tir11.Student where student.name=:name”); q.setString(“name”, “maggie”); List l = q.list(); Student maggie = l.get(0);

June 2nd, 2004 Object Oriented Design Course 27 Updating & Deleting //update maggie.setName(“mag”); session.saveOrUpdate(maggie); //delete Session.delete(bart); Tx.commit();

June 2nd, 2004 Object Oriented Design Course 28 Relations How to declare the relation between Student and CourseParticipation ? Student.hbm.xml <set name="courses" inverse="true” table="CourseParticipations"> <one-to-many class= "tir11.CourseParticipation"/>

June 2nd, 2004 Object Oriented Design Course 29 And on the other side <class name="tir11.CourseParticipation" table="CourseParticipations"> <many-to-one name=“student" class=“tir11.Student" column=“StudentId" /> <many-to-one name=“course" class="tir11.Course" column=“CourseNumber"/>

June 2nd, 2004 Object Oriented Design Course 30 Example Course ood =...; Course db =...; Student bart = new Student( , “Bart”, “Simpson”); bart.addCourse(ood); bart.addCourse(db); tx.begin(); session.save(bart); tx.commit() Session.close();

June 2nd, 2004 Object Oriented Design Course 31 How can that happen? Reflection Bytecode manipulation

June 2nd, 2004 Object Oriented Design Course 32 Objects & Databases Two widely used technologies … that not always cooperate easily Direct CRUD operations are expensive to write (test!) and maintain O/R mapping tools can ease the development

June 2nd, 2004 Object Oriented Design Course 33 More information /01/14/hibernate.html Similar to the example shown in class