SlideShare ist ein Scribd-Unternehmen logo
1 von 82
Downloaden Sie, um offline zu lesen
Database
Programming
Prof. Dr. Ralf Lämmel
Universität Koblenz-Landau
Software Languages Team
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Elevator speech
Think of information systems and data processing!
1. How to persist data?
2. How to separate data and functionality?
3. How to deal with a lot of data efficiently?
4. How to implement entity relationships?
XML may serve 1.-2.
Relational databases serve 1.-4.
Exercise: what’s XML specifically good for?
Also: how to
remain an OO
programmer?
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Database programming
(ignoring OO specifics)
1. Model data via entity-relationship (ER) model
2. Map ER model to relational model (tables)
3. Implement relational model via SQL
4. Implement CRUD functionality
akin to classes +
associations
Tables = rows /
columns of cells
Create, Read,
Update, Delete
The Entity/
Relationship model
Just a quick ride;
see your DB course
for details.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Entities
company: name
department: name
employee: name, address, salary, manager
There are only
attributes of
“simple” types.
Just a quick ride;
see your DB course
for details.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Relationships
The company of a department.
The super-department of a sub-department.
The company of an employee.
The department of an employee.
Exercise: figure out
cardinalities for the
listed relationships.
Just a quick ride;
see your DB course
for details.
The relational model
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Relations (tables)
Relation
Vertically: set of tuples (“rows”)
Horizontally: set of columns
Each cell is of some type
Strings
Numbers
Row IDs (numbers again)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Relational schemas
Key terms
Attributes (names)
Attribute domains (types)
Relational schema (attribute-domain pairs)
Instance of relational schema (sets of tuples)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Relational algebra:
compute relations
Projection (narrow down on certain columns)
Selection (narrow down on certain rows)
Join (compose two tables by condition)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
The relational schema
for 101companies
Relational schemas (names only)
company (id, name)
department (id, name, cid, did)
employee (id, name, address, salary, manager, cid, did)
Key constraints:
Primary key (underlined) for identification of tuple
Foreign key (italics) for reference to another table
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Map ER to relations
Every entity becomes a table.
Relationships
1:1 use foreign key
otherwise (mostly) use extra table.
Compare with implementation of UML class diagrams.
We also speak of tables instead of relations.
Just a quick ride;
see your DB course
for details.
SQL DDL
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
CREATE TABLE company (
! id INTEGER,
! name VARCHAR(100)
)
DDL statement for
company
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
CREATE TABLE company (
! id INTEGER PRIMARY KEY,
! name VARCHAR(100) UNIQUE NOT NULL
)
More details
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
CREATE TABLE company (
! id INTEGER AUTO_INCREMENT PRIMARY KEY,
! name VARCHAR(100) UNIQUE NOT NULL
)
More details
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
CREATE TABLE department (
! id INTEGER,
! name VARCHAR(100),
! cid INTEGER,
! did INTEGER,
)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
CREATE TABLE department (
! id INTEGER PRIMARY KEY,
! name VARCHAR(100) NOT NULL,
! cid INTEGER NOT NULL,
! did INTEGER,
! FOREIGN KEY (cid) REFERENCES company(id),
! FOREIGN KEY (did) REFERENCES department(id)
)
More details
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
CREATE TABLE department (
! id INTEGER PRIMARY KEY,
! name VARCHAR(100) UNIQUE NOT NULL,
! cid INTEGER NOT NULL,
! did INTEGER,
! FOREIGN KEY (cid) REFERENCES company(id)
! ON DELETE CASCADE ON UPDATE CASCADE,
! FOREIGN KEY (did) REFERENCES department(id)
! ON DELETE CASCADE ON UPDATE CASCADE
)
More details
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
CREATE TABLE employee (
! id INTEGER PRIMARY KEY,
! name VARCHAR(50) NOT NULL,
! address VARCHAR(50) NOT NULL,
! salary DOUBLE NOT NULL,
	 manager BOOL NOT NULL,
! cid INTEGER NOT NULL,
! did INTEGER NOT NULL,
! FOREIGN KEY (cid) REFERENCES company(id),
! FOREIGN KEY (did) REFERENCES department(id)
)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
DDL language summary
CREATE TABLE
NOT NULL
PRIMARY / FOREIGN KEY
ON DELETE / UPDATE CASCADE
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Database programming with SQL
(Structured Query Language)
Represent schema in DDL subset of SQL
DDL - Data Definition Language
Part of SQL for data definition
Represent population in DML subset of SQL
DML - Data Manipulation Language
Part of SQL for CRUD (Create, Read, Update, Delete)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
DEMO
http://101companies.org/wiki/
Contribution:mySqlMany
We use a local database server and SQL
monitor; see the online documentation
for the contribution.
SQL DML
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
CRUD
C: Create (SQL: Insert)
R: Read (SQL: Select)
U: Update
D: Delete
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
INSERT INTO company (name)
VALUES ("Acme Corporation")
Insert a new company into
the corresponding table.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
INSERT INTO department (name,cid)
VALUES ("Research",1)
INSERT INTO department (name,cid)
VALUES ("Development",1)
...
Insert several departments
into the corresponding table.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
SELECT * FROM DEPARTMENT
id,name,cid,did
1,Research,1,NULL
2,Development,1,NULL
3,Dev1,1,2
4,Dev1.1,1,3
List of tuples of the
department table.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
SELECT SUM(salary) FROM employee
Select all employees, project to
their salaries, and sum them up.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
SELECT SUM(salary) FROM employee
WHERE cid = 1
Retrieve only salaries of a
specific company.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
SELECT SUM(salary) FROM employee
WHERE cid =
(SELECT id FROM company
WHERE name = "Acme Corporation")
Use a nested query to
determine the company id.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
UPDATE employee
SET salary = salary / 2
Cut all salaries in half.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
UPDATE employee
SET salary = salary / 2
WHERE cid = 1
Limit update to employees
with company id = 1.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
UPDATE employee
SET salary = salary / 2
WHERE cid =
(SELECT id FROM company
WHERE name = "Acme Corporation")
Use a nested query to
determine the company id.
Embedding SQL
with JDBC
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Total all salaries
	 public static double total(MyConnection myConnection, String companyName){
	 	 double total = 0;
	 	 try {
	 	 String query = "SELECT salary FROM employee "
	 	 	 	 + "WHERE cid = (SELECT id FROM company WHERE name = ?);";
	 	 PreparedStatement pstmtEmployees = myConnection.getConn()
	 	 	 	 .prepareStatement(query);
	 	 pstmtEmployees.setString(1, companyName);
	 	 ResultSet salaries = pstmtEmployees.executeQuery();
	 	 while (salaries.next())
	 	 	 total += salaries.getDouble("salary");
	 	 } catch (SQLException e){
	 	 	 e.printStackTrace();
	 	 }
	 	 return total;
	 }
