Middleware for Heterogeneous and Distributed Information Systems Sample Solution Exercise Sheet 5

Similar documents
Java EE Architecture, Part Three. Java EE architecture, part three 1(57)

foreword to the first edition preface xxi acknowledgments xxiii about this book xxv about the cover illustration

Java EE Architecture, Part Three. Java EE architecture, part three 1(69)

Chapter 6 Object Persistence, Relationships and Queries

Middleware for Heterogeneous and Distributed Information Systems Exercise Sheet 1

object/relational persistence What is persistence? 5

Chapter 6 Object Persistence, Relationships and Queries

Chapter 6 Object Persistence, Relationships and Queries

Enterprise JavaBeans, Version 3 (EJB3) Programming

Chapter 6 Object Persistence, Relationships and Queries. Lifetime of a persistent object may exceed the execution of individual applications

Pro JPA 2. Mastering the Java Persistence API. Apress* Mike Keith and Merrick Schnicariol

Fast Track to EJB 3.0 and the JPA Using JBoss

Chapter 6 Object Persistence, Relationships and Queries

Chapter 11 - Data Replication Middleware

5/2/2017. The entity manager. The entity manager. Entities lifecycle and manipulation

CO Java EE 6: Develop Database Applications with JPA

Chapter 2: Entity-Relationship Model

find() method, 178 forclass() method, 162 getcurrentsession(), 16 getexecutablecriteria() method, 162 get() method, 17, 177 getreference() method, 178

Chapter 6: Entity-Relationship Model

"Charting the Course... Mastering EJB 3.0 Applications. Course Summary

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Java EE Architecture, Part Three. Java EE architecture, part three 1(24)

Roadmap of This Lecture. Weak Entity Sets Extended E-R Features Reduction to Relation Schemas Database Design UML*

Chapter 9: Relational DB Design byer/eer to Relational Mapping Relational Database Design Using ER-to- Relational Mapping Mapping EER Model

Lightweight J2EE Framework

Chapter 7: Entity-Relationship Model

Chapter 7: Entity-Relationship Model

Unit I. By Prof.Sushila Aghav MIT

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Enterprise JavaBeans 3.1

Essay Question: Explain 4 different means by which constrains are represented in the Conceptual Data Model (CDM).

Chapter 7: Entity-Relationship Model

Database Design. IIO30100 Tietokantojen suunnittelu. Michal Zabovsky. Presentation overview

Chapter 11 Object and Object- Relational Databases

Chapter 6: Entity-Relationship Model. The Next Step: Designing DB Schema. Identifying Entities and their Attributes. The E-R Model.

The Next Step: Designing DB Schema. Chapter 6: Entity-Relationship Model. The E-R Model. Identifying Entities and their Attributes.

Database Management

Object Persistence Design Guidelines

Understanding Impact of J2EE Applications On Relational Databases. Dennis Leung, VP Development Oracle9iAS TopLink Oracle Corporation

Chapter 11: Data Management Layer Design

Chapter 1 SQL and Data

CS211 Lecture: Database Design

Dali Java Persistence Tools. User Guide Release for Eclipse


Web Application Development Using Spring, Hibernate and JPA

Tutorial notes on. Object relational structural patterns

A database can be modeled as: + a collection of entities, + a set of relationships among entities.

Ch. 21: Object Oriented Databases

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

Chapter 6: Entity-Relationship Model

Solved MCQ on fundamental of DBMS. Set-1

Glossary. abort. application schema

A7-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

Object-Relational Mapping Tools let s talk to each other!

Chapter 9: Object-Based Databases

DATABASE MANAGEMENT SYSTEM

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 6: Entity-Relationship Model. E-R Diagrams

Chapter 7: Entity-Relationship Model

Web Application Development Using Spring, Hibernate and JPA

Chapter 7: Entity-Relationship Model. Chapter 7: Entity-Relationship Model

Web Application Development Using Spring, Hibernate and JPA

Index. setmaxresults() method, 169 sorting, 170 SQL DISTINCT query, 171 uniqueresult() method, 169

EJB 3 Entity Relationships

The Entity-Relationship Model (ER Model) - Part 2

Efficient Object-Relational Mapping for JAVA and J2EE Applications or the impact of J2EE on RDB. Marc Stampfli Oracle Software (Switzerland) Ltd.

Chapter 2 Introduction to Relational Models

