ralf lämmel software languages team university of koblenz...

90
© 2010-16, Software Languages Team Ralf Lämmel, [email protected] O/R/X et al. mapping 1 Ralf Lämmel Software Languages Team University of Koblenz-Landau

Upload: others

Post on 18-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

O/R/X et al. mapping

1

Ralf Lämmel Software Languages Team

University of Koblenz-Landau

Page 2: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 2

Why we need O/R/X mapping … The Bermuda Triangle of data processing

Relations

Objects

XML

In fact, there is yet other islands, e.g., JSON.

Thanks to Erik Meijer for contributing this slide or part thereof.

Page 3: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 3

Different data paradigms (Different technological spaces)

Different type systems (Different schema languages)

Page 4: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 4

Object model for 101companies system

public class Company {private String name;private List<Department> depts = new LinkedList<Department>();public String getName() { return name; }public void setName(String name) { this.name = name; }public List<Department> getDepts() { return depts; }

}

public class Department { ... }

public class Employee { ... }

Page 5: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 5

Aside: EBNF for 101companies system

company : 'company' STRING '{' department* '}' EOF; department : 'department' STRING '{' ('manager' employee) ('employee' employee)* department* '}';

employee : STRING '{' 'address' STRING 'salary' FLOAT '}';

STRING : '"' (~'"')* '"';FLOAT : ('0'..'9')+ ('.' ('0'..'9')+)?;

A short excursion:

concrete syntax

(Another mapping issue)

Page 6: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 6

XSD for 101companies system

<xs:element name="company"><xs:complexType>

<xs:sequence><xs:element ref="name"/><xs:element maxOccurs="unbounded"

minOccurs="0" ref="department"/>

</xs:sequence></xs:complexType>

</xs:element>

<xs:element name="department"> ... </xs:element>

<xs:complexType name="employee"> ... </xs:complexType>

XML too

Grammar-like

Object model-like

Page 7: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 7

Relational schema for 101companies system

CREATE TABLE company (

id INTEGER PRIMARY KEY,

name VARCHAR(100) UNIQUE NOT NULL

)

CREATE TABLE department ( ... )

CREATE TABLE employee ( ... )Observe one detail: companies do not

refer to departments (but vice versa).

Page 8: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 8

What’s O/R/X et al. mapping?

An approach to data access

Given at development time:

An object model OM

A corresponding R or X or et al. schema S

OM may be derived from S or vice versa

Runtime support:

Represent S data as OM objects and vice versa

Page 9: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 9

What’s not O/R/X et al. mapping?

Represent R/X et al. data with concrete data type on O side

e.g., JSON in Python dictionaries

Represent R/X et al. data with abstract data type on O side

e.g., DOM (Document Object Model for XML)

Process R/X et al. data by parsing in O world

e.g., StAX (Pull-based parsing of XML in Java)

Process R/X et al. data by events in O world

e.g., SAX (Push-based parsing of XML in Java)

Page 10: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 10

This lecture: 2 ways of mapping in and out O

O/X mapping (also known as XML data binding)

O/R mapping (related to persistence)

Page 11: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

O/X mapping (aka XML data binding)

11Acknowledgement: Jürgen Starek has significantly contributed to this content.

XML data binding refers to a means of representing information in an XML document as a business object in computer memory. This allows applications to access the data in the XML from the object rather than using the DOM or SAX to