We do not use SQL’s
SUM here so that we
can demonstrate JDBC’s
ResultSets with the
example.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Library support
for database programming
JDBC (part of Java Core API)
Connections to databases
Submit SQL statements
Retrieve results
MySQL connector (part of demo project)
JDBC-based driver for MySQL
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Embedded SQL
Important JDBC types
Connection
Statement (different forms thereof)
ResultSet (for queries in particular)
SQL Exception
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Cut all salaries in half
	 public static void cut(MyConnection myConnection, String companyName) {
	 	 try {
	 	 	 // cut salaries in all employee columns
	 	 	 String sqlCut = "UPDATE employee SET salary = salary / 2 "
	 	 	 	 	 + "WHERE cid = (SELECT id FROM company WHERE name = ?);";
	 	 	 PreparedStatement pstmtEmployees = myConnection.getConn()
	 	 	 	 	 .prepareStatement(sqlCut);
	 	 	 pstmtEmployees.setString(1, companyName);
	 	 	 pstmtEmployees.executeUpdate();
	 	 } catch (SQLException e) {
	 	 	 e.printStackTrace();
	 	 }
	 }
Exercise: understand the
notion of injection attacks
and argue how “prepared
statements” help avoiding
the problem.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
DEMO
http://101companies.org/wiki/
Contribution:jdbc
Object/Relational
Mapping
We travel between the O and R spaces here.
We traveled between O and X spaces before.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Context: X/O/R mapping
The Bermuda Triangle of data processing
Relations
Objects
XML
In fact, there are further technical spaces.
Thanks to Erik Meijer for contributing this slide or part thereof.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
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 { ... }
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
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>
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Relational schema (SQL DDL)
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).
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Elevator pitch
How to persist objects in database tables?
How to map object models to relational schemas?
... or the other way around?
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
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”!
Hibernate
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Hibernate:
Simplifying persistence in Java
Capabilities
Store objects in database tables, one object per row.
Restore objects in different programs / runs.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
The Hibernate architecture
Source: Hibernate Reference Documentation
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
A persistent class
public class Cat {
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
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Metadata for O/R mapping
<hibernate-mapping>
<class name=“Cat" table="CAT“>
<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name“>
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
</class>
</hibernate-mapping>
Map Java
String type to
SQL type
with length
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
A database table
The CAT table in the database
Column | Type | Modifiers
--------+-----------------------+-----------
cat_id | character(32) | not null
name | character varying(16) | not null
sex | character(1) |
weight | real |
Indexes: cat_pkey primary key btree (cat_id)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
A Hibernate session
(in Java code)
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
Cat princess = new Cat();
princess.setName("Princess");
princess.setSex('F');
princess.setWeight(7.4f);
session.save(princess);
tx.commit();
HibernateUtil.closeSession();
Make a n object
persistent and
commit changes.
Regular OO code
Set up session
and begin
transaction
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Query query = session.createQuery(
"select c from Cat as c where c.sex = :sex");
query.setCharacter("sex", 'F');
for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
out.println("Female Cat: " + cat.getName() );
}
How to retrieve persistent objects?
Use HQL (Hibernate query language).
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
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.
http://101companies.org/wiki/
Contribution:hibernate
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Hibernate configuration
See Makefile for usage scenario
Data dir to be used by HSQLDB
Hibernate-enabled object model
with mapping files
SQL scripts with relational schema
and instance data.
The usual features.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Employee POJOs
public class Employee {
	 private long id;
	 private String name;
	 private String address;
	 private double salary;
	 private boolean manager;
	 public long getId() {
	 	 return id;
	 }
	 @SuppressWarnings("unused")
	 private void setId(long id) {
	 	 this.id = id;
	 }
...
}
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Mapping for employees
<hibernate-mapping>
<class name="org.softlang.company.Employee" table="EMPLOYEE">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="name" />
<property name="address" />
<property name="salary" />
<property name="manager" />
</class>
</hibernate-mapping>
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Mapping for departments
<hibernate-mapping>
<class name="org.softlang.company.Department" table="DEPARTMENT">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="name" />
<set name="employees" cascade="all">
<key column="DEPT_ID" />
<one-to-many class="org.softlang.company.Employee" />
</set>
<set name="subdepts" cascade="all">
<key column="DEPT_ID" />
<one-to-many class="org.softlang.company.Department" />
</set>
</class>
</hibernate-mapping>
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Hibernate configuration
<hibernate-configuration>
<session-factory>
<!-- Database connection settings. -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Create the database schema, if needed; update otherwise -->
<property name="hbm2ddl.auto">update</property>
<!-- Mapping files in the project -->
<mapping resource="org/softlang/company/Company.hbm.xml" />
<mapping resource="org/softlang/company/Department.hbm.xml" />
<mapping resource="org/softlang/company/Employee.hbm.xml" />
...
</session-factory>
</hibernate-configuration>
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
DEMO
http://101companies.org/wiki/
Contribution:hibernate
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
After starting DB and GUI
Non-default
selection
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
After JUnit test Load and Refresh
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
After executing PopulateTables.sql
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Running a simple SQL query
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Summary
Use OOP as the programming paradigm.
Use relational DBs as the data management paradigm.
OO programs can use embedded SQL for database access.
SQL may be represented as strings.
Query results may be as weakly typed collections.
Object models may be mapped to relational schemas.
Relational data may be loaded into objects.
Object changes may be saved back to database.
Beware of the O/R impedance mismatch.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Hibernate topics not covered
 Use annotations instead of mapping files
 R-to-O
 Database schema already present
 No need to manually write mapping
 No need to manage constraints
 Use Hibernate’s generator tools
 Object queries
 Use Hibernate’s language for object queries
 EJB-QL
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Developer’s view
on using Hibernate
The content on
this slide
is covered
“in
passing”
in
the lecture.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Developer’s view on using Hibernate
1. Link Hibernate libraries
2. Configure database
3. Hibernate-enable classes
4. Define a mapping
5. Write CRUD code
The content on
this slide
is covered
“in
passing”
in
the lecture.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Developer’s view on using Hibernate
1. Link Hibernate libraries
2. Configure database
3. Hibernate-enable classes
4. Define a mapping
5. Write CRUD code
The content on
this slide
is covered
“in
passing”
in
the lecture.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Download Hibernate
http://www.hibernate.org/
The content on
this slide
is covered
“in
passing”
in
the lecture.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Required for Hibernate 3.2
(+ HSQLDB support)
 antlr-2.7.6.jar
 asm.jar
 asm-attrs.jar
 cglib-2.1.3.jar
 commons-collections-2.1.1.jar
 commons-logging-1.0.4.jar
 dom4j-1.6.1.jar
 ehcache-1.2.3.jar
 jta.jar
 log4j-1.2.11.jar
 hibernate3.jar
 hsqldb.jar
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.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Developer’s view on using Hibernate
1. Link Hibernate libraries
2. Configure database
3. Hibernate-enable classes
4. Define a mapping
5. Write CRUD code
The content on
this slide
is covered
“in
passing”
in
the lecture.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Hibernate relies on a RDBMS
Simple option
Use hsqldb (HyperSQL DB engine)
hsqldb.jar
More flexible option
Use any SQL database via JDBC
The content on
this slide
is covered
“in
passing”
in
the lecture.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Using hsqldb
! Create a “data” subdirectory in your project directory.
! Start the DB engine from within the “data” directory:
java -cp ../lib/hsqldb.jar org.hsqldb.Server
! Keep it running in the background.
! Use database manager to monitor database.
java -cp lib/hsqldb.jar org.hsqldb.util.DatabaseManagerSwing
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Developer’s view on using Hibernate
1. Link Hibernate libraries
2. Configure database
3. Hibernate-enable classes
4. Define a mapping
5. Write CRUD code
The content on
this slide
is covered
“in
passing”
in
the lecture.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Hibernate-enable classes
Use POJOs
Provide a default constructor
Model the following field
private Long id;
+ public getter
+ private setter
The content on
this slide
is covered
“in
passing”
in
the lecture.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Developer’s view on using Hibernate
1. Link Hibernate libraries
2. Configure database
3. Hibernate-enable classes
4. Define a mapping
5. Write CRUD code
The content on
this slide
is covered
“in
passing”
in
the lecture.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
O/R mapping
with Hibernate
Defined in an XML file
MyClass.java
MyClass.hbm.xml
The content on
this slide
is covered
“in
passing”
in
the lecture.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Developer’s view on using Hibernate
1. Link Hibernate libraries
2. Configure database
3. Hibernate-enable classes
4. Define a mapping
5. Write CRUD code
The content on
this slide
is covered
“in
passing”
in
the lecture.

