0% found this document useful (0 votes)
8 views

Lecture 07 - Database Connectivity - (Part - 2)

Uploaded by

Sherissa Pinnock
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 07 - Database Connectivity - (Part - 2)

Uploaded by

Sherissa Pinnock
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Database Connectivity - Part 2

Object Relational Mapping


ADVANCED PROGRAMMING
Lecture 7

Prepared by:
Christopher Panther
From Last Week - Database Connectivity

What is Database Connectivity?


- Application Programing interfaces (API) that
manages connecting and interacting with
databases.
Mainly relating to Relational Databases
-

2
From Last Week - Database Connectivity Types
• JDBC (Java DataBase Connectivity) – Java (Our focus)
• ODBC (Open DataBase Connectivity) – C
- Microsoft
• SQLJ – C, FORTRAN, others
• OLE-DB (Object Linking and Embedding for Database)
- Microsoft
• ADO.NET(ActiveX Data Objects)
- Microsoft
3
From Last Week - Database Connectivity (Java)
Java Database Connectivity (JDBC) is the:
- Java API that manages connecting to a database,
issuing queries and commands, and handling result
sets obtained from the database
- JDBC was one of the first components developed
for the Java persistence layer.
- Oriented toward relational databases

4
Relational Database Management Systems
(RDBMS)
Main Features
• Data stored as tables
• Data maybe be present in multiple tables which
can be related to each other
• Multiple data elements can be accessed at same
time
• Other features…
5
Object Relational Mapping (ORM)
• Programming Technique
– Conversion of Data
• Object-Oriented to Relational (Domain Class to Relational)
• Vice Versa
• Data Management
– Primarily with objects
• Concerned with persistence
– Domain objects
6
Object to Relational Mapping
Mapping or Associating a domain/class
object’s data fields/attributes to specific
columns in tables in a Relational
Database

7
Object to Relational Mapping
Java objects

Java

8
Object to Relational Mapping Tools - Java
• Hibernate (Our focus)
• Apache OpenJPA
• Ebean
• EclipseLink
• Enterprise JavaBeans (EJB)
• Java Data Objects (JDO)
• Others

9
Object to Relational Mapping Tools -
Microsoft.NET
• Base One Foundation Component Library
• Dapper
• Entity Framework
• LINQ to SQL
• NHibernate
• nHydrate
• Quick Objects
• Others
10
Object-Relational Tools
• ORM API
– Classes, Interfaces and Libraries
– Methods
• ORM Configuration
– Configuration Files
• Define interaction parameters for Database
– Mapping Files
• Map DB tables to OO classes
• Database
– Persistence Layer
11
ORM Advantages
• Rapid Development
– Easier for developer to work in one paradigm
– High Abstraction (concerned with logic)
– SQL Generation
– Reusable
– Special OO Query Language
– Transaction API
• (Unit of Work Pattern) Single transaction that involves multiple
operations of Insert/Update/Delete, etc.
– Automatic Concurrency Management
12
ORM Disadvantages
• Performance Overhead
– Conversion to SQL
• Database only understands SQL
• Timely setup and configuration
• Learning Curve
• Software Bloat
– Extra libraries and API

13
Hibernate 5 (ORM Tool)
• Developed by jBoss
• Download (latest):
https://sourceforge.net/projects/hibernate/files/hibernat
e-orm/5.4.20.Final/hibernate-release-
5.4.20.Final.zip/download
• Pre-requisites:
• Log4j2:
https://www.apache.org/dyn/closer.lua/logging/log4j/2.1
3.3/apache-log4j-2.13.3-bin.zip

14
Required for Project
•Hibernate 5

•Log4j2

•MySQL Connector J

15
Setting up Hibernate 5
• Download latest stable release of hibernate
• Unzip downloaded file to the C: drive or a folder
of your choice
• Add the hibernate-release-
5.4.18.Final/lib/required folder to your Class Path
• Restart your system OR logout and log back in

16
Set up Project to use Hibernate
• Create a Java Project
• Add the hibernate-release-5.4.18.Final/lib/required
folder to your project’s Build Path
• Add the Connector J:- mysql-connector-java Jar file to
your Project’s Build Path
• Create the hibernate.cnf.xml Configuration file in your
project’s root directory/src folder

17
Hibernate Configuration File
hibernate.cfg.xml

18
Consider this ORM – View 1

19
Consider this ORM – View 2

20
Using Hibernate in your program
•Two ways to implement Hibernate ORM in
your programs:
1. Using Hibernate-Class Mapping xml files;
and
2. Using Javax class annotation

21
Using Hibernate (Method 1)
hbm.xml file Example

22
Using Hibernate (Method 2)
Javax Class Annotation

23
Using Hibernate API to Setup Connection

24
Using Hibernate API to Setup Connection Cont’d

25
Create a Record Using Hibernate API

26
Update a Record Using Hibernate API

27
Delete a Record Using Hibernate API

28
Retrieve Records Using Hibernate API

public List selectAllStaff() {


List staffList = null;
Session session = getSession();
Transaction trans = session.beginTransaction();
staffList = session.createSQLQuery("SELECT * FROM staff").getResultList();
trans.commit();
session.close();
return staffList;
}

29
Retrieve Record By Primary Key Using Hibernate API

public Customer selectStaff(int id) {


Session session = getSession();
Transaction trans = session.beginTransaction();
Staff obj = (Staff) session.get(Staff.class, id);
trans.commit();
session.close();
return obj;
}
30
Hibernate Query Language (HQL) -
OO Query Language
• Special Object-Oriented Query Language
• Special Syntax
• SQL Like operations but Not SQL
– Converted to SQL by ORM
• Query Result Constraints
– Clauses in HQL
– Criteria in QBC (Query By Criteria)
– Example in QBE (Query By Example)
31
Query by Criteria Using Hibernate API

public Staff selectStaffQBC(int id) {


Session session = getSession();
Transaction trans = session.beginTransaction();
Criteria criteria = session.createCriteria(Staff.class);
criteria.add(Restrictions.like("id", id));
List results = criteria.getResultList();
trans.commit();
session.close();
return (Staff)results.get(0);
}
32
Query By Using Hibernate API

public Staff selectStaffQBE(Staff obj) {


Session session = getSession();
Transaction trans = session.beginTransaction();
Criteria criteria = session.createCriteria(Staff.class);
criteria.add(Example.create(obj));
List results = criteria.getResultList();
trans.commit();
session.close();
return (Staff)results.get(0);
}
33
HQL Query Using Hibernate API

34
Questions

35

You might also like