Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django...

34
Object Relational Mapping Recitation 6 February 20 th , 2020

Transcript of Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django...

Page 1: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Object Relational Mapping

Recitation 6February 20th, 2020

Page 2: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Outline

• Introduction to Object Relational Mapping

• ORM case study (1): JPA

• ORM case study (2): Django ORM

Page 3: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Outline

• Introduction to Object Relational Mapping

• ORM case study (1): JPA

• ORM case study (2): Django ORM

Page 4: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Building a web application…

ID:

Name:

GPA:

Create Student Sid Name gpa

22 Adam 3.6

23 Joy 3.2

24 AG 4.0

Page 5: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Database

JDBC

SQL

JPA

Servlets

WebSockets JSON Restful WS JSP + HTML

SQL

ORM

SQL

Building a web application…

Page 6: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Database

JDBC

SQL

JPA

Servlets

WebSockets JSON Restful WS JSP + HTML

SQL

ORM

SQL

Building a web application…

Page 7: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Database

JDBC

SQL

JPA

Servlets

WebSockets JSON Restful WS JSP + HTML

SQL

ORM

SQL

Building a web application…

Page 8: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Database

JDBC

SQL

JPA

Servlets

WebSockets JSON Restful WS JSP + HTML

SQL

ORM

SQL

Building a web application…

Presentation

Logic / Business

Data Layer

What happens here?

Page 9: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Building a web application…

What happens here?

? Sid Name gpa

22 Adam 3.6

23 Joy 3.2

Page 10: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Building a web application…

What happens here?

Sid Name gpa

22 Adam 3.6

23 Joy 3.2API

JDBC

JDBC Example:

Page 11: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Building a web application…

JDBC Example:

Static SQL code

Student is an object, we need to break it down to an SQL code…

What if we are inserting 100 records to two tables?

When I receive an SQL query result, how do I convert it back to an object?What if I decide to change

my database from SQL to Oracle?

Page 12: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Database

JDBC

SQL

JPA

Servlets

WebSockets JSON Restful WS JSP + HTML

SQL

ORM

SQL

Building a web application…

Page 13: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Database

JDBC

SQL

JPA

Servlets

WebSockets JSON Restful WS JSP + HTML

SQL

ORM

SQL

Building a web application…

Page 14: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

“Think like an object”Relation / Table Class

Record / Row / Tuple ObjectAttribute / Column Member / Field

Hierarchy (Is-A) InheritanceRelationship Composition / Aggregation

Sailors (sid: integer, sname: string, rating: integer, age: real)

class Sailors {int sid;String sname;int rating;double age;

} Sid Sname Rating Age

Page 15: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

“Think like an object”Relation / Table Class

Record / Row / Tuple ObjectAttribute / Column Member / Field

Hierarchy (Is-A) InheritanceRelationship Composition / Aggregation

Sailors (sid: integer, sname: string, rating: integer, age: real)

class Sailors {int sid;String sname;int rating;double age;

}

Sailors S1 = new Sailor()S1.sid = 123;S1.sname = Zeinab;S1.rating = 9;S1.age = 26

Sid Sname Rating Age

123 Zein.. 9 26

Page 16: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

“Think like an object”Relation / Table Class

Record / Row / Tuple ObjectAttribute / Column Member / Field

Hierarchy (Is-A) InheritanceRelationship Composition / Aggregation

Sailors (sid: integer, sname: string, rating: integer, age: real)

class Sailors {int sid;String sname;int rating;double age;

}

Sailors S2 = new Sailor()S2.sid = 124;S2.sname = Omar;S2.rating = 10;S2.age = 32

Sid Sname Rating Age

123 Zein.. 9 26

124 Oma.. 10 32

Page 17: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

“Think like an object”Relation / Table Class

Record / Row / Tuple ObjectAttribute / Column Member / Field

Hierarchy (Is-A) InheritanceRelationship Composition / Aggregation

Sailors (sid: integer, sname: string, rating: integer, age: real)

class Sailors {int sid;String sname;int rating;double age;

}