Weitere ähnliche Inhalte

Ähnlich wie Database programming including O/R mapping (as part of the the PTT lecture)

Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)Ralf Laemmel
 
Integrate SparkR with existing R packages to accelerate data science workflows
 Integrate SparkR with existing R packages to accelerate data science workflows Integrate SparkR with existing R packages to accelerate data science workflows
Integrate SparkR with existing R packages to accelerate data science workflowsArtem Ervits
 
Generative programming (mostly parser generation)
Generative programming (mostly parser generation)Generative programming (mostly parser generation)
Generative programming (mostly parser generation)Ralf Laemmel
 
SparkR Best Practices for R Data Scientists
SparkR Best Practices for R Data ScientistsSparkR Best Practices for R Data Scientists
SparkR Best Practices for R Data ScientistsDataWorks Summit
 
SparkR best practices for R data scientist
SparkR best practices for R data scientistSparkR best practices for R data scientist
SparkR best practices for R data scientistDataWorks Summit
 
Spreadsheets: Functional Programming for the Masses
Spreadsheets: Functional Programming for the MassesSpreadsheets: Functional Programming for the Masses
Spreadsheets: Functional Programming for the Masseskfrdbs
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Ralf Laemmel
 
SQL Server - Introduction to TSQL
SQL Server - Introduction to TSQLSQL Server - Introduction to TSQL
SQL Server - Introduction to TSQLPeter Gfader
 
