1 / 15

Apache Cayenne Object Relational Mapping

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.

yasuo
Télécharger la présentation

Apache Cayenne Object Relational Mapping

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Apache Cayenne Object Relational Mapping

  2. 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.

  3. 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.

  4. 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.

  5. Frameworks for ORM • Hibernate. • EclipseLink. • Data Knowledge Objects. • jOOQ. • Enterprise Java Beans EJB3. • Apache Cayenne.

  6. 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.

  7. 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:

  8. 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

  9. 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

  10. 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.

  11. 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

  12. 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

  13. 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:

  14. 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

  15. 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.

More Related