Database Applications (15-415) ORM - Part I Lecture 11, February 11, 2018 Mohammad Hammoud.

Slides:



Advertisements
Similar presentations
V 6, Mats Strandberg ORM With Hibernate.
Advertisements

Chapter 10: Designing Databases
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
Fast Track to ColdFusion 9. Getting Started with ColdFusion Understanding Dynamic Web Pages ColdFusion Benchmark Introducing the ColdFusion Language Introducing.
RIZWAN REHMAN, CCS, DU. Advantages of ORDBMSs  The main advantages of extending the relational data model come from reuse and sharing.  Reuse comes.
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
IST Databases and DBMSs Todd S. Bacastow January 2005.
Object Persistence and Object-Relational Mapping James Brucker.
Java Persistence API Maciej Adamiak. Agenda -Entity, -Entity Operations, -Query Language.
Rice KRAD Data Layer JPA Design Eric Westfall July 2013.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Towards Bboogle 3.0.0: a Technical Walkthrough Patricia Goldweic Sr. Software Engineer AR&T, Northwestern University Brian Nielsen Manager, Faculty Support.
Database System Concepts and Architecture Lecture # 3 22 June 2012 National University of Computer and Emerging Sciences.
Maven for building Java applications By Nalin De Zoysa
1 Lecture 17 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
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.
MIS 385/MBA 664 Systems Implementation with DBMS/ Database Management Dave Salisbury ( )
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
ECE 8443 – Pattern Recognition ECE 3822 – Software Tools For Engineers Topics: Definitions and Terminology Abstraction Schema Tables Types of Databases.
Optimize Your Object Model Paul Dayan – Sales Engineer.
Object Oriented Analysis and Design 1 Chapter 7 Database Design  UML Specification for Data Modeling  The Relational Data Model and Object Model  Persistence.
OK lets start, who am I ?. Nayden Gochev (a.k.a. JOKe) Java Spring Android Hybris GWT EJB JSF RMI JAX-RS Struts JMS JPA Hibernate C# ASP.NET TestStudio.
Chapter 12: Designing Databases
EJB 3.0 Persistence Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Topic : Hibernate 2: Object Persistence and ORM Kaster Nurmukan.
Java Data Persistence Using Hibernate Jack Gardner October 2004.
An Introduction to Object/Relational Persistence and Hibernate Yi Li
Burapha University, 2003 Object-Oriented Analysis Basic of Object Mapping to Relational Database.
Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
Topic : Hibernate 3:Advanced ORM Kaster Nurmukan.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
The Java Persistence API ©SoftMoore ConsultingSlide 1.
Java Persistence API part 1 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
Java Persistence API (JPA) Relationships. Kinds of relationships UML associations and aggregations (ER non- identifying relationships, shared semantics)
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.
JPA in Vaadin CSCI 3130 Winter What is JPA?  Java Persistence API  Allows for “easy” storage of Java Objects  Is a type of Object Relational.
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
Singleton Academy, Pune. Course syllabus Singleton Academy Pune – Course Syllabus1.
Faeez, Franz & Syamim.   Database – collection of persistent data  Database Management System (DBMS) – software system that supports creation, population,
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.
Hibernate Java Persistence API. What is Persistence Persistence: The continued or prolonged existence of something. Most Applications Achieve Persistence.
Hibernate Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan Java Annotation Lesson 1.
12. Chapter 12: Designing Databases Systems Analysis and Design in a Changing World, Fourth Edition 12.
Object-Relational Mapping. Contents  Default Mapping Rules  Elementary Mapping  Embeddables  Relationship Mapping  Inheritance Mapping.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA Chengyu Sun California State University, Los Angeles.
Management Information Systems by Prof. Park Kyung-Hye Chapter 7 (8th Week) Databases and Data Warehouses 07.
Databases and DBMSs Todd S. Bacastow January
CompSci 280 S Introduction to Software Development
Don't Know Jack About Object-Relational Mapping?
CS6320 – Java Persistence API
Chengyu Sun California State University, Los Angeles
Oracle & SQL Introduction
Chengyu Sun California State University, Los Angeles
Advanced Java Programming
Hibernate (JPA) Code First Entity Relations
LECTURE 34: Database Introduction
JPA: Mapping to an existing database
Chengyu Sun California State University, Los Angeles
Object Relational Mapping Tools
CS4961 Software Design Laboratory Understand Aquila Backend
Topics Persistence API for JAVA EE5 Java Transaction API Entity class.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Database Applications (15-415) ORM - Part I Lecture 11, February 11, 2018 Mohammad Hammoud

Today… Last Session: Standard Query Language (SQL) - Part III Today’s Session: Object Relational Mapping (ORM) - Part I Announcements: P1 is due on Thursday, Feb 15 by midnight