Who killed object oriented design?
Who killed object oriented design?Who killed object oriented design?
Who killed object oriented design?Amir Barylko
 
Porting Oracle Applications to PostgreSQL
Porting Oracle Applications to PostgreSQLPorting Oracle Applications to PostgreSQL
Porting Oracle Applications to PostgreSQLPeter Eisentraut
 
Design Patterns and Form Processing
Design Patterns and Form ProcessingDesign Patterns and Form Processing
Design Patterns and Form ProcessingJaime Metcher
 
Text Mining Infrastructure in R
Text Mining Infrastructure in RText Mining Infrastructure in R
Text Mining Infrastructure in RAshraf Uddin
 
AI-SDV 2022: Accommodating the Deep Learning Revolution by a Development Proc...
AI-SDV 2022: Accommodating the Deep Learning Revolution by a Development Proc...AI-SDV 2022: Accommodating the Deep Learning Revolution by a Development Proc...
AI-SDV 2022: Accommodating the Deep Learning Revolution by a Development Proc...Dr. Haxel Consult
 
200612_BioPackathon_ss
200612_BioPackathon_ss200612_BioPackathon_ss
200612_BioPackathon_ssSatoshi Kume
 
Boulder/Denver BigData: Cluster Computing with Apache Mesos and Cascading
Boulder/Denver BigData: Cluster Computing with Apache Mesos and CascadingBoulder/Denver BigData: Cluster Computing with Apache Mesos and Cascading
Boulder/Denver BigData: Cluster Computing with Apache Mesos and CascadingPaco Nathan
 
Four Languages From Forty Years Ago
Four Languages From Forty Years AgoFour Languages From Forty Years Ago
Four Languages From Forty Years AgoScott Wlaschin
 
Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Ralf Laemmel
 

