hibernate Flashcards
What is SessionFactory x 3
Provides the instance of Session.
Thread safe.
It holds the data of second level cache( not default).
What is Session? x 4
Maintains a connection between the hib and database.
It provides methods such as persist(), update(), delete(), load(), get() etc.
It is a factory of Query, Criteria and Transaction.
Not thread safe
SQL query vs HQL
Session.createSQLQuery
Session.createQuery
How can we add criteria to a SQL query
Session.createCriteria
session.save() vs session.persist()
returns Serializable vs void
get() las load()
null | real| | always hit the db vs ObjectNotFoundException | proxy
update() vs merge()
inside a session or error vs any time
states of the object in hibernate
transient, persisted, detached
Transient
Just created, no primary key (identifier), no session
Persistent
A session is open, and you just saved, retrieved the instance in / from db.
Detached
If a session is closed. call lock() or update() to go back to persisted.
Inheritance mapping strategies
Table per hierarchy | concrete class |subclass
Table per hierarchy
One table with discriminator column, annotations:
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name=”type”,discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue
@DiscriminatorValue
Table per hierarchy
One table with discriminator column, annotations:
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
Table per class
Tables are created as per class. But duplicate column is added in subclass tables
@Inheritance(strategy =InheritanceType.TABLE_PER_CLASS)