http://markpapis.com/java-interview-workshop-starter/
Java Hibernate: States of entity, loading data into entity, example relation mapping database table to object
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Java Hibernate Cheat Sheet
1. E X A M P L E D B T A B L E T O E N T I T Y ( 1 T O M A N Y R E L A T I O N )
S T A T E S O F E N T I T Y1
L O A D I N G D A T A I N T O E N T I T Y2
HIBERNATE
CHEAT
SHEET
new - new entity
load() OR get()
transient - entity class not associated with
Hibernate Session (no record in Database)
save() OR persist() OR saveOrUpdate()
persistent - entity class associated with Hibernate
Session (record in DB + ID given + waiting for
commit)
clear() OR evict()
detached - if we close Session, still can be modified
and reattached to new Session
object exists OR if You only assign a
relationships ID
- load() can return placeholder (proxy)
that triggers the loading of the real
object without hitting the database
not sure about objects existence
- get() always go to the database
Session.save()
- return the generated identifier
- can be used outside and inside transaction
- id in Detached state, always assigns new id
Session.persist()
- does not return the database identifier (so we get
it from entity class)
- can be used only within a transaction (so it’s safe
and takes care of any cascaded objects)
Session.saveOrUpdate()
- as save
- if id is not found -> update
Good Practice TIPS
use FetchType.LAZY to lazily
load the associated collections
use FETCH JOIN when needed
use bind variables to
eliminate SQL Injection
Session.update()
- if you are sure that the Hibernate session does not
contain an already persistent instance with the
same id
Session.merge()
- if you want to merge your modifications at any
time without considering the state of the session
- to update existing values
Session.saveOrUpdate()
- as update
MynameisMarkand
I'mJavaSenior
Consultantwith+10
yearsofexperience