Spring-Hibernate0

1 using JPA annotation

@Entity // this class is entity bean

@Table(name = "student")

@Column(name = "id")

2. Two Key Players

Class

Description

SessionFactory

Reads the hibernate config file; Creates Session objects; Heavy-weight object; Only create once in your app

Session

Wraps a JDBC connection; Main object used to save/retrieve objects; Short-lived object; Retrieved from SessionFactory

3. Hibernate and Primary Keys

Hibernate 5

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;

3.1 ID Generation Strategies

Name

Description

GenerationType.AUTO

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

3.2 define your own CUSTOM generation strategy

  • create subclass of org.hibernate.id.SequenceGenerator

  • Override the method: public Serializable generate(...)

3.3 changing the index

SQL

3.4 retrieving an object

3.5 Querying Object

hibernate 5.2+

e.g.

e.g.

3.6 Updating Objects

3.7 Delete a Student

e.g.1

e.g.2

4. Advanced Mapping

  • One-to-One

  • Many-to-One, One-to-Many

  • Many-to-Many

Uni-Directional: One -to- One

4.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

4.2 Cascade

  • You can cascade operations

  • Apply the same operation to related entities

  • By default, no operations are cascaded.

e.g

If we delete an instructor, we should also delete their instuctor_detail

This is known as "CASCADE DELETE". It depends on the use case.

4.3 Eager vs Lazy Loading

Eager will retrieve everything

Lazy will retrieve on request

4.4 Create Instructor class - @OneToOne

5 Entity Lifecycle

Operations

Description

Detach

If entity is detached, it is not associated with a Hibernate session.

Merge

If instance is detached from session, then merge will reattach to session.

Persist

Transitions new instances to managed state. Next flush/ commit will save in db.

Remove

Transitions managed entity to be removed. Next flush/ commit will delete from db.

Refresh

Reload/ synch object with data from db. Prevents stale data

5.1 @OneToOne

5.1.1 - Cascade Type

Persist, Remove, Refresh, Detach, Merge, All.

configure cascade Type

configure cascade Type

5.1.2 @OneToOne Bi-Directional

mappedBy tells Hibernate

  • Look at the instructorDetail property in the Instructor class

  • Use information from the Instructor class @JoinColumn

  • To help find associated instructor

5.1.3 Code Refactoring Add Exception Handling

5.1.4 Only delete InstructorDetail keep the Instructor

remove the associated object reference

break bi-directional link

cascade: Do not apply cascading deletes!

5.2 @OneToMany

  • An instructor can have many courses

  • Bi-directional

Many-to-One Mapping

  • Many courses can have one instructor

  • Inverse / opposite of One-to-Many

Real-World Project Requirement

  • If you delete an instructor, DO NOT delete the courses

  • If you delete a course, DO NOT delete the instrucrot

When add support for Cascading:

Do not apply cascading deletes!

5.2.1 @JoinColumn & @mappedBy

@JoinColumn

A Join Column in JPA is a column in the owner entity that refers to a key (usually a primary key) in the non-owner or inverse entity. The first thing that comes to mind after reading the above line is that JoinColumns are nothing but a Foreign Key Columns. And it is indeed the case. JPA calls them Join Columns, possibly because they are more verbose in what their actual role is, to join the two entities using a common column.

Another thing to notice above is that we have used the terms owner and non-owner entities. For easy rememberability, the entity that has a join column is always the owning entity.

@mappedBy

The field that owns the relationship. Required unless the relationship is unidirectional.

Last updated

Was this helpful?