Informatics 1: Data & Analysis

OBJECTIVES. How to derive a set of relations from a conceptual data model. How to validate these relations using the technique of normalization.

Middleware for Heterogeneous and Distributed Information Systems Exercise Sheet 8

The Relational Model

3. Object-Oriented Databases

EJB 3 Entities. Course Multi Tier Business Applications with Java EE. Prof. Dr. Eric Dubuis Berner Fachhochschule Biel. Berner Fachhochschule

Dali Java Persistence Tools. User Guide Release 2.0 for Eclipse

High Level Database Models

What data persistence means? We manipulate data (represented as object state) that need to be stored

Developing Applications with Java EE 6 on WebLogic Server 12c

Generation of EJB3 Artifacts in a Modeling Platform

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

O/R mapping with Hibernate

TopLink Grid: Scaling JPA applications with Coherence

Chapter 12 Object and Object Relational Databases

Data about data is database Select correct option: True False Partially True None of the Above

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

Project # 1: Database Programming

(ADVANCED) DATABASE SYSTEMS (DATABASE MANAGEMENTS) PROF. DR. HASAN HÜSEYİN BALIK (6 TH WEEK)

Chapter 6: Entity-Relationship Model

Overview of Database Design Process Example Database Application (COMPANY) ER Model Concepts

Systems Analysis and Design in a Changing World, Fourth Edition. Chapter 12: Designing Databases

Entity-Relationship Models: Good Design and Constraints

Database Applications (15-415)

6232B: Implementing a Microsoft SQL Server 2008 R2 Database

MIDTERM EXAMINATION Spring 2010 CS403- Database Management Systems (Session - 4) Ref No: Time: 60 min Marks: 38

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

Instructor: Craig Duckett. Lecture 11: Thursday, May 3 th, Set Operations, Subqueries, Views

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions.

Introduction to Data Management. Lecture #6 E-Rà Relational Mapping (Cont.)

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

Transcription:

AG Heterogene Informationssysteme Prof. Dr.-Ing. Stefan Deßloch Fachbereich Informatik Technische Universität Kaiserslautern Middleware for Heterogeneous and Distributed Information Systems Sample Solution Exercise Sheet 5 Tuesday, November, 2016 13:45 to 15:15 Room 13-222 Object-Relational Mapping A persistence service is used for enterprise applications (e.g. written in Java) to handle persistent object data for long-term data management. In this exercise, we will first look at how general object-relational mapping is handled in this scenario and what key aspects of mapping are mainly focused. 1. Before we start to examine different aspects covered in object-relational mapping, let's first clarify the differences between two worlds, i.e. what is so-called object/relational impedance match? 2. When providing persistence in Java, three design principles/aspects of persistence needed to be considered. Explain these aspects of persistence 1. Orthogonal persistence: a) persistence independent of data type, class b) instances of the same class may be transient or persistent. Transitive persistence: (aka persistence by reachability) a) objects can be explicitly designated to become persistent (i.e., roots) b) object referenced by persistent objects automatically become persistent, too Persistence independence: (aka transparent persistence) 1 http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.5.2318&rep=rep1&type=pdf Middleware for Heterogeneous and Distributed Information Systems 1

a) code operating on transient and persistent objects is (almost) the same b) "client object" side: no impact when interacting with persistent objects i. application may have to explicitly "persist" an object, but continues to use the same interface for interacting with the persistent object ii. interactions with a data store are not visible to/initiated by the client object, but happen automatically (e.g., when object state is modified or at EOT) a) "persistent object" side: no special coding for "implementing" persistence 3. How is a single, primitive Java object (object class, object reference, instance of class, data types) mapped to its relational format in general? How does an entity mapping to RDBMS in JPA look like and which system components are involved? Explain whether JPA supports orthogonal persistence. Object class -> a single table or a set of multiple tables to support inheritance/complex field values Object reference -> foreign key constraint Entity instance/object -> one row in a table with a unique identifier Data type mapping In JP entities are annotated by @Entity. An entity must have primary key which is represented by annotation @Id. By default, field variable name is used as mapped column name whereas the annotation @Column can be used to explicitly set the mapped column name. Relationships between parent objects and containing objects are explained in the following. 4. In the Entity-Relation Model 2, there is a concept called weak entity. Explain how are weak entities supported in JPA and how are they handled when mapped to RDMBS. Embeddable class is used to support weak entities in JPA. Instances of an embeddable class are seen as embedded objects and do not have a persistent identity. Single-valued/collection-valued embedded objects are mapped to the same table as the containing entity, or to a collection table, respectively. 5. Inheritance is a key feature used in object-oriented (OO) programming model. Taking a login system example shown below, explain those three approaches introduced in the lecture which support mapping OO-inheritance to RDMBS. Furthermore, please discuss their advantages/disadvantages. Single table with discriminator column (InheritanceType.SINGLE_TABLE): stores columns for all attributes of any class in the hierarchy with a special discriminator column identifying the class within the hierarchy. +: avoid redundancy; each entity is stored as one tuple. 2 https://en.wikipedia.org/wiki/entity-relationship_model Middleware for Heterogeneous and Distributed Information Systems 2

