-
Entity Classes
simple classes with simple properties and get/set methods do share similarities with database tables
Database tables correspond (roughly) to class definitions. Classes are much more general than database tables (e.g. tables do not inherit one another, they do not contain methods and they do not implement interfaces). However simple classes with simple properties and get/set methods do share similarities with database tables. These classes are called Entity Classes.
-
ORM
The mapping from classes to database tables (and vice versa) is called Object Relational Mapping (ORM).
-
JPA
- Java Persistence API (JPA)
- a specification for implementing ORM
- is not a specific implementation – it defines how implementations should manage the object/relational mapping between Java classes and relational databases.
- Specific implementations include Hibernate and EclipseLink (and several others).
-
Why JPA instead of jdbc?
- jdbc uses Strings
- JPA uses objects
Whereas jdbc allows Java developers to create database connections and to execute SQL statements (represented as strings in the Java code), JPA allows developers to manage database connections and database operations via Java objects. JPA queries return Java objects (entities) and entities can be stored (persisted) to the database. JPA also provides an object-oriented query language (as well as supporting execution of native SQL statements, if preferred).
-
What annotations do Entity classes use to support ORM.
- @Entity annotation: used to introduce entity class
- @Id annotation: attribute that is to be mapped to the primary key
JPA handles composite primary keys, but for the purposes of this introduction, primary keys will be assumed to be single-valued.
-
other JPA annotations for various relationships
- @OneToOne
- @ManyToOne
- @OneToMany
- @ManyToMany
-
EntityManager
- JPA provides the EntityManager class to manage data base operations for a variety of different platforms (e.g. MySQL, JavaDB, Oracle, DB2, etc.).
- Each EntityManager instance must be configured for use with a particular database connection on a particular database platform on a particular host. This configuration is based on information stored in an XML file called a Persistence Unit. The actual configuration is done by an EntityManagerFactory class.
-
Persistence Unit
an XML file that stores information that an EntityManager uses to configure a particular database connection on a particular database platform on a particular host.
-
EntityManager instance must be configured...this is done by
The actual configuration is done by an EntityManagerFactory class.
-
main steps to create a Simple Console Application with JPA
- Create a Java console application.
- Create a new MySQL database and database connection.
- Add a persistence unit to the project. Name the persistence unit JPAConsoleAppPU and link it to the MySQL demodb connection.
- Add an Item entity class to the project.
- Add code to the main class to instantiate an EntityManagerFactory and EntityManager.
- Add code to create and store/retrieve Item objects to/from the database
-
What is JPA?
- Java Persistence API is a collection of classes and methods to persistently store the vast amounts of data into a database which is
- provided by the Oracle Corporation.
-
Where to use JPA?
To reduce the burden of writing codes for relational object management, a programmer follows the ‘JPA Provider’ framework, which allows easy interaction with database instance.
-
core classes and interfaces of JPA.
- EntityManagerFactory
- EntityManager
- EntityTransaction
- Query
- Persistence
-
EntityManagerFactory
EntityManager
- EntityManagerFactory
- This is a factory class of EntityManager. It creates and manages multiple EntityManager instances.
- EntityManager
- It is an Interface, it manages the persistence operations on objects. It works like factory for Query instance.
-
Entity
EntityTransaction
Entity: Entities are the persistence objects, stores as records in the database.
EntityTransaction: It has one-to-one relationship with EntityManager. For each EntityManager, operations are maintained by EntityTransaction class.
-
Persistence
Query
Persistence: This class contain static methods to obtain EntityManagerFactory instance.
Query: This interface is implemented by each JPA vendor to obtain relational objects that meet the criteria.
-
JPA Class Relationships diagram
-
Object Relational Mapping
- ORM is a programming ability to covert data from object type to relational type and vice versa.
- The main feature of ORM is mapping or binding an object to its data in the database. While mapping we have to consider the data, type of data and its relations with its self-entity or entity in any other table.
-
EntityManager
- creates, reads, updates and deletes entities
- CRUD
|
|