retrieve the data from a direct representation of the XML itself. [Source: http://en.wikipedia.org/wiki/XML_data_binding]

Page 12: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 12

Elevator pitch

12

<…/>

Think of a business programmer who wants to focus on business rules and the object model for the application. Suddenly someone drops the X* word on her and she needs to send and receive messages in a XSD-ruled format. How can we make this person happy again?

Page 13: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 13http://xbrl.org/

Example of an XML language: XBRL--a language for the electronic

communication of business and financial data

Page 14: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 14http://xbrl.org/

Objects, please!

Page 15: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 15

101 in XML

<company> <department deptno="1"> <label>Leadership department</label> <manager> <name>Merlin</name> <salary>9999</salary> </manager> <member> <department deptno="11"> <label>Planning department</label> <manager> <name>Douglas</name> <salary>4200</salary> </manager> <member> <employee> <name>Peter</name> <salary>4241</salary> </employee> </member> </department> </member> . . .

Think of totaling and cutting salaries for all employees.

O/X mapping would allow us to represent XML in

“business objects” and to implement functionality in

an OO manner.

Page 16: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 16

X-to-O type mappingX-to-O

type mapping tool

XML types

OO types

For instance, “xjc” for Technology:JAXBof the Java platform.

Page 17: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

XML schemas (XSD)

17

Page 18: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 18

XSD for 101

<xs:element name="company"><xs:complexType>

<xs:sequence><xs:element ref="name"/><xs:element maxOccurs="unbounded"

minOccurs="0" ref="department"/>

</xs:sequence></xs:complexType>

</xs:element>

<xs:element name="department"> ... </xs:element>

<xs:complexType name="employee"> ... </xs:complexType>

XML too

Grammar-like

OO types-like

Page 19: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 19

XSD – schema components

• Element declarations

• Complex type definitions

• Model-group definitions

• Simple type definitions

• Attribute declarations

• Attribute-group definitions

• Redefinitions

• Annotations Comments and hints for schema processors

Deprecated

Types of leafs in XML trees (both elements and attributes).

Nonrecursive macros without subtyping

Recursive macros with subtyping

Sets of XML trees rooted by a certain element name

Page 20: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 20

Sketch of the company schema

<xs:schema …> <xs:element name="company“> … </xs:element> <xs:element name="department“> … </xs:element> <xs:complexType name="employee"> … </xs:complexType> <xs:element name="name"> … </xs:element> <xs:element name="address"> … </xs:element> <xs:element name="salary"> … </xs:element>

</xs:schema>

Exercise: once you have seen the entire schema and completed this lecture, try to answer the following question: Why does it make (some) sense that both element declarations and complex-type definitions are put to work in the sample schema?

Page 21: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 21

Model group compositors

• <sequence> juxtaposition in EBNF • <choice> “|” in EBNF • <all> “||” (permutation phrases) • minOccurs=”0” ? • minOccurs=“1” maxOccurs=“unbounded” + • minOccurs=“0” maxOccurs=“unbounded” *

Page 22: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 22

<sequence><xs:complexType name="employee"> <xs:sequence> <xs:element ref="name" /> <xs:element ref="address" /> <xs:element ref="salary" /> </xs:sequence> </xs:complexType>

An employee element has children for name, address, and salary.

Page 23: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 23

<choice><xs:element name="subunit"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee" /> <xs:element ref="department" /> </xs:choice> </xs:complexType> </xs:element>

In a variation of our preferred schema, a subunit (of a department) is either an

employee or a department.

Page 24: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 24

“*”<xs:element name="company">

<xs:complexType><xs:sequence>

<xs:element ref="name"/><xs:element maxOccurs="unbounded"

minOccurs="0" ref="department"/>

</xs:sequence></xs:complexType>

</xs:element> A company element has any number of

department elements as its children.

Page 25: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 25

Global vs. local

<xs:choice> <xs:element name="employee" type="employee" /> <xs:element ref="department" /> </xs:choice>

Reference to a global element declaration

Declaration of a local element declaration

Page 26: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 26

XSD simple types• Comparable to primitive types in Java. • Example: <xs:element name="salary" type="xs:double"/> • There are predefined simple types in XSD. • Attributes are of simple types. • New simple types can be defined by:

– Restriction – Union – List

Page 27: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 27

XSD simple type systemSource: http://dret.net/lectures/xml-fall07/

27

Page 28: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

A simple view on Object/XML mapping

(aka XML data binding)

28

Page 29: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 29

O/X type mapping<element name=“point"> <complexType> <sequence> <element name="x" type=“xs:int"/> <element name="y" type="xs:int"/> </sequence> </complexType> </element>

Maps to public class Point { public int x; public int y; }

Maps to

Page 30: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 30

Object model for 101companies system

public class Company {private String name;private List<Department> depts = new LinkedList<Department>();public String getName() { return name; }public void setName(String name) { this.name = name; }public List<Department> getDepts() { return depts; }

}

public class Department { ... }

public class Employee { ... }

Page 31: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 31

XSD for 101companies system

<xs:element name="company"><xs:complexType>

<xs:sequence><xs:element ref="name"/><xs:element maxOccurs="unbounded"

minOccurs="0" ref="department"/>

</xs:sequence></xs:complexType>

</xs:element>

<xs:element name="department"> ... </xs:element>

<xs:complexType name="employee"> ... </xs:complexType>

XML too

Grammar-like

Object model-like

Page 32: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 32

Demo

http://101companies.org/wiki/Contribution:jaxbComposition

For sanity’s sake, let’s look only at Total.java and Cut.java.

Page 33: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 33

Object/XML mapping

Directions for Object/XML mapping

Generate classes from XML schemas.

Generate XML schemas from classes.

Describe mapping only without generation.

Motivations for Object/XML mapping

Support valid XML output.

Hide XML in OO programming.

Use XML-based object serialization.

Page 34: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

Some bits of the O/X impedance mismatch

34

Page 35: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 35

How to map “s = a:x (b:y+ c:z)+ ”?

<xs:element name="s"> <xs:complexType> <xs:sequence> <xs:element name="a" type="x"/> <xs:sequence maxOccurs="unbounded"> <xs:element name="b" type="y" maxOccurs="unbounded"/> <xs:element name="c" type="z"/> </xs:sequence> </xs:sequence> </xs:complexType> </xs:element>`

Page 36: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 36

How to map “s = a:x (b:y+ c:z)+ ”?

Grouping of b’s and c’s is lost.

This may be Ok for read access.

This is not Ok for round-tripping.

Occurrence constraints not enforced:

Mandatory a, b, c

class s { x a; y[] b; z[] c; }

Page 37: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 37

XML data binding is difficult becauseXML are parented trees whereas …

<Add>

<Add> <Const>

element

<Const><Const>

Thanks to Erik Meijer for contributing this slide or part thereof.

Page 38: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 38

Objects are freewheeling graphs.

1

.Value

.right.left

.right

.left

Add

Add Const

Thanks to Erik Meijer for contributing this slide or part thereof.

Page 39: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 39

Its also difficult becauseXML trees are node-labeled whereas …

element

<Add>

<Add> <IConst>1 2

Thanks to Erik Meijer for contributing this slide or part thereof.

Page 40: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 40

Object graphs are edge-labeled.

1

.Value

.right.left

.right

.left

ConstAdd

<Add>

Const

Thanks to Erik Meijer for contributing this slide or part thereof.

Page 41: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

Technology:JAXB

41

Page 42: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 42

Option chosen by JAXB

• POJO with fields and getters/setters. • Annotations define XSD-related properties. • “*” and “+” are mapped to generics. • Uses of heterogeneous (weakly typed) containers:

– Nested composites – Mixed context

• An element factory is provided.

Page 43: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 43

POJOspublic class Company { protected String name; protected List<Department> department; public String getName() { return name; } public void setName(String value) { this.name = value; } public List<Department> getDepartment() { if (department == null) { department = new ArrayList<Department>(); } return this.department; }}

What’s going on here?

Page 44: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 44

Cutting salaries with JAXBpublic class Cut {

public static void cut(Company c) {for (Department d : c.getDepartment())

cut(d);}public static void cut(Department d) {

cut(d.getManager());for (Department s : d.getDepartment())

cut(s);for (Employee e : d.getEmployee())

cut(e);}public static void cut(Employee e) {

e.setSalary(e.getSalary() / 2);}

}

Page 45: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 45

Un-/marshaling

public static Company readCompany(File input)throws JAXBException

{JAXBContext jaxbContext =

JAXBContext.newInstance("org.softlang.company");Unmarshaller unMarshaller =jaxbContext.createUnmarshaller();return (Company)unMarshaller.unmarshal(input);

}

Page 46: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 46

Annotations@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "", propOrder = { "name", "department"})@XmlRootElement(name = "company")public class Company {

@XmlElement(required = true) protected String name; protected List<Department> department; ...}

Page 47: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 47

Liberal mapping

public class Subunit { protected Employee employee; protected Department department; public Employee getEmployee() { return employee; } public void setEmployee(Employee value) { this.employee = value; } public Department getDepartment() { return department; } public void setDepartment(Department value) { this.department = value; }}

<xs:element name="subunit"><xs:complexType>

<xs:choice><xs:element name="employee" type="employee"/><xs:element ref="department"/>

</xs:choice></xs:complexType>

</xs:element>What is (too) liberal here?

Page 48: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 48

XML data binding with JAXB

JAXB is an integral part of the Java SDK since Java 6. Source: http://java.sun.com/developer/technicalArticles/WebServices/jaxb/

Page 49: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 49

Samples on 101

Contribution:jaxbComposition

Contribution:jaxbChoice

Contribution:jaxbExtension

Contribution:jaxbSubstitution

Page 50: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

O/R mapping (related to persistence)

50Acknowledgement: Jürgen Starek has significantly contributed to this content.

Object-relational mapping (OR mapping) products integrate object programming language capabilities with relational databases managed by Oracle, DB2, Sybase, and other RDBMSs. Database objects appear as programming language objects in

one or more existing object programming languages. [Source: http://www.service-architecture.com/articles/object-relational-mapping]

Page 51: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 51

Elevator pitchHow to persist objects in database tables?

How to map object models to relational schemas?

... or the other way around?

Page 52: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 52

Exercises

Bing for this statement and read about it:

“O/R Mapping … is the Vietnam of Computer Science”!

Read about the “O/R impedance mismatch”!

Page 53: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 53

Why is O/R mapping challenging? Objects vs. tables

5353

Object graph of a company

Name ACME CorporationDepartments [o,…]

Name CraigAddress RedmondSalary 123456

Manager WAHR

:EmployeeName Erik

Address UtrechtSalary 12345

Manager FALSCH

:EmployeeName Ralf

Address KoblenzSalary 1234

Manager FALSCH

:Employee

:Company

Name ResearchDepartments […]Employees [o, o, o]

:Department

Page 54: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 54

Why is O/R mapping challenging? Tables vs. objects

5454

Relational tables for a company

Id Name Address Salary Manager Department

1 Craig Redmond 123456 WAHR 42

2 Erik Utrecht 12345 FALSCH 42

3 Ralf Koblenz 1234 FALSCH 42

… … … … … …

Id Name Department Company

42 Research NULL 88

43 Development NULL 88

44 Dev1 43 88

… … … …

Employee

Department

Id Name

88 ACME Corporation

… …

Company

Arrows are inverse when compared to

objects

Page 55: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 55

Why is O/R mapping challenging?

O R

Collections refer to members Rows use foreign key

no NOT NULL NOT NULL

Non-primitive attributes Normal form

In memory with GC Huge data volume

No Transactions Transactions

Page 56: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

Poor men's O/R mapping

56

Page 57: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 57

Employee POJOspublic class Employee {

private int id;private String name;private String address;private double salary;

public int getId() { return id; }public void setId(int id) { this.id = id; }public String getName() { return name; }public void setName(String name) { this.name = name; }public String getAddress() { return address; }public void setAddress(String address) { this.address = address; }public double getSalary() { return salary; }public void setSalary(double salary) { this.salary = salary; }

}

Page 58: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 58

Load employee

int id = employee.getId();

String sql = "SELECT * FROM employee WHERE id = ?";

PreparedStatement stm =

myConnection.getConn().prepareStatement(sql);

stm.setInt(1, id);

ResultSet result = stm.executeQuery();

result.next();

employee.setSalary(result.getDouble("salary"));

employee.setName(result.getString("name"));

employee.setAddress(result.getString("address"));

Page 59: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 59

Change trackingpublic class Employee {

// Some additional / revised members are shown.

private boolean changed;

public void setName(String name) {this.name = name;changed = true;

}

public void setChanged(boolean changed) { this.changed = true; }public boolean isChanged() { return changed; }

}

Page 60: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 60

Save employeeif (employee.isChanged()) {if (employee.getId() == 0) {String sqlInsert = "INSERT employee ...";...stmInsert.execute();

String sqlSelectId = "SELECT max(id) AS maxid FROM employee";...ResultSet maxid = pstmtSelectId.executeQuery();maxid.next();employee.setId(maxid.getInt("maxid"));

} else {String sqlUpdate = "UPDATE employee ...";...stmUpdate.executeUpdate();

}employee.setChanged(false);

}

Is that a robust way of retrieving the id?

This update could fail. What to do?

Page 61: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 61

Loading a company lazilyString sqlDepts = "SELECT id FROM department WHERE did IS NULL " +

"AND cid = (SELECT id FROM company WHERE name = ?);";PreparedStatement stmDepts = myConnection.getConn().prepareStatement(sqlDepts);

stmDepts.setString(1, company.getName());ResultSet deptsR = stmDepts.executeQuery();while (deptsR.next()) {

Department dept = new Department(deptsR.getInt("id"));dept.setObjectFactory(this);dept.setLoaded(false);company.getDepts().add(dept);

}company.setChanged(false);company.getDepts().setUnchanged();

Department objects are populated with

ids only.

Page 62: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 62

101implementation:jdbc2

DEMO

Page 63: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

Java Persistence API (JPA)

63

Page 64: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 64

JPA: Simplifying persistence in Java

Capabilities

Store objects in database tables, one object per row.

Restore objects in different programs / runs.

Page 65: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 65

The JPA architecture

Database

JPAJPA

properties

Application

Persistent Objects

Page 66: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 66

A persistent class

@Entity public class Cat { @Id private String id; private String name; private char sex; private float weight; public String getId() { return id; } private void setId(String id) { this.id = id; } // … other getters and setters … }

Used for the primary key

Mapping with Annotations

Page 67: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 67

A database table

The CAT table in the database

Column | Type | Modifiers --------+-----------------------+----------- id | Number(10) | not null name | Varchar | not null sex | character(1) | weight | Number(10,5) |

PRIMARY KEY (ID)

Page 68: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 68

A JPA session (in Java code)

EntityManagerFactory fac = Persistence.createEntityManagerFactory(…);

EntityManager em = factory.createEntityManager(); em.getTransaction().begin()

Cat princess = new Cat(); princess.setName("Princess"); princess.setSex('F'); princess.setWeight(7.4f);

em.persist(princess); em.getTransaction().commit(); em.close();

Make a n object persistent and

commit changes.

Regular OO code

Set up session and begin transaction

Page 69: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 69

Query query = em.createQuery( "select c from Cat c where c.sex = :sex"); query.setParameter("sex", ‚F'); List<Cat> cats = (List<Cat>) query.getResultList(); for (Cat cat : cats) { out.println("Female Cat: " + cat.getName() ); }

How to retrieve persistent objects? Use JPQL (Java Persistence query language).

Page 70: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 70

So what’s O/R mapping?Wikipedia’s definition

Ralf’s definition (relatively naïve version): • Category 1:

– Start from (idiomatic) classes. – Map object model to relational schema. – Deploy relational schema in database. – Encode CRUD in OO code. – Add transactions to OO code.

• Category 2: – Start from database (schema, instance, SPROC). – Derive object model to encapsulate data access. – Continue as above ...

• Category 1 + 2: classes and tables given, mapping wanted. • Category 2’:

– Like Category 2 but ... – ER/relational model-level mapping. – Coverage of distributed database and data integration.

Page 71: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

https://101wiki.softlang.org/Contribution:jpa

71

Page 72: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

The usual features

Object model / Mapping files

JPA configuration

Data dir to be used by SQLITE

Page 73: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 73

Employee Mapping File@Entitypublic class Employee {

@Id@GeneratedValue(strategy = GenerationType.AUTO)private long id;private String name;private String address;private double salary;

public long getId() {return id;

...public void cut() {...}

}

Maps to Table: EMPLOYEE

Maps to columns: ID,NAME,

ADDRESS,SALARY

Getter and Setter

more methods

Page 74: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 74

Department Mapping File

@Entitypublic class Department {

@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;

private String name;

@OneToOne(cascade=CascadeType.ALL)private Employee manager;

@OneToMany(cascade=CascadeType.ALL)private List<Department> subdepts = new LinkedList<Department>();

@OneToMany(cascade=CascadeType.ALL)private List<Employee> employees = new LinkedList<Employee>();

...

}

Maps to Table: DEPARTMENT

Maps to columns: ID,NAME

@OneToOne maps to column: MANAGER_ID

(Foreign Key EMPLOYEE(ID)

@OneToMany maps to a TABLE: DEPARTMENT_EMPLOYEE

with columns (Foreign Keys): DEPARTMENT_ID, EMPLOYEES_ID

Page 75: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 75

Company Mapping File

@Entitypublic class Company {

@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;

private String name;

@OneToMany(cascade=CascadeType.ALL)private List<Department> depts = new LinkedList<Department>();

...

}

Maps to Table: COMPANY

Maps to columns: ID,NAME

@OneToMany maps to a TABLE: COMPANY_DEPARTMENT

with columns (Foreign Keys): DCOMPANY_ID, DEPTS_ID

some properties

Page 76: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 76

JPA configuration persistence.xml

Mapping filesName are used by EntityManagerFactory

Database settings

JPA settings

Page 77: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 77

https://101wiki.softlang.org/Contribution:jpa

DEMO

Page 78: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 78

Developer’s view on using JPA

The content on this slide

is covered “in passing”

in the lecture.

Page 79: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 79

Developer’s view on using JPA

1.Link JPA libraries2.Configure database3.Create Entities (Object model)4.Write CRUD code

Page 80: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 80

Developer’s view on using JPA

1.Link JPA libraries2.Configure database3.Create Entities (Object model)4.Write CRUD code

Page 81: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 81

Link JPA Libraries

e.g., SQLite l sqlite-jdbc-3.16.1.jar

Required for JPA

https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink/2.6.4

Tip: create a lib dir within your project and place all those jars over there. Then, make sure your project’s build path references the jars.

l eclipselink-2.6.4.jar l javax.persistence-2.1.1.jar l …

The simplest way:Maven Dependencies

Required for DBspecific DB Driver

https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink/2.6.4

Page 82: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 82

Developer’s view on using JPA

1.Link JPA libraries2.Configure database3.Create Entities (Object model)4.Write CRUD code

Page 83: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 83

JPA relies on a RDBMS

Simple option

Use SQLite

sqlite-jdbc-3.16.1.jar

More flexible option

Use any other SQL database via JDBC

Page 84: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 84

Using SQLite

2. Create only folder

EntityManagerFactory emFactory = javax.persistence.Persistence.createEntityManagerFactory("jpa");

EntityManager em = emFactory.createEntityManager();

3. create entry in persistence.xml

1. Add library

4. Use in Code

Page 85: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 85

Developer’s view on using JPA

1.Link JPA libraries2.Configure database3.Create Entities (Object model)4.Write CRUD code

Page 86: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 86

Create Entities

Use POJOsUse Annotations for Mapping (@Entity, @Id,…)

Provide a default constructor

Model an id field as primary key

Add Getter and Setter for all attributes

Optional add more methods

Page 87: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected] 87

Developer’s view on using JPA

1.Link JPA libraries2.Configure database3.Create Entities (Object model)4.Write CRUD code

Page 88: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

Perhaps, don’t use JPA or any „explicit“ O/R mapping. Rather, leverage „implicit“ O/R mapping as integrated, for example, with popular web-application frameworks

such as Django and Rails (but this also comes with unique challenges).

88

Page 89: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

What we should also cover eventually …

Object/JSON mapping

„Implicit“ Object/Relational mapping

89Acknowledgement: Jürgen Starek has significantly contributed to this content.

Page 90: Ralf Lämmel Software Languages Team University of Koblenz ...softlang/pttcourse/slides/mapping… · • Model-group definitions • Simple type definitions • Attribute declarations

© 2010-16, Software Languages Team Ralf Lämmel, [email protected]

End of Lecture

90