ODBC and JDBC O/R Mapping Amit Shabtay. June 2nd, 2004 Object Oriented Design Course 2 Objects And...
-
date post
21-Dec-2015 -
Category
Documents
-
view
213 -
download
0
Transcript of ODBC and JDBC O/R Mapping Amit Shabtay. June 2nd, 2004 Object Oriented Design Course 2 Objects And...
![Page 1: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/1.jpg)
ODBC and JDBCO/R Mapping
Amit Shabtay
![Page 2: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/2.jpg)
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?
![Page 3: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/3.jpg)
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?
![Page 4: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/4.jpg)
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!
![Page 5: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/5.jpg)
June 2nd, 2004 Object Oriented Design Course 5
Example – The Relations
Student
Id
FirstName
LastName
Course
Number
Name
DescriptionCourse Participation
Id
StudentId
CourseNumber
Grade
Table
Relations(Constraints
)
![Page 6: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/6.jpg)
June 2nd, 2004 Object Oriented Design Course 6
Example – The Tables (1)
Id FirstName LastName
111111111 Bart Simpson
222222222 Lisa Simpson
333333333 Milhouse Van Houten
Number Name Description
67615 ood Object Oriented Design
67506 db Databases
67808 os Operating Systems
Cou
rses
S
tud
ents
![Page 7: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/7.jpg)
June 2nd, 2004 Object Oriented Design Course 7
Example – The Tables (2)
Id StudentId CourseNumber Grade
2537 111111111 67506 95
2538 111111111 67615 90
2539 222222222 67615 97
2560 333333333 67808 88
How do we use all this data ?
![Page 8: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/8.jpg)
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”
![Page 9: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/9.jpg)
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”
![Page 10: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/10.jpg)
June 2nd, 2004 Object Oriented Design Course 10
The Objects
![Page 11: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/11.jpg)
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.
![Page 12: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/12.jpg)
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.
![Page 13: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/13.jpg)
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
![Page 14: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/14.jpg)
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
![Page 15: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/15.jpg)
June 2nd, 2004 Object Oriented Design Course 15
Example
Set conn = new ADODB.Connectionconn.Open “<database-name>”Set rs = new ADODB.RecordSetrs.Open “SELECT * From Students”, _conn
For Each f in rs.FieldsResponse.Write “<th>” & f.Name & _
“</th>”Next
![Page 16: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/16.jpg)
June 2nd, 2004 Object Oriented Design Course 16
Example (II)While Not rs.EOFResponse.Write “<tr>”For Each f in rs.Fields
Response.Write “<td>”Response.Write rs(f.Name)Response.Write “</td>”
NextResponse.Write “</tr>”rs.MoveNext
Wend
![Page 17: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/17.jpg)
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
![Page 18: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/18.jpg)
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
![Page 19: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/19.jpg)
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 …
![Page 20: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/20.jpg)
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
•http://www.hibernate.org/
![Page 21: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/21.jpg)
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.
![Page 22: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/22.jpg)
June 2nd, 2004 Object Oriented Design Course 22
How to use the structure?// 1. Build a CourseCourse c = new Course(67615, "ood",
"Object Oriented Course");// 2. Fire up HibernateConfiguration cfg = new Configuration();cfg.addClass(Course.class);SessionFactory sf = cfg.buildSessionFactory();// 3. Open SessionSession session = sf.openSession();// 4. Save Product and close SessionTransaction tx = sess.beginTransaction();session.save(c);tx.commit();session.close();
![Page 23: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/23.jpg)
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
![Page 24: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/24.jpg)
June 2nd, 2004 Object Oriented Design Course 24
Course.hbm.xml<hibernate-mapping> <class name=“tir11.Course" table=“Courses">
<id name="id" type=“int" unsaved-value=“-1" > <column name="id" sql-type=“integer"/> <generator class=“my.generator"/> </id> <property name="name"> <column name="name" sql-type="char(255)" not-null="true"/> </property> <property name=“description"> <column name=" description" sql-type="char(255)" not-null="true"/> </property> </class></hibernate-mapping>
![Page 25: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/25.jpg)
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
![Page 26: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/26.jpg)
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);
![Page 27: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/27.jpg)
June 2nd, 2004 Object Oriented Design Course 27
Updating & Deleting
//update
maggie.setName(“mag”);
session.saveOrUpdate(maggie);
//delete
Session.delete(bart);
Tx.commit();
![Page 28: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/28.jpg)
June 2nd, 2004 Object Oriented Design Course 28
Relations How to declare the relation between Student and CourseParticipation?
Student.hbm.xml<hibernate-mapping> <class name="tir11.Student" table="Students"> <set name="courses" inverse="true” table="CourseParticipations"> <key column="id" /> <one-to-many class= "tir11.CourseParticipation"/> </set> </class></hibernate-mapping>
![Page 29: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/29.jpg)
June 2nd, 2004 Object Oriented Design Course 29
And on the other side
<hibernate-mapping> <class name="tir11.CourseParticipation" table="CourseParticipations"> <property ...> <many-to-one name=“student" class=“tir11.Student" column=“StudentId" /> <many-to-one name=“course" class="tir11.Course" column=“CourseNumber"/> </class></hibernate-mapping>
![Page 30: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/30.jpg)
June 2nd, 2004 Object Oriented Design Course 30
Example
Course ood = ...;Course db = ...;Student bart = new Student(1111111, “Bart”, “Simpson”);
bart.addCourse(ood);bart.addCourse(db);tx.begin();session.save(bart);tx.commit()Session.close();
![Page 31: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/31.jpg)
June 2nd, 2004 Object Oriented Design Course 31
How can that happen?
Reflection Bytecode manipulation
![Page 32: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/32.jpg)
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
![Page 33: 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.](https://reader030.fdocuments.net/reader030/viewer/2022032522/56649d645503460f94a47246/html5/thumbnails/33.jpg)
June 2nd, 2004 Object Oriented Design Course 33
More information
http://www.hibernate.org/ http://www.onjava.com/pub/a/
onjava/2004/01/14/hibernate.html•Similar to the example shown in class