Outline Introduction to Object Relational Mapping Java Persistence API Mapping Relationships Mapping Inheritance

The Big Picture Database JDBC JPA JTA Servlets WebSockets JSON SQL JPA JTA Servlets WebSockets JSON RESTful WS JSP + HTML ORM

Object Relational Mapping (ORM) Objects are first-class citizens: “Think like an object” Relation / Table  Class Record / Row / Tuple  Object Attribute / Column  Member / Field Relationship  Composition / Aggregation Hierarchy (Is-A)  Inheritance Objectives Load / Persist objects (typically via a relational database) Application stays oblivious to type of persistent storage Combines the benefits of OOP and RDBMS Precludes the need for writing SQL Generates and executes DDL and DML as needed Provides flexibility w.r.t. mapping strategies

OOP Recap Classes and objects Simple, structured, flexible, and popular Classes Encapsulation (fields and methods) Composition / Aggregation (Has-A) Inheritance (Is-A) Objects Scoped attributes and methods (e.g., private, protected, public) Attribute validation (e.g., through getters and setters) Polymorphism

Object Serialization What to serialize? Attributes and complex types Composed / aggregated objects Inherited objects Object Graph Load/persist partially or fully? When to serialize? Load/persist eagerly or lazily? Animal Human Wild Pet

Outline Introduction to Object Relational Mapping Java Persistence API Mapping Relationships Mapping Inheritance

Java Persistence API (JPA) Java standard specification for ORM Entity = persistable object Uses in-line code annotations for denoting entities, attributes, relationships fine-grained control over mappings Multiple implementations EclipseLink by Oracle (reference) Hibernate by JBoss (popular) Advanced features (e.g., distributed object cache)

Introduction to ORM Database JDBC JPA JTA Servlets WebSockets JSON SQL Database JDBC JPA JTA Servlets WebSockets JSON RESTful WS JSP + HTML ORM

JPA Walkthrough Customer Order Product User Salesman Invoice ccn oid placed_on pid price qty price Customer Order Product name User uid pass Salesman Invoice salary iid paid _on

@Entity class Customer { String username; String password; String full_name; String cc_number; boolean logged_in; Set<Order> orders; } Customer name ccn uid pass

@Entity @Entity class Customer { @Id String username; String password; String full_name; String cc_number; @Transient boolean logged_in; @... Set<Order> orders; } Customer name ccn uid pass

@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; @Transient boolean logged_in; @... Set<Order> orders; } Customer name ccn uid pass

Outline Introduction to Object Relational Mapping Java Persistence API Mapping Relationships Mapping Inheritance

Entity Relationships @OneToMany / @ManyToOne @ManyToMany @OneToOne Relationships are bidirectional Owning side Propagates relationship updates to the database Usually the side with the foreign key Owned side Maps to the owning side Order Customer Order Product Order Invoice

@OneToMany / @ManyToOne class Order { long order_id; Date placed_on; Customer customer; Set<Product> products; Invoice invoice; } Order Customer

@OneToMany / @ManyToOne @Entity class Order { @Id @GeneratedValue (strategy = GenerationType.SEQUENCE) @Column (name = "oid") long order_id; @Column (nullable = false) Date placed_on; @... Customer customer; Set<Product> products; Invoice invoice; } Order Customer

