You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@OverridepublicBookfindById(intid) {
returnentityManager.find(Book.class, id);
}
@OverridepublicList<Book> findAll() {
returnentityManager.createQuery("select s FROM Book s ORDER BY s.name ASC", Book.class).getResultList();
}
2.5. Querying for Java Objects
@OverridepublicList<Book> findAll() {
returnentityManager.createQuery("select s FROM Book s ORDER BY s.name ASC", Book.class).getResultList();
}
2.6. Hibernate / JPA and JDBC
How does Hibernate / JPA relate to JDBC?
Hibernate / JPA uses JDBC for all database communications.
In Spring Boot, Hibernate is the default implementation of JPA.
EntityManager is main component for creating queries etc...
EntityManager is from Jakarta Persistence API (JPA).
Based on configs, Spring Boot will automatically create the beans:
DataSource, EntityManager, ...
You can then inject these into your app, for example your DAO.
Pick an appropriate strategy for the particular database.
GenerationType.IDENTITY
Assign primary keys using database identity column.
GenerationType.SEQUENCE
Assign primary keys using a database sequence.
GenerationType.TABLE
Assign primary keys using an underlying database table to ensure uniqueness.
GenerationType.UUID
Assign primary keys using a globally unique identifier (UUID) to ensure uniqueness.
3.4.1. Custom Strategies
You can define your own CUSTOM generation strategy.
Create implementation of org.hibernate.id.IdentifierGenerator
Override the method: public Serializable generate(...)
4. DAO - Data Access Object
Responsible for interfacing with the database.
This is a common design pattern: Data Access Object (DAO).
4.1. JPA Entity Manager
Our DAO needs a JPA Entity Manager.
JPA Entity Manager is the main component for saving/retrieving entities.
Our JPA Entity Manager needs a Data Source.
The Data Source defines database connection info.
JPA Entity Manager and Data Source are automatically created by Spring Boot.
Based on the file: application.properties (JDBC URL, user id, password, etc ...)
We can autowire/inject the JPA Entity Manager into our Student DAO
4.2. What about JpaRepository?
Spring Data JPA has a JpaRepository interface.
This provides JPA database access with minimal coding.
In Simple Terms
If you need low-level control and flexibility, use EntityManager
If you want high-level of abstraction, use JpaRepository
4.2.1. Which One EntityManager or JpaRepository?
Entity Manager
JPA Repository
Need low-level control over the database operations and want to write custom queries
Provides commonly used CRUD operations out of the box, reducing the amount of code you need to write
Provides low-level access to JPA and work directly with JPA entities
Additional features such as pagination, sorting
Complex queries that required advanced features such as native SQL queries or stored procedure calls
Generate queries based on method names
When you have custom requirements that are not easily handled by higher-level abstractions
Can also create custom queries using @Query
4.2.2. My Recommendation
Choice depends on the application requirements and developer preference.
You can also use both in the same project.
For learning purposes, start with EntityManager then learn JpaRepository.
This will help you understand the low-level coding behind the scenes.
Knowing BOTH EntityManager and JpaRepository will help you on future projects.
5. Spring @Transactional
Spring provides an @Transactional annotation.
Automagically begin and end a transaction for your JPA code.
No need for you to explicitly do this in your code.
6. Spring @Repository
Spring provides the @Repository annotation
Applied to DAO implementations
Spring will automatically register the DAO implementation
thanks to component-scanning
Spring also provides translation of any JDBC related exceptions
7. JPA Query Language (JPQL)
Query language for retrieving objects.
Similar in concept to SQL.
where, like, order by, join, in, etc...
However, JPQL is based on entity name and entity fields.
7.1. Retrieving all Students
@OverridepublicList<Book> findAll() {
returnentityManager.createQuery("select s FROM Book s ORDER BY s.name ASC", Book.class).getResultList();
}
Note: this is NOT the name of the database table.
All JPQL syntax is based on entity name and entity fields
7.2. JPQL - Named Parameters
@Override@Transactionalpublicintdelete(intid) {
Queryquery = entityManager.createQuery("DELETE FROM Book WHERE id = :id");
query.setParameter("id", id);
returnquery.executeUpdate();
}
8. Create database tables
JPA/Hibernate provides an option to automagically create database tables.
Creates tables based on Java code with JPA/Hibernate annotations.
Useful for development and testing.
8.1. Configuration
In Spring Boot configuration file: application.properties.
spring.jpa.hibernate.ddl-auto=create
When you run your app, JPA/Hibernate will drop tables then create them.
Based on the JPA/Hibernate annotations in your Java code.
8.2. application.properties
Property Value
Property Description
none
No action will be performed.
create-only
Database tables are only created.
drop
Database tables are dropped.
create
Database tables are dropped followed by database tables creation.
create-drop
Database tables are dropped followed by database tables creation. On application shutdown, drop the database tables.
validate
Validate the database tables schema.
update
Update the database tables schema.
When database tables are dropped, all data is lost.
8.3. Basic Projects
For basic projects, can use auto configuration.
spring.jpa.hibernate.ddl-auto=create
Database tables are dropped first and then created from scratch.
If you want to create tables once ... and then keep data, use: update.
However, will ALTER database schema based on latest code updates.
Be VERY careful here... only use for basic projects.
Don't do this on Production databases!!!
You don't want to drop your Production data.
All data is deleted!!!
Instead for Production, you should have DBAs run SQL scripts.
8.4. Use Case
spring.jpa.hibernate.ddl-auto=create
Automatic table generation is useful for.
Database integration testing with in-memory databases.
Basic, small hobby projects.
8.5. Recommendation
In general, I don't recommend auto generation for enterprise, real-time projects.
You can VERY easily drop PRODUCTION data if you are not careful.
I recommend SQL scripts.
Corporate DBAs prefer SQL scripts for governance and code review.
The SQL scripts can be customized and fine-tuned for complex database designs.
The SQL scripts can be version-controlled.
Can also work with schema migration tools such as liquibase and flyway.
9. Advanced Mappings
One-to-One.
One-to-Many, Many-to-One.
Many-to-Many.
9.1. One-to-One
An Teacher can have an Teacher Detail entity.
Similar to an Teacher Profile.
9.2. One-to-Many
An Teacher can have many Subjects.
Bi-Directional
9.2.1. Many-to-One
Inverse / opposite of One-to-Many.
9.3. Many-to-Many
A Subject can have many Students.
A Student can have many Subjects.
10. Important Database Concepts
Primary key and foreign key.
Cascade.
10.1. Primary Key and Foreign Key
Primary key: Identify a unique row in a table.
Foreign key
Link tables together a field in one table that refers to primary key in another table.
Main purpose is to preserve relationship between tables.
Referential Integrity.
Prevents operations that would destroy relationship.
Ensures only valid data is inserted into the foreign key column.
Can only contain valid reference to primary key in other table.
10.2. Cascade
You can cascade operations.
Apply the same operation to related entities.
If we delete an teacher, we should also delete their teacher_detail.
This is known as CASCADE DELETE.
Cascade delete DEPENDS on the use case.
Developer can configure cascading.
10.2.1. Cascade Types
Cascade Type
Description
PERSIST
If entity is persisted / saved, related entity will also be persisted.
REMOVE
If entity is removed / deleted, related entity will also be deleted.
REFRESH
If entity is refreshed, related entity will also be refreshed.
DETACH
If entity is detached (not associated w/ session), then related entity will also be detached.
MERGE
If entity is merged, then related entity will also be merged.
Look at the student_id column in the subject_student table.
For other side (inverse), look at the subject_id column in the subject_student table.
Use this information to find relationship between student and subject.
15.1. More on "inverse"
In this context, we are defining the relationship in the Student class.
The Subject class is on the other side ... so it is considered the inverse.
"Inverse" refers to the other side of the relationship.
16. Common Errors
Error:org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.example.real_jpa_entity_relationships.models.Teacher.teacherDetail -> com.example.real_jpa_entity_relationships.models.TeacherDetail
Solution: Forgot CascadeType.ALL or CascadeType.PERSIST.
Error: Infinite recursion stackoverflow problem, this happens as it's going to convert the Java Object into a JSON Object.