#ORM (Object Relational Mapping) with JPA (Java Persistence API)
##Step by step:
Purpose: Simple walk through of JPA Steps:
- Create the project.
- Create a database and connect (for this demo I use mysql database: jpa1)
- Add the MYSQL JDBC Driver to the project libraries
- Create a new Persistance Unit (right click project and choose new..-> Persistence ...)
- choose EclipseLink JPA 2.1 (Should be the default choice for the Persistence Provider)
- Create new file -> Entity Class.
- Name it Person. and give it fields (ID, age, name)
- Run this class to create a db table from the entities:
public class Creater {
public static void main(String[] args) {
Persistence.generateSchema("JPADemo1PU", null); //Change PU name
}
}
- Then create a class with an EntityManager based on the Persistence Unit See how running the persist() method creates a new table in the database called person with ID, Age and Name.
- To get the created SQL from EclipseLink insert the following 2 property lines in to the persistence.xml. :
Resources:
Tutorialspoint on JPA
Simple JPA
More detailed JPA - this one provides detailed information
-
Java Persistence API is a source to store business entities (java objects) as relational entities (db tables)
-
Challenges:
- Table structure in db vs graph structure in objects (references containing references ...)
- Inheritance: Objects with inheritance hierarchy to be mirrored in tables
-
JPA providers:
- Hybernate
- EclipseLink
- Toplink
-
JPA architecture: javax.Persistence !alt text
-
Annotations
-
Persistence.xml
- registers the database and specify the entity class
-
Getting started: set up the first project.
###Exercise 1 - Basic JPA Find the exercise here
##Day 2 - More JPA
- JPA mapping directions
Cardinality | Direction | Example |
---|---|---|
One-to-one | Unidirectional | person and car |
One-to-one | Bidirectional | person and spouse |
One-to-many | Unidirectional | workplace and work station |
Many-to-one/one-to-many | Bidirectional | student and teacher |
Many-to-one | Unidirectional | student and school |
Many-to-many | Unidirectional | person and phone |
Many-to-many | Bidirectional | employee and company car |
- In relational database it is always a unidirectional relationship based on foreign keys
- In OO Programming Objects can have reference to each other
//In the Student class
private Teacher teacher;
//In the Teacher class
private List<Student> students;
###Exercise 2 - Relationships Find the exercise here
##Day 3 - JPA and Inheritance
- The 3 strategies for mapping inheritance in database tables
###Exercise 3 - Inheritance Find the exercise here
##Day 4 - JPQL
- Java Persitence Query Language
- Query the managed objects using the Entity Manager
- Like SQL
- Query Objects and fields instead of tables and collumns
###Exercise 4 - JPQL Find the exercise here