Ähnlich wie Database programming including O/R mapping (as part of the the PTT lecture) (20)

Flextable and Officer
Flextable and OfficerFlextable and Officer
Flextable and Officer
 
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
 
2 rel-algebra
2 rel-algebra2 rel-algebra
2 rel-algebra
 
Integrate SparkR with existing R packages to accelerate data science workflows
 Integrate SparkR with existing R packages to accelerate data science workflows Integrate SparkR with existing R packages to accelerate data science workflows
Integrate SparkR with existing R packages to accelerate data science workflows
 
Generative programming (mostly parser generation)
Generative programming (mostly parser generation)Generative programming (mostly parser generation)
Generative programming (mostly parser generation)
 
SparkR Best Practices for R Data Scientists
SparkR Best Practices for R Data ScientistsSparkR Best Practices for R Data Scientists
SparkR Best Practices for R Data Scientists
 
SparkR best practices for R data scientist
SparkR best practices for R data scientistSparkR best practices for R data scientist
SparkR best practices for R data scientist
 
Spreadsheets: Functional Programming for the Masses
Spreadsheets: Functional Programming for the MassesSpreadsheets: Functional Programming for the Masses
Spreadsheets: Functional Programming for the Masses
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)
 
SQL Server - Introduction to TSQL
SQL Server - Introduction to TSQLSQL Server - Introduction to TSQL
SQL Server - Introduction to TSQL
 
Who killed object oriented design?
Who killed object oriented design?Who killed object oriented design?
Who killed object oriented design?
 
Porting Oracle Applications to PostgreSQL
Porting Oracle Applications to PostgreSQLPorting Oracle Applications to PostgreSQL
Porting Oracle Applications to PostgreSQL
 
Design Patterns and Form Processing
Design Patterns and Form ProcessingDesign Patterns and Form Processing
Design Patterns and Form Processing
 
Text Mining Infrastructure in R
Text Mining Infrastructure in RText Mining Infrastructure in R
Text Mining Infrastructure in R
 
AI-SDV 2022: Accommodating the Deep Learning Revolution by a Development Proc...
AI-SDV 2022: Accommodating the Deep Learning Revolution by a Development Proc...AI-SDV 2022: Accommodating the Deep Learning Revolution by a Development Proc...
AI-SDV 2022: Accommodating the Deep Learning Revolution by a Development Proc...
 
200612_BioPackathon_ss
200612_BioPackathon_ss200612_BioPackathon_ss
200612_BioPackathon_ss
 
Boulder/Denver BigData: Cluster Computing with Apache Mesos and Cascading
Boulder/Denver BigData: Cluster Computing with Apache Mesos and CascadingBoulder/Denver BigData: Cluster Computing with Apache Mesos and Cascading
Boulder/Denver BigData: Cluster Computing with Apache Mesos and Cascading
 
Four Languages From Forty Years Ago
Four Languages From Forty Years AgoFour Languages From Forty Years Ago
Four Languages From Forty Years Ago
 
Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)
 
Orms
OrmsOrms
Orms
 

Kürzlich hochgeladen

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 