Sailors S2 = new Sailor()S2.sid = 124;S2.sname = Omar;S2.rating = 10;S2.age = 32

Sid Sname Rating Age

123 Zein.. 9 26

124 Oma.. 10 32

Page 18: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Outline

• Introduction to Object Relational Mapping

• ORM case study (1): JPA (Entity & Inheritance)

• ORM case study (2): Django ORM

Page 19: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Introduction to ORM

SQL

Database

JDBC

SQL

JPA JTA

Servlets

WebSockets JSON RESTful WS JSP + HTML

ORM

SQL

Page 20: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

JPA Walkthrough

Order Product

InvoiceSalesman

ccn

salary

User

uid pass

name

oid placed_on

iid paid_on

pid price

Customer

priceqty

Page 21: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

@Entity

class Customer{

String username;

String password;

String full_name;

String cc_number;

boolean logged_in;

Set<Order> orders;}

Customer

name ccn

uid pass

Page 22: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

@Entity

@Entity

class Customer{

@Id

String username;

String password;

String full_name;

String cc_number;

@Transientboolean logged_in;

@...Set<Order> orders;

}

Customer

name ccn

uid pass

Page 23: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

@Entity

@Entity@Table (name = "Customers", schema = "V2")class Customer{

@Id@Column (name = "cid")String username;

@Column (nullable = false)String password;

@Column (name = "name", nullable = false)String full_name;

@Column (name = "ccn")String cc_number;

@Transientboolean logged_in;

@...Set<Order> orders;

}

Customer

name ccn

uid pass

Page 24: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Inheritance

Salesman

ccn

salary

User

uid pass

name

Customer

Page 25: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Inheritance

@[email protected] User{

@Id@Column (name = "uid")String username;

@Column (nullable = false)String password;

@Column (nullable = false)String full_name;

@Transientboolean logged_in;

}

Salesman

ccn

salary

User

uid pass

name

Customer

Page 26: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Inheritance

@Entityclass Customer extends User{

String cc_number;}

@Entityclass Salesman extends User{

int salary;}

Salesman

ccn

salary

User

uid pass

name

Customer

Page 27: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Inheritance

@Entity@MappedSuperClass

class User{

@Id@Column (name = "uid")String username;

@Column (nullable = false)String password;

@Column (nullable = false)String full_name;

@Transientboolean logged_in;

}

Fields get embeddedwith subclass entities

Salesman

ccn

salary

User

uid pass

name

Customer

Customer

uid pass name ccn

myahmad 1234 Yousuf 123456

Salesman

uid pass name salary

tjab p@$$ Tamim

Page 28: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Inheritance

@Entity| @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)| @Inheritance(strategy = InheritanceType.SINGLE)| @Inheritance(strategy = InheritanceType.JOIN)abstract class User{

@Id@Column (name = "uid")String username;

@Column (nullable = false)String password;

@Column (nullable = false)String full_name;

@Transientboolean logged_in;

}

Salesman

ccn

salary

User

uid pass

name

Customer

Page 29: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Outline

• Introduction to Object Relational Mapping

• ORM case study (1): JPA

• ORM case study (2): Django ORM: you will be using Django for your projects …

Page 30: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Model View Controller (MVC)

Controller

Request/ Command

Model

Request information

Response information

View

Send Data

Renders Response

Page 31: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Model View Controller (MVC)

Controller

Request/ Command

Model

Request information

Response information

View

Send Data

Renders Response

View

Template

Page 32: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Model View Controller (MVC)Model View Template (MVT)

Controller

Request/ Command

Model

Request information

Response information

View

Send Data

Renders Response

View

Template

Page 33: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Model View Template (MVT)

View

Request/ Command

Model

Request information

Response information

Template

Send Data

Renders Response

Page 34: Object Relational Mappingmhhammou/15415-s20/recitations/Reci… · •ORM case study (2): Django ORM. Introduction to ORM SQL Database JDBC SQL JPA JTA Servlets WebSockets JSON RESTful

Next Recitation…

• Mapping relationships

• Practice with Django