a digital commerce consultancy san francisco ~ new york ~ london ~ chişinău ~ guadalajara 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 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.
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.
Frameworks for ORM Hibernate. EclipseLink. Data Knowledge Objects. jOOQ. Enterprise Java Beans EJB3. Apache Cayenne.
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.
Getting Started The next step is to follow the Getting Started tutorial from the Cayenne website: We are going to build a simple web application that has this database design:
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
Cayenne Modeler New Project > Project Create 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
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.
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
Eclipse Refresh the workspace and solve the dependencies: – Refresh (F5). – Edit the file pom.xml to look like in the following link: –
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:
Eclipse Create Main class in the org.example.cayenne package. – – Add the MySQL J Connector dependency to the pom.xml file. – Refer to the following page in the tutorial: – – 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: –
Eclipse Go to 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 – Enhance your web application so it can add paintings to an artist.