JPA and Hibernate
Introduction
Object-Relational-Mapping (ORM)
Initially, the property of java object is unmatched with the table of database, and we use JDBC directly to connect to the database which require us to write sql command for many times which will be time-consuming

So , we need Object-Relational-Mapping (ORM) to allow the conversion of data between java object and table of database. We can access to our target java object, and then converse the suitable data format automatically and store into database.
JPA (Java Persistence API)
It is a standard that providing method of insert, delete, update
Hibernate
Hibernate is one of the solution of ORM and is a hidden implementation of JPA interface, so as to convert the method of JPA into relative SQL command to access the table
Hibernate will create the sql command based the object, do the work of JDBC to connect to database

Entity Manager
Introduction
EntityManager is part of the Java Persistence API. Chiefly, it implements the programming interfaces and lifecycle rules defined by the JPA 2.0 specification.
It implements the hidden logic of JPA repository to manage the state of entity
Life Cycle
There are mainly four states of the entity :
Transient State
Persistent State
Detached State
Removed State

Transient State
The transient state is the first state of an entity object. When we instantiate an object of a POJO class using the new operator then the object is in the transient state. This object is not connected with any hibernate session.
Persistent State
Once the object is connected with the Hibernate Session then the object moves into the Persistent State.
In this state. each object represents one row in the database table. Therefore, if we make any changes in the data then hibernate will detect these changes and make changes in the database table.
Detached State
For converting an object from Persistent State to Detached State, we either have to close the session or we have to clear its cache. As the session is closed here or the cache is cleared, then any changes made to the data will not affect the database table.
Removed State
In the removed state, when the entity object is deleted from the database then the entity object is known to be in the removed state.
Pre-Action
Install Dependencies
Enter the configuration of database
Modal
Setup a modal of the java object which will be used to map to its relative table in database

@Entity: To label a class which will be used to mapped to its relational table, but also occupy columns and row to the table
@Column: As the name of column of table may not be the same as the class, it should be used top label the variable so as to map to its relational column with the name provided
@Id: To label the variable as a primary key
@GeneratedValue : It is mainly divided into 3 different strategies - Identity, Auto, Sequence, Table
Identity: The primary key of SQL have set the default value or auto-increment
Auto: No need do a setter and getter for primary key, the primary key will increased automatically and make sure the primary key of sql must be integer type
Not set: The primary key is a string type and set by setter
@DynamicInsert / DynamicUpdate: If the field is null, the field will not be inserted or updated , in order to improve the efficiency, detail: https://www.gaoyaxuan.net/blog/425.html
Repository
An interface which contains the method of JPA to access the table , we can also define the custom method on there.
1st parameter is a entity class
2nd parameter is the type of primary key
Basic Operation (CRUD)
Insert
Update
Delete
One-To-One Relationship
SQL
Modals
Service
Since Student is a entry point, so cascade must be added to Student class
One-To-Many Relationship
SQL
Modals
Service
Fetch type is divided into 2 types - lazy (default) and eager
Lazy: the list of hobby will not be obtained immediately when a student is obtained from database, only when the getter of student is called, the list of hobby can be obtained
Eager: When student is obtained from database, the list of hobby is obtained immediately, even the list will not be used
Fetch Mode:
https://www.baeldung.com/hibernate-fetchmode
https://www.jianshu.com/p/23bd82a7b96e
Lazy -> SELECT(default)
EAGER -> JOIN(default), SUB-SELECT
Many-To-Many Relationship
SQL
Modals
Service
Customize Repository
Method 1
Custom Interface
Custom Implementation
Original Interface
Method 2
Reference
Transaction
Introduction
Spring creates proxies for all the classes annotated with @Transactional, either on the class or on any of the methods. The proxy allows the framework to inject transactional logic before and after the running method, mainly for starting and committing the transaction, which applies AOP on the application
Implementation
Apart from that, we can also use programatic approach to implement by using transactional template
Propagation
Introduction
Propagation determines how transactions treats with other transaction
Spring calls TransactionManager::getTransaction to get or create a transaction according to the propagation.
Required
If a transaction is already active when the method is called, the method will run within that transaction. If a transaction is not active, a new transaction will be started.
Required_new
This option always starts a new transaction, regardless of whether a transaction is already active when the method is called. Any existing transaction will be suspended until the new transaction is complete.
If an exception is thrown and caught within the transaction, only the changes made within the current transaction will be rolled back, and the outer transaction will continue unaffected.
Mandatory
A transaction is already active when the method is called. If a transaction is not active, an exception will be thrown.
Never
A transaction should not be active when the method is called. If a transaction is already active, an exception will be thrown.
Not Supported
The method should not run within a transaction, but if a transaction is already active, it will be suspended until the method is complete
Supported
If a transaction is already active when the method is called, the method will run within that transaction. If a transaction is not active, the method will run outside of the transaction
Last updated
Was this helpful?