Hibernate
Hibernate
Hibernate
3. What is a SessionFactory?
SessionFactory provides an instance of Session. It is a factory class that gives the Session objects based
on the configuration parameters in order to establish the connection to the database.
As a good practice, the application generally has a single instance of SessionFactory. The internal state
of a SessionFactory which includes metadata about ORM is immutable, i.e once the instance is created,
it cannot be changed.
This also provides the facility to get information like statistics and metadata related to a class, query
executions, etc. It also holds second-level cache data if enabled.
HIBERNATE
4. What do you think about the statement - “session being a thread-safe object”?
No, Session is not a thread-safe object which means that any number of threads can access data from
it simultaneously.
It is to be noted that, since Hibernate 3 version, this feature has been enabled by default. This signifies
that child objects are not loaded until the parent gets loaded.
6. What is the difference between first level cache and second level cache?
Hibernate has 2 cache types. First level and second level cache for which the difference is given below:
This is local to the Session object and cannot be shared between multiple sessions.------This cache is
maintained at the SessionFactory level and shared among all sessions in Hibernate.
In the case of using annotations, immutable classes in hibernate can also be created by using
@Immutable annotation.
Hibernate does not provide immunity to SQL Injection. However, following good practices avoids SQL
injection attacks. It is always advisable to follow any of the below options:
HIBERNATE
Incorporate Prepared Statements that use Parameterized Queries.
Use Stored Procedures.
Ensure data sanity by doing input validation.
This method gets the data from the database as soon as it is called.----This method returns a proxy
object and loads the data only when it is required.
The database is hit every time the method is called.----The database is hit only when it is really needed
and this is called Lazy Loading which makes the method better.
The method returns null if the object is not found.-----The method throws ObjectNotFoundException if
the object is not found.
This API allows to programmatically development criteria query objects. The org.hibernate.Criteria
interface is used for these purposes. The Session interface of hibernate framework has createCriteria()
method that takes the persistent object’s class or its entity name as the parameters and returns
persistence object instance the criteria query is executed.
It also makes it very easy to incorporate restrictions to selectively retrieve data from the database. It
can be achieved by using the add() method which accepts the org.hibernate.criterion.Criterion object
representing individual restriction.
Usage examples:
Similarly, it also has other methods like isNull(), isNotNull(), gt(), ge(), lt(), le() etc for adding more
varieties of restrictions. It has to be noted that for Hibernate 5 onwards, the functions returning an
object of typeCriteria are deprecated. Hibernate 5 version has provided interfaces like CriteriaBuilder
and CriteriaQuery to serve the purpose:
javax.persistence.criteria.CriteriaBuilder
javax.persistence.criteria.CriteriaQuery
// Create CriteriaBuilder
CriteriaBuilder builder = session.getCriteriaBuilder();
// Create CriteriaQuery
CriteriaQuery<YourClass> criteria = builder.createQuery(YourClass.class);
Transient:
This state is the initial state of any entity object.
Once the instance of the entity class is created, then the object is said to have entered a transient
state. These objects exist in heap memory.
In this state, the object is not linked to any session. Hence, it is not related to any database due to
which any changes in the data object don't affect the data in the database.
InterviewBitEmployee employee=new InterviewBitEmployee(); //The object is in the transient state.
employee.setId(101);
employee.setFullName("Hibernate");
employee.setEmail("hibernate@interviewbit.com");
Persistent:
This state is entered whenever the object is linked or associated with the session.
An object is said to be in a persistence state whenever we save or persist an object in the database.
Each object corresponds to the row in the database table. Any modifications to the data in this state
cause changes in the record in the database.
HIBERNATE
Following methods can be used upon the persistence object:
session.save(record);
session.persist(record);
session.update(record);
session.saveOrUpdate(record);
session.lock(record);
session.merge(record);
Detached:
The object enters this state whenever the session is closed or the cache is cleared.
Due to the object being no longer part of the session, any changes in the object will not reflect in the
corresponding row of the database. However, it would still have its representation in the database.
In case the developer wants to persist changes of this object, it has to be reattached to the hibernate
session.
In order to achieve the reattachment, we can use the methods load(), merge(), refresh(), update(), or
save() methods on a new session by using the reference of the detached object.
The object enters this state whenever any of the following methods are called:
session.close();
session.clear();
session.detach(record);
session.evict(record);
This does not cache the state of actual entities in the result set but it only stores the identifier values
and results of the value type. Hence, query cache should be always used in association with second-
level cache.