Apache Cayenne Object Relational Mapping

15
a digital commerce consultancy san francisco ~ new york ~ london ~ chişinău ~ guadalajara Apache Cayenne Object Relational Mapping

description

Apache Cayenne Object Relational Mapping. What is ORM?. ORM stands for Object Relational Mapping. It is a technique that creates an object oriented data model on top of a Relational Database. Persistence. - PowerPoint PPT Presentation

Transcript of Apache Cayenne Object Relational Mapping

Page 1: Apache Cayenne Object Relational Mapping

a digital commerce consultancy

san francisco ~ new york ~ london ~ chişinău ~ guadalajara

Apache CayenneObject Relational Mapping

Page 2: Apache Cayenne Object Relational Mapping

What is ORM?

• ORM stands for Object Relational Mapping.

• It is a technique that creates an object oriented data model on top of a Relational Database.

Grupo

id_grupoPK

programa

semestre

Alumno

id_alumnoPK

id_grupoFK

nombre_completo

*1

Grupo

idprogramasemestregetId()setId()getPrograma()setPrograma()getSemestre()setSemestre()

Alumno

idgruponombreCompletogetId()setId()getGrupo()setGrupo()getNombreCompleto()setNombreCompleto()

Page 3: Apache Cayenne Object Relational Mapping

Persistence

• Persistence means that the data used in a software application survives the application process.

• In other words, once you stop using or close the application the data will remain stored in a (relational) database.

• In Java terms, we would like the state of some of our objects to live beyond the JVM so that the same state is available later.

Page 4: Apache Cayenne Object Relational Mapping

Object-Relational Impedance Mismatch

• Impedance mismatch refers to the structural differences between a Relational Database and an Object Oriented Database.

• In order to do the mapping between both, we must overcome the following problems:

– Granularity.– Subtypes (Inheritance).– Identities.– Associations.– Access to the information.

Page 5: Apache Cayenne Object Relational Mapping

Frameworks for ORM

• Hibernate.• EclipseLink.• Data Knowledge Objects.• jOOQ.• Enterprise Java Beans EJB3.• Apache Cayenne.

Page 6: Apache Cayenne Object Relational Mapping

Prerequisites

• Have already installed:

– Java 7.– Eclipse EE with Maven plugin.– Install any *AMP server or MySQL from scratch.– Apache Cayenne Modeler 3.0 version.– MySQL JDBC driver.

• It is important to have the required versions because there are syntax changes from version to version.

Page 7: Apache Cayenne Object Relational Mapping

Getting Started

• The next step is to follow the Getting Started tutorial from the Cayenne website:

• https://cayenne.apache.org/docs/3.0/tutorial-starting-project.html• We are going to build a simple web application that has this

database design:

Page 8: Apache Cayenne Object Relational Mapping

Hands On

1. Create a Maven Project in Eclipse.– File > New > Project > Other > Maven > Maven Project > Simple project– Group Id: org.example.cayenne– Artifact Id: tutorial

2. Create a Cayenne Modeler Project.– New Project > Project– DataDomain Name > datadomain

3. Create a MySQL database in PHPMyAdmin– Start the Apache and MySQL servers– Open PHPMyAdmin– Databases > testdb > Create

Page 9: Apache Cayenne Object Relational Mapping

Cayenne Modeler

New Project > ProjectCreate Data Node > datanode– Datanode Name: datanode– JDBC Driver: com.mysql.jdbc.Driver– DB URL: jdbc:mysql://localhost:3306/testdb (port might change)– User: root– Password:

Create DataMap– DataMap Name: datamap– DataNode: datanode– Java Package: org.example.cayenne.persistent– Save the project in: tutorial/src/main/resources

Page 10: Apache Cayenne Object Relational Mapping

Cayenne Modeler

Create the database tables:– datamap > Create DB Entity– Name: ARTIST– Create Attribute: ID, INTEGER, PK– Repeat the steps for the Painting and Gallery tables.

Create relations between tables:– Select DB Entity ARTIST.– Select Relationships and Create Relationship.– Target: Painting.– Select Database Mapping. Relationship: PAINTING; Reverse: ARTIST; Source: ID;

Target: ARTIST_ID– In the paintings relation from ARTIST select To Many.– In the artist relation from PAINTING unselect To Many.– Repeat the same process for the GALLERY to PAINTING relation.

Page 11: Apache Cayenne Object Relational Mapping

Cayenne Modeler

Create entities for class generation:– Right click on ARTIST and click CREATE OBJECT ENTITY.– Select the generated object and click synchronize relationships.

Generate Java Classes:– Tools > Generate Classes– Output Directory: tutorial/src/main/java– Classes > Check All Classes– Generate

Page 12: Apache Cayenne Object Relational Mapping

Eclipse

Refresh the workspace and solve the dependencies:– Refresh (F5).– Edit the file pom.xml to look like in the following link: – http://cayenne.apache.org/docs/3.0/tutorial-java-classes.html

Page 13: Apache Cayenne Object Relational Mapping

Cayenne Modeler

Generate the Database Schema:– Tools > Preferences > ClassPath > Add Jar and select the JAR file for MySQL

Connector J– Tools > Generate Database Schema.– Username: root– Password:

Page 14: Apache Cayenne Object Relational Mapping

Eclipse

Create Main class in the org.example.cayenne package.– http://cayenne.apache.org/docs/3.0/tutorial-objectcontext.html– Add the MySQL J Connector dependency to the pom.xml file.– http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.27

Refer to the following page in the tutorial:– http://cayenne.apache.org/docs/3.0/tutorial-persistent-objects.html– Add the setDateOfBirth method to Artist.java– In the Main class, write the code to add one painter, two paintings and one

gallery.– Run the code and check the results in phpMyAdmin.– Then try the delete rules proposed in the tutorial:– http://cayenne.apache.org/docs/3.0/tutorial-delete.html

Page 15: Apache Cayenne Object Relational Mapping

Eclipse

Go to http://cayenne.apache.org/docs/3.0/tutorial-webapp.html in order to convert our Applcation it into a Web Application:– Create the folder src/main/webapp/WEB-INF– Create the file web.xml into src/main/webapp/WEB-INF– Create the file webapp/index.jsp– Create the file webapp/detail.jsp– Add the dependencies of maven-jetty-plugin to the pom.xml

Create an execution configuration for Maven:– Run > Run Configurations > Maven Build > New– Name: cayenne-tutorial– Base directory: ${workspace_loc:/tutorial}– Goals: jetty:run– Apply > Run– Go to http://localhost:8080/tutorial/– Enhance your web application so it can add paintings to an artist.