@OneToMany / @ManyToOne @Entity class Customer { ... @OneToMany // owned Set<Order> orders; } class Order @ManyToOne // owning Customer customer; Order Customer

@OneToMany / @ManyToOne @Entity class Customer { ... @OneToMany (mappedBy = "customer", // owned cascade = CascadeType.ALL, fetch = FetchType.EAGER) Set<Order> orders; } class Order @ManyToOne ( // owning optional = false) Customer customer; Order Customer Owned entity always maps to owning entity! Collections are fetched lazily by default!

@ManyToMany class Product { long prod_id; float price; Set<Order> orders; } Order Product

@ManyToMany @Entity class Product { @Id @GeneratedValue (strategy = GenerationType.SEQUENCE) @Column (name = "pid") long prod_id; @Column (nullable = false) float price; @ManyToMany (...) // owning/owned Set<Order> orders; } Order Product

@ManyToMany @Entity class Product { ... @ManyToMany (mappedBy = "products", // owned fetch = FetchType.EAGER) Set<Order> orders; } class Order @ManyToMany (fetch = FetchType.EAGER) // owning Set<Product> products; Order Product

@ManyToMany @Entity class Product { ... @ManyToMany (mappedBy = "products", // owned fetch = FetchType.EAGER) Set<Order> orders; } class Order @ManyToMany (fetch = FetchType.EAGER) // owning @JoinTable (name = "OrderLines", joinColumns = @JoinColumn (name = "oid"), inverseJoinColumns = @JoinColumn (name = "pid")) Set<Product> products; Order Product

@ManyToMany with Attributes Order Product price qty class OrderLine { Order order; Product product; int quantity; float price; } Order Product price qty OrderLine pid

@ManyToMany with Attributes @Entity class OrderLine { // Id? @ManyToOne (optional = false) // owning @JoinColumn (name = "oid") Order order; @ManyToOne (optional = false) @JoinColumn (name = "pid") Product product; @Column (nullable = false) int quantity; float price; } Order Product price qty OrderLine pid

@ManyToMany with Attributes @Entity class Order { ... @OneToMany (mappedBy = "order", // owned cascade = CascadeType.ALL) Set<OrderLine> order_lines; } class Product @OneToMany (mappedBy = "product", // owned Order Product price qty OrderLine pid

@ManyToMany with Attributes @Entity class OrderLine { // Id? @EmbeddedId OrderLineId id; @ManyToOne (optional = false) // owning @JoinColumn (name = "oid") Order order; @ManyToOne (optional = false) @JoinColumn (name = "pid") Product product; @Column (nullable = false) int quantity; float price; } Order Product price qty OrderLine pid @Embeddable class OrderLineId { @Column (name = "oid") long order_id; @Column (name = "pid") long prod_id; }

@OneToOne class Invoice { Order order; long invoice_id; float total; Date paid_on; } Order Invoice

@OneToOne @Entity class Invoice { @... Order order; @Id @GeneratedValue (strategy = GenerationType.SEQUENCE) @Column (name = "iid") long invoice_id; @Column (nullable = false) float total; Date paid_on; } Order Invoice

@OneToOne @Entity class Invoice { @OneToOne (...) // owning Order order; ... } class Order @OneToOne (...) // owned Invoice invoice; Order Invoice

@OneToOne @Entity class Invoice { @OneToOne (optional = false) // owning Order order; ... } class Order @OneToOne (mappedBy = "order", // owned optional = false, cascade = CascadeType.ALL) Invoice invoice; Order Invoice

@OneToOne vs. @Embeddable @Embeddable class Invoice { @Column (name = "iid") long invoice_id; @Column (nullable = false) float total; Date paid_on; } @Entity class Order ... @Embedded Invoice invoice; Order Invoice Embeddable Objects - Do not get their own table - Are not directly queryable - Are deleted w/ their parent

Outline Introduction to Object Relational Mapping Java Persistence API Mapping Relationships Mapping Inheritance

Inheritance Salesman ccn salary User uid pass name Customer

Inheritance @Entity @... class User { @Id @Column (name = "uid") String username; @Column (nullable = false) String password; String full_name; @Transient boolean logged_in; } Salesman ccn salary User uid pass name Customer

Inheritance @Entity class Customer extends User { String cc_number; } class Salesman extends User int salary; Salesman ccn salary User uid pass name Customer

Fields get embedded with subclass entities Inheritance Fields get embedded with subclass entities @Entity @MappedSuperClass class User { @Id @Column (name = "uid") String username; @Column (nullable = false) String password; String full_name; @Transient boolean logged_in; } 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

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; String full_name; @Transient boolean logged_in; } Salesman ccn salary User uid pass name Customer

Outline Java Persistence API Mapping Relationships Mapping Inheritance Putting it all together

Putting it all together ccn oid placed_on pid price qty Customer Order Product name User Update invoice #1234 to paid uid pass Salesman Invoice salary iid paid _on

Putting it all together // Update invoice #1234 to paid EntityManager em = ...; // Instantiate EM Invoice i = em.find(Invoice.class, 1234); i.paid_on = new Date(); // defaults to "now" em.persist(i);

Putting it all together ccn oid placed_on pid price qty Customer Order Product name User Report all orders with unpaid invoices uid pass Salesman Invoice salary iid paid _on

Putting it all together // Report all orders with unpaid invoices EntityManager em = ...; // Instantiate EM List unpaidInvoices = em.createQuery( "SELECT i FROM invoice WHERE i.paid_on IS NULL").getResultList(); for(Invoice i : unpaidInvoices) { Order o = i.getOrder(); Customer c = o.getCustomer(); println("Customer: " + c.full_name); println("Order #: " + String.valueOf(o.order_id)); println("Invoice #: " + String.valueOf(i.invoice_id)); float total = 0.0; for(OrderLine ol : o.getOrderLines()) { total += ol.price * ol.quantity; Product p = ol.getProduct(); println("Item: " + String.valueOf(p.prod_id) + ", " + "Qty: " + String.valueOf(ol.quantity) + ", " + "Price: " + String.valueOf(ol.price)); } println("Total: $" + String.valueOf(total)); println("----------------------------------");

Next Class Disks and Files – Part I