Java Persistence API
Java Persistence API
ORM Frameworks
• Following are the various frameworks that function on ORM
mechanism: -
• Hibernate
• TopLink
• ORMLite
• iBATIS
• JPOX
JPA ORM
Mapping Directions
• Mapping Directions are divided into two parts: -
• Unidirectional relationship - In this relationship, only one
entity can refer the properties to another. It contains only one
owing side that specifies how an update can be made in the
database.
• Bidirectional relationship - This relationship contains an
owning side as well as an inverse side. So here every entity
has a relationship field or refer the property to other entity.
JPA ORM
Types of Mapping:
Following are the various ORM mappings: -
• One-to-one - This association is represented by @OneToOne
annotation. Here, instance of each entity is related to a single
instance of another entity.
• One-to-many - This association is represented by
@OneToMany annotation. In this relationship, an instance of
one entity can be related to more than one instance of another
entity.
JPA ORM
Types of Mapping:
Following are the various ORM mappings: -
• Many-to-one - This mapping is defined by @ManyToOne
annotation. In this relationship, multiple instances of an
entity can be related to single instance of another entity.
• Many-to-many - This association is represented by
@ManyToMany annotation. Here, multiple instances of an
entity can be related to multiple instances of another entity.
In this mapping, any side can be the owing side.
Architecture Of JPA
Database in JSP:
• JPA provides a convenient way to interact with databases without
having to write low-level SQL code.
ORM:
• ORM stands for Object-Relational Mapping, which is a
programming technique that allows software engineers to write in
any programming language of their choice and encapsulate (hide)
the code needed to manipulate the data, so user don't use SQL
anymore;
• User interact directly with an object in the same language you're
writing.
Database and the Application
ORM:
• The goal of ORM is to help software engineers work with databases
by allowing them to interact with the database using object-oriented
programming concepts, such as classes, objects, and methods, rather
than having to write low-level SQL code.
• For example, working with database using Java application.
• User have a database, and use JDBC to connect the Java application to the
database.
• Where user have a class and the class structure, that class has some variables,
and then have a table.
• Next will implement, table name will be the class name, and the columns will
be the class variable. And, this is how we use object-relational mapping.
Database and the Application
entityManager.getTransaction().commit();
• The code above creates a new "Customer" object, sets its first name and
last name, and then persists it to the database.
• The transaction is committed at the end, which ensures that the changes
are saved to the database.
Database and the Application
• In JPA, use annotations such as "@Entity" and "@Id" to map Java
objects to database tables.
• an example of a "Customer" entity class:
@Entity @Table(name = "customers")
public class Customer
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
//can add getters and setters here
}
Database and the Application
• The code above defines a "Customer" entity class with three fields: "id"
"firstName" and "lastName."
• The "@Entity" annotation specifies that this class should be mapped to
a database table.
• The "@Id" annotation specifies that the "id" field is the primary key for
this table, and the "@GeneratedValue" annotation specifies that this
field should be automatically generated by the database.
Retrieve a "Customer" object by its ID:
Customer customer = entityManager.find(Customer.class,
1L);
• This code retrieves the "Customer" object with ID 1 from the database.
Database and the Application
Applications in JPA:
• JPA combines the best features from previous persistence
mechanisms such as Java Database Connectivity (JDBC) APIs,
Object Relational Mapping (ORM) frameworks, and Java Data
Objects (JDO).
• Creating entities under JPA is as simple as creating serializable
classes.
• JPA supports the large data sets, data consistency, concurrent use,
and query capabilities of JDBC.
Database and the Application
• User can create new database tables from existing entity classes
(top-down mapping) or new entity beans from existing database
tables (bottom-up mapping).
• User can also use the tools to create mappings between existing
database tables and entity beans (meet-in-the-middle mapping),
where names or other attributes differ.
• Also create mappings from several types of Java class, and can
specify entity inheritance with several options for database design.
Specification Of JPA