Kürzlich hochgeladen (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

Database programming including O/R mapping (as part of the the PTT lecture)

  • 1. Database Programming Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team
  • 2. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Elevator speech Think of information systems and data processing! 1. How to persist data? 2. How to separate data and functionality? 3. How to deal with a lot of data efficiently? 4. How to implement entity relationships? XML may serve 1.-2. Relational databases serve 1.-4. Exercise: what’s XML specifically good for? Also: how to remain an OO programmer?
  • 3. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Database programming (ignoring OO specifics) 1. Model data via entity-relationship (ER) model 2. Map ER model to relational model (tables) 3. Implement relational model via SQL 4. Implement CRUD functionality akin to classes + associations Tables = rows / columns of cells Create, Read, Update, Delete
  • 4. The Entity/ Relationship model Just a quick ride; see your DB course for details.
  • 5. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Entities company: name department: name employee: name, address, salary, manager There are only attributes of “simple” types. Just a quick ride; see your DB course for details.
  • 6. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Relationships The company of a department. The super-department of a sub-department. The company of an employee. The department of an employee. Exercise: figure out cardinalities for the listed relationships. Just a quick ride; see your DB course for details.
  • 8. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Relations (tables) Relation Vertically: set of tuples (“rows”) Horizontally: set of columns Each cell is of some type Strings Numbers Row IDs (numbers again)
  • 9. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Relational schemas Key terms Attributes (names) Attribute domains (types) Relational schema (attribute-domain pairs) Instance of relational schema (sets of tuples)
  • 10. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Relational algebra: compute relations Projection (narrow down on certain columns) Selection (narrow down on certain rows) Join (compose two tables by condition)
  • 11. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) The relational schema for 101companies Relational schemas (names only) company (id, name) department (id, name, cid, did) employee (id, name, address, salary, manager, cid, did) Key constraints: Primary key (underlined) for identification of tuple Foreign key (italics) for reference to another table
  • 12. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Map ER to relations Every entity becomes a table. Relationships 1:1 use foreign key otherwise (mostly) use extra table. Compare with implementation of UML class diagrams. We also speak of tables instead of relations. Just a quick ride; see your DB course for details.
  • 14. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) CREATE TABLE company ( ! id INTEGER, ! name VARCHAR(100) ) DDL statement for company
  • 15. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) CREATE TABLE company ( ! id INTEGER PRIMARY KEY, ! name VARCHAR(100) UNIQUE NOT NULL ) More details
  • 16. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) CREATE TABLE company ( ! id INTEGER AUTO_INCREMENT PRIMARY KEY, ! name VARCHAR(100) UNIQUE NOT NULL ) More details
  • 17. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) CREATE TABLE department ( ! id INTEGER, ! name VARCHAR(100), ! cid INTEGER, ! did INTEGER, )
  • 18. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) CREATE TABLE department ( ! id INTEGER PRIMARY KEY, ! name VARCHAR(100) NOT NULL, ! cid INTEGER NOT NULL, ! did INTEGER, ! FOREIGN KEY (cid) REFERENCES company(id), ! FOREIGN KEY (did) REFERENCES department(id) ) More details
  • 19. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) CREATE TABLE department ( ! id INTEGER PRIMARY KEY, ! name VARCHAR(100) UNIQUE NOT NULL, ! cid INTEGER NOT NULL, ! did INTEGER, ! FOREIGN KEY (cid) REFERENCES company(id) ! ON DELETE CASCADE ON UPDATE CASCADE, ! FOREIGN KEY (did) REFERENCES department(id) ! ON DELETE CASCADE ON UPDATE CASCADE ) More details
  • 20. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) CREATE TABLE employee ( ! id INTEGER PRIMARY KEY, ! name VARCHAR(50) NOT NULL, ! address VARCHAR(50) NOT NULL, ! salary DOUBLE NOT NULL, manager BOOL NOT NULL, ! cid INTEGER NOT NULL, ! did INTEGER NOT NULL, ! FOREIGN KEY (cid) REFERENCES company(id), ! FOREIGN KEY (did) REFERENCES department(id) )
  • 21. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) DDL language summary CREATE TABLE NOT NULL PRIMARY / FOREIGN KEY ON DELETE / UPDATE CASCADE
  • 22. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Database programming with SQL (Structured Query Language) Represent schema in DDL subset of SQL DDL - Data Definition Language Part of SQL for data definition Represent population in DML subset of SQL DML - Data Manipulation Language Part of SQL for CRUD (Create, Read, Update, Delete)
  • 23. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) DEMO http://101companies.org/wiki/ Contribution:mySqlMany We use a local database server and SQL monitor; see the online documentation for the contribution.
  • 25. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) CRUD C: Create (SQL: Insert) R: Read (SQL: Select) U: Update D: Delete
  • 26. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) INSERT INTO company (name) VALUES ("Acme Corporation") Insert a new company into the corresponding table.
  • 27. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) INSERT INTO department (name,cid) VALUES ("Research",1) INSERT INTO department (name,cid) VALUES ("Development",1) ... Insert several departments into the corresponding table.
  • 28. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) SELECT * FROM DEPARTMENT id,name,cid,did 1,Research,1,NULL 2,Development,1,NULL 3,Dev1,1,2 4,Dev1.1,1,3 List of tuples of the department table.
  • 29. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) SELECT SUM(salary) FROM employee Select all employees, project to their salaries, and sum them up.
  • 30. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) SELECT SUM(salary) FROM employee WHERE cid = 1 Retrieve only salaries of a specific company.
  • 31. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) SELECT SUM(salary) FROM employee WHERE cid = (SELECT id FROM company WHERE name = "Acme Corporation") Use a nested query to determine the company id.
  • 32. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) UPDATE employee SET salary = salary / 2 Cut all salaries in half.
  • 33. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) UPDATE employee SET salary = salary / 2 WHERE cid = 1 Limit update to employees with company id = 1.
  • 34. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) UPDATE employee SET salary = salary / 2 WHERE cid = (SELECT id FROM company WHERE name = "Acme Corporation") Use a nested query to determine the company id.
  • 36. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Total all salaries public static double total(MyConnection myConnection, String companyName){ double total = 0; try { String query = "SELECT salary FROM employee " + "WHERE cid = (SELECT id FROM company WHERE name = ?);"; PreparedStatement pstmtEmployees = myConnection.getConn() .prepareStatement(query); pstmtEmployees.setString(1, companyName); ResultSet salaries = pstmtEmployees.executeQuery(); while (salaries.next()) total += salaries.getDouble("salary"); } catch (SQLException e){ e.printStackTrace(); } return total; } We do not use SQL’s SUM here so that we can demonstrate JDBC’s ResultSets with the example.
  • 37. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Library support for database programming JDBC (part of Java Core API) Connections to databases Submit SQL statements Retrieve results MySQL connector (part of demo project) JDBC-based driver for MySQL
  • 38. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Embedded SQL Important JDBC types Connection Statement (different forms thereof) ResultSet (for queries in particular) SQL Exception
  • 39. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Cut all salaries in half public static void cut(MyConnection myConnection, String companyName) { try { // cut salaries in all employee columns String sqlCut = "UPDATE employee SET salary = salary / 2 " + "WHERE cid = (SELECT id FROM company WHERE name = ?);"; PreparedStatement pstmtEmployees = myConnection.getConn() .prepareStatement(sqlCut); pstmtEmployees.setString(1, companyName); pstmtEmployees.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } Exercise: understand the notion of injection attacks and argue how “prepared statements” help avoiding the problem.
  • 40. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) DEMO http://101companies.org/wiki/ Contribution:jdbc
  • 41. Object/Relational Mapping We travel between the O and R spaces here. We traveled between O and X spaces before.
  • 42. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Context: X/O/R mapping The Bermuda Triangle of data processing Relations Objects XML In fact, there are further technical spaces. Thanks to Erik Meijer for contributing this slide or part thereof.
  • 43. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) 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 { ... }
  • 44. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) 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>
  • 45. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Relational schema (SQL DDL) 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).
  • 46. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Elevator pitch How to persist objects in database tables? How to map object models to relational schemas? ... or the other way around?
  • 47. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) 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”!
  • 49. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Hibernate: Simplifying persistence in Java Capabilities Store objects in database tables, one object per row. Restore objects in different programs / runs.
  • 50. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) The Hibernate architecture Source: Hibernate Reference Documentation
  • 51. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) A persistent class public class Cat { 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
  • 52. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Metadata for O/R mapping <hibernate-mapping> <class name=“Cat" table="CAT“> <id name="id" type="string" unsaved-value="null" > <column name="CAT_ID" sql-type="char(32)" not-null="true"/> <generator class="uuid.hex"/> </id> <property name="name“> <column name="NAME" length="16" not-null="true"/> </property> <property name="sex"/> <property name="weight"/> </class> </hibernate-mapping> Map Java String type to SQL type with length
  • 53. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) A database table The CAT table in the database Column | Type | Modifiers --------+-----------------------+----------- cat_id | character(32) | not null name | character varying(16) | not null sex | character(1) | weight | real | Indexes: cat_pkey primary key btree (cat_id)
  • 54. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) A Hibernate session (in Java code) Session session = HibernateUtil.currentSession(); Transaction tx= session.beginTransaction(); Cat princess = new Cat(); princess.setName("Princess"); princess.setSex('F'); princess.setWeight(7.4f); session.save(princess); tx.commit(); HibernateUtil.closeSession(); Make a n object persistent and commit changes. Regular OO code Set up session and begin transaction
  • 55. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Query query = session.createQuery( "select c from Cat as c where c.sex = :sex"); query.setCharacter("sex", 'F'); for (Iterator it = query.iterate(); it.hasNext();) { Cat cat = (Cat) it.next(); out.println("Female Cat: " + cat.getName() ); } How to retrieve persistent objects? Use HQL (Hibernate query language).
  • 56. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) 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.
  • 58. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Hibernate configuration See Makefile for usage scenario Data dir to be used by HSQLDB Hibernate-enabled object model with mapping files SQL scripts with relational schema and instance data. The usual features.
  • 59. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Employee POJOs public class Employee { private long id; private String name; private String address; private double salary; private boolean manager; public long getId() { return id; } @SuppressWarnings("unused") private void setId(long id) { this.id = id; } ... }
  • 60. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Mapping for employees <hibernate-mapping> <class name="org.softlang.company.Employee" table="EMPLOYEE"> <id name="id" column="ID"> <generator class="native" /> </id> <property name="name" /> <property name="address" /> <property name="salary" /> <property name="manager" /> </class> </hibernate-mapping>
  • 61. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Mapping for departments <hibernate-mapping> <class name="org.softlang.company.Department" table="DEPARTMENT"> <id name="id" column="ID"> <generator class="native" /> </id> <property name="name" /> <set name="employees" cascade="all"> <key column="DEPT_ID" /> <one-to-many class="org.softlang.company.Employee" /> </set> <set name="subdepts" cascade="all"> <key column="DEPT_ID" /> <one-to-many class="org.softlang.company.Department" /> </set> </class> </hibernate-mapping>
  • 62. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Hibernate configuration <hibernate-configuration> <session-factory> <!-- Database connection settings. --> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:hsql://localhost</property> <property name="connection.username">sa</property> <property name="connection.password"></property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.HSQLDialect</property> <!-- Create the database schema, if needed; update otherwise --> <property name="hbm2ddl.auto">update</property> <!-- Mapping files in the project --> <mapping resource="org/softlang/company/Company.hbm.xml" /> <mapping resource="org/softlang/company/Department.hbm.xml" /> <mapping resource="org/softlang/company/Employee.hbm.xml" /> ... </session-factory> </hibernate-configuration>
  • 63. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) DEMO http://101companies.org/wiki/ Contribution:hibernate
  • 64. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) After starting DB and GUI Non-default selection
  • 65. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) After JUnit test Load and Refresh
  • 66. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) After executing PopulateTables.sql
  • 67. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Running a simple SQL query
  • 68. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Summary Use OOP as the programming paradigm. Use relational DBs as the data management paradigm. OO programs can use embedded SQL for database access. SQL may be represented as strings. Query results may be as weakly typed collections. Object models may be mapped to relational schemas. Relational data may be loaded into objects. Object changes may be saved back to database. Beware of the O/R impedance mismatch.
  • 69. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Hibernate topics not covered  Use annotations instead of mapping files  R-to-O  Database schema already present  No need to manually write mapping  No need to manage constraints  Use Hibernate’s generator tools  Object queries  Use Hibernate’s language for object queries  EJB-QL
  • 70. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Developer’s view on using Hibernate The content on this slide is covered “in passing” in the lecture.
  • 71. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Developer’s view on using Hibernate 1. Link Hibernate libraries 2. Configure database 3. Hibernate-enable classes 4. Define a mapping 5. Write CRUD code The content on this slide is covered “in passing” in the lecture.
  • 72. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Developer’s view on using Hibernate 1. Link Hibernate libraries 2. Configure database 3. Hibernate-enable classes 4. Define a mapping 5. Write CRUD code The content on this slide is covered “in passing” in the lecture.
  • 73. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Download Hibernate http://www.hibernate.org/ The content on this slide is covered “in passing” in the lecture.
  • 74. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Required for Hibernate 3.2 (+ HSQLDB support)  antlr-2.7.6.jar  asm.jar  asm-attrs.jar  cglib-2.1.3.jar  commons-collections-2.1.1.jar  commons-logging-1.0.4.jar  dom4j-1.6.1.jar  ehcache-1.2.3.jar  jta.jar  log4j-1.2.11.jar  hibernate3.jar  hsqldb.jar 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.
  • 75. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Developer’s view on using Hibernate 1. Link Hibernate libraries 2. Configure database 3. Hibernate-enable classes 4. Define a mapping 5. Write CRUD code The content on this slide is covered “in passing” in the lecture.
  • 76. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Hibernate relies on a RDBMS Simple option Use hsqldb (HyperSQL DB engine) hsqldb.jar More flexible option Use any SQL database via JDBC The content on this slide is covered “in passing” in the lecture.
  • 77. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Using hsqldb ! Create a “data” subdirectory in your project directory. ! Start the DB engine from within the “data” directory: java -cp ../lib/hsqldb.jar org.hsqldb.Server ! Keep it running in the background. ! Use database manager to monitor database. java -cp lib/hsqldb.jar org.hsqldb.util.DatabaseManagerSwing
  • 78. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Developer’s view on using Hibernate 1. Link Hibernate libraries 2. Configure database 3. Hibernate-enable classes 4. Define a mapping 5. Write CRUD code The content on this slide is covered “in passing” in the lecture.
  • 79. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Hibernate-enable classes Use POJOs Provide a default constructor Model the following field private Long id; + public getter + private setter The content on this slide is covered “in passing” in the lecture.
  • 80. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Developer’s view on using Hibernate 1. Link Hibernate libraries 2. Configure database 3. Hibernate-enable classes 4. Define a mapping 5. Write CRUD code The content on this slide is covered “in passing” in the lecture.
  • 81. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) O/R mapping with Hibernate Defined in an XML file MyClass.java MyClass.hbm.xml The content on this slide is covered “in passing” in the lecture.
  • 82. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Developer’s view on using Hibernate 1. Link Hibernate libraries 2. Configure database 3. Hibernate-enable classes 4. Define a mapping 5. Write CRUD code The content on this slide is covered “in passing” in the lecture.