150 likes | 406 Views
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.
E N D
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: • 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:
Hands On • Create a Maven Project in Eclipse. • File > New > Project > Other > Maven > Maven Project > Simple project • Group Id: org.example.cayenne • Artifact Id: tutorial • Create a Cayenne Modeler Project. • New Project > Project • DataDomainName > datadomain • Create a MySQL database in PHPMyAdmin • Startthe 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 Createthedatabasetables: • datamap> Create DB Entity • Name: ARTIST • CreateAttribute: ID, INTEGER, PK • RepeatthestepsforthePainting and Gallerytables. 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.xmlto look like in the following link: • http://cayenne.apache.org/docs/3.0/tutorial-java-classes.html
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. • http://cayenne.apache.org/docs/3.0/tutorial-objectcontext.html • Add the MySQL J Connector dependency to the pom.xmlfile. • 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
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.