Sunday, June 19, 2022

Hiberante

 Map entity to multiple tables

  • Annotate table with @SecondayTable(name ="author_details") annotation.
  • Provide table name in @Column(table ="author_details")  annotation
  • Benefit to split entity and its details into separate tables. Like author and author details
    Reference

Difference between First level and Second Level cache


KeyFirst level cacheSecond level cache


Basic 

First level cache is a session level 

cache and it is always associated with 

session level object

Second level cache is session factory level 

cache and it is available across all sessions


Enabled 

It is enabled by default.

It is not enabled by default.


Availability 

It is available for a session 

It is available across all session.


Configuration 

No Extra configuration required 

We have to decide  which
concurrency strategy to use and
also need to configure cache
expiration 
and physical cache attributes.


Different Entity states in JPA and Hibernate
























N+1 Problem

  • there is car and wheel table --> SELECT * FROM Cars;
  • in order to fetch wheel car we will hit db  --> SELECT * FROM Wheel WHERE CarId = ?
  • Solution to this JOIN tables (or Entity Graph)

Reference : Stackoverflow
Reference : Entity Graph 


Hibernate Fetch types

  • FetchMode JOIN : Eager loading which loads all the collections and relations at the same time.
  • FetchMode SELECT(default) : Lazy loading which loads the collections and relations only when required.
  • FetchMode SELECT with Batch Size : Fetch upto “N”collections or entities(“Not number of records”)
  • FetchMode SUBSELECT : Group the collection of an entity into a Sub-Select query.
Reference : link
Reference : Baeldung

Load() vs Get()

load()get()
Only use load() method if you are sure that the object exists.If you are not sure that the object exist, then use one of get() methods.
load() method will throw an exception if the unique id is not found in the database.get() method will return null if the unique id is not found in the database.
load() just returns a proxy by default and database won't be hit until the proxy is first invoked.get() will hit the database immediately.

Reference : link

Dirty Checking

  • Hibernate has a feature of checking all managed entity properties. Whenever an entity is loaded through hibernate, it makes an additional copy of that whole entity object along with the all entity's property values
  • So even if only one property of the persistent object is changed, Hibernate will still check all managed entities. It detects which objects have been modified and then calls update statements on all updated objects.
  • Hibernate monitors all persistent objects. At the end of a unit of work, it knows which objects have been modified. Then it calls update statement on all updated objects. This process of monitoring and updating only objects that have been changed is called automatic dirty checking in hibernate.
  • Within the Persistence Context, Hibernate has a copy of all persistent objects that were loaded from the database. It compares these persistent objects with these objects to detect the objects that have been modified or are dirty. This is the default implementation.
Reference : link

Query Cache and Second Level Cache

Second level cache look likes(key value)
*---------------------------------------------*
|          Person Data Cache                     |
|----------------------------------------------|
| 1 -> [ "John" , "Q" , "Public" , null ]   |
| 2 -> [ "Joey" , "D" , "Public" ,  1   ]    |
| 3 -> [ "Sara" , "N" , "Public" ,  1   ]    |
*---------------------------------------------*

Query cache look likes (key value)
*-------------------------------------------------------------------- *
|                       Query Cache                                                 |                     
|----------------------------------------------------------|-----------
| ["from Person where firstName=?", ["Joey"] ] -> [1, 2] ]  |
*---------------------------------------------------------------------*

What is the relation between the two caches?
  • If a query under execution has previously cached results, then no SQL statement is sent to the database. Instead the query results are retrieved from the query cache, and then the cached entity identifiers are used to access the second level cache.
  • If the second level cache contains data for a given Id, it re-hydrates the entity and returns it. If the second level cache does not contain the results for that particular Id, then an SQL query is issued to load the entity from the database.
Reference : link


Hibernate Inheritance Mapping 

  • MappedSuperclass – the parent classes, can't be entities
  • Single Table – The entities from different classes with a common ancestor are placed in a single table.
  • Joined Table – Each class has its table, and querying a subclass entity requires joining the tables.
  • Table per Class – All the properties of a class are in its table, so no join is required.
Reference : link


No comments:

Post a Comment

Thank you for your comment!