+: avoid union and join operation at runtime to retrieve instances from super- and subclass tables -: increased storage cost (discriminator column, null values for sub-class specific columns) Vertical partitioning (InheritanceType.JOINED): one table per entity class with columns for newly defined attributes specific to the sub-class plus ID column. +: reduced storage cost -: increased update and search cost -: high runtime latency due to instance construction using join-operation on id column. Horizontal partitioning (InheritanceType.TABLE_PER_CLASS): one table per entity class, with columns for all attributes including inherited attributes from super-class. +: small storage cost; each entity is stored as one tuple and no additional null values as compared to SINGLE_TABLE approach. -: sub-class instance access can lead to recursive search in sub-classes. -: super-class instance access can lead to union of rows based on overlapping columns from both super- and sub-tables. Figure 1: Sample class hierarchy of login system 6. Relationships between either objects (Java) or relations (RDMBS) are differently supported in both data models. In Java, so-called "networks" of objects are normally used, i.e. an object contains another object using references and objects can be navigationally accessed. In relational data model, relationships are represented as value-based, primary key/foreign key relationships. 6.1. How many relationship types do we have? How many directionalities? How many combinations between relationship types and directionalities do we have? How are these combinations represented at object-oriented level? Explain the roles of owning side and inverse side in each combination. - Relationship types: one-to-one, one-to-many, many-to-one, many-to-many - directionalities: uni-/bidirectional - seven combinations. This is because a bidirectional one-to-many relationship is equivalent to a bidirectional many-to-one relationship. Middleware for Heterogeneous and Distributed Information Systems 3

- they are represented using containing relationship. For instance, many students attend a lecture. Thus, a lecture object may contain a list of student object references. - bi-directional combination has a designated owning side and inverse side; for 1:n and n:1, the "many" side has to be the owning side which determines the state at the persistent data store. Uni-directional relationship only has an owning side. 6.2. Take an example that one person (firstname, lastname) has only one address (city, zipcode). How is this relationship mapped to database schema in all directionality settings in JPA? Mapping is accomplished by using primary key/foreign key relationships. Table for the designated "owning" side, which is Person class, has to contain the foreign key. Person(person_id, firstname, lastname, address_id); Address(address_id, city, zipcode) 6.3. Take the following customer-order relationship as an example. How is this mapped to relational schema? Figure 2: Customer-order relationship This is a bi-directional one-to-many relationship. As defined previously the owning side is the "many" side which is Order class. Furthermore, mapped foreign key should also be covered in "owning" side. Customer(customer_id); Order(order_id, name, customer_id) 6.4. With another order-lineitem relationship as below, how is this relationship again mapped to relational schema? Figure 3: Order-lineitem relationship This is an exceptional case where a uni-directional one-to-many relationship. The owning side is Order class while the inverse side is LineItem class. However, in this case, the foreign key is mapped not onto the "owning" side but onto the "n" side which is LineItem class. Exception: for uni-directional 1:n-relationship, foreign key is on the table for the "n" side. Order(order_id, name); LineItem(item_id, comment, order_id) 6.5. Given an example that one student can attend multiple lectures while one lectured have multiple students, how is this case mapped now? We need an extra join table for this many-to-many relationship which contains a composite key consisting of the primary keys from both student and lecture tables. Student(student_id, name); Lecture(lecture_id, title); Jointable(student_id, lecture_id) Middleware for Heterogeneous and Distributed Information Systems 4

Entity Management In JPA, programmers use annotations to specify the persistence properties so that entity managers can control the persistent state of entities at runtime. 1. Illustrate the life cycle of entity and explain the life cycle state and corresponding transition methods. Figure. Entity life cycle 3 The life of an entity instance has two main aspects: its relationship to a specific persistence context, and the synchronization of its state with the database. The EntityManager distinguishes between four states in the life cycle of an entity: New. The entity instance was created in memory, but is not yet associated with either a persistent identity in the database or a persistence context. This is the state that an entity is in right after its creation using its constructor method. Changes in the entity state are not synchronized with the database at this stage. Managed. The entity has a persistent identity in the database and is currently associated with a persistence context. This entity is in the managed state after the persist() method is called. Changes to the entity will be synchronized with the database when transactions are committed (whenever a business method returns) or when synchronization is explicitly triggered using the flush() operation. Detached. The entity does has a persistent identity but is not or is no longer associated with the persistence context and no synchronization with database will happen. An entity becomes detached when the persistence context ends and can be synchronized with database again by merging it into a new persistence context. Removed. The entity is currently associated with a persistence context but has been scheduled for removal from the database. 2. JPA uses the cascade annotation to support transitive persistence. Please first explain possible "cascade" types in JPA and examine what kind of underlying database facility could be utilized to support "all" or "remove" from cascade types. cascade types: persist, merge, remove, refresh, all 3 Rima Patel Sriganesh, gerald Brose, Micah Silverman: Mastering Enterpris JavaBeans 3.0, Wiley Publishing, 2006 Middleware for Heterogeneous and Distributed Information Systems 5

referential integrity constraints 3. Explain possible loading schemes. How are they supported in JPA? "lazy loading" [Annotation fetch=fetchtype.lazy] - object is retrieved only when accessed based on primary key or reference (relationship); easy to implement; may cause increased communication with data source, resulting in performance drawbacks. "eager loading" [Annotation fetch=fetchtype.eager] - when an object is requested, transitively load all the objects reachable through references; requires construction/generation of complex data store queries; may cause a lot of unnecessary objects to be loaded. 4. How many approaches do we have to lookup entities? Compare their usage cases and explain their JPA support. Student m n n 1 Lecture Professor Figure 4: Lecture example by entity persistent identifier/primary key (look up) - a single entity is returned by calling find(entityclass, primarykey). by query - but in many situations we either don't know the primary key, or we need more than one result, or need to specify one or more search conditions. The method createquery(querystring) returns java.persistence.query object to retrieve sets of results by calling query.getresultlist() subsequently. 5. Consider the abstract persistence schema depicted in 4. Give EJB QL statements for the following queries. a. Find all lectures. SELECT l FROM Lecture l b. Given a lecture name, find all students that attend lectures of that name. SELECT DISTINCT s FROM Student s, IN (s.lecture) AS l WHERE l.title =?1 c. Given a student id, find all professors that teach this student. SELECT DISTINCT p FROM Professor p, IN (p.lecture) AS l, IN (l.student) AS s WHERE s.id =?1 Middleware for Heterogeneous and Distributed Information Systems 6

Transaction and Concurrency control Persistent objects are manipulated in the scope of a transaction while the persistence is supported by relational databases. Now we discuss transaction support in JPA in a scenario where multiple applications/transactions run with persistent data. Figure 5: Modifying persistent state 4 1. What kind of anomaly can occur when an object that was detached is reattached in the same application? How can this problem be compensated? Lost updates anomaly can occur in this case due to concurrent updates on persistent state and it can be controlled by explicit refresh or using optimistic locking. 2. JPA supports optimistic locking with "read committed" as the highest isolation level. What kind of anomalies can still occur? non-repeatable or phantom reads 3. Comment on "possible load schemes from the persistence service might impact transaction processing differently when "read committed" is considered as the transaction isolation level.". A parent entity contains a set of dependent entities and eagerly loaded each time when the parent entity is requested. Suppose a transaction that modifies an attribute (not its dependent entities) occurs concurrently with another transaction that modifies one of the dependent entity. In the eager-loading scheme, once the "parent" transaction wants to commit, the version number of dependent entities differs, resulting in rolling-back of the transaction running on the parent object, which is not expected from user. 4 Rima Patel Sriganesh, gerald Brose, Micah Silverman: Mastering Enterpris JavaBeans 3.0, Wiley Publishing, 2006 Middleware for Heterogeneous and Distributed Information Systems 7