Hibernate

  • Hibernate - Native SQL 
  • Hibernate - SQLQuery 
  • Restrictions with Criteria 
  • Pagination Using Criteria 
  • Sorting the Results 
  • Projections :- 
  • Aggregations :- 
  • Conjunction :- It used to add multiple AND clause in SQL query within brackets. 
  • Disjunction :- It used to add multiple OR clause in SQL query within brackets.
What is Hibernate? What are the advantages of hibernate framework ?
  • OpenSource and Lightweight
  • Fast Performance
  • Database Independent Query - HQL (Hibernate Query Language)
  • Automatic Table creation
  • Simplifies Complex Join
  • Provides Query Statistics and DataBase Status.
Fetching strategies in hibernate
  • Join
  • Batch
  • Select
  • Sub-Select
Inheritance in hibernate
  • Single Table
  • Joined Table
  • Table per Class
Question: Cache Concurrency Strategy ?
Answer: Below are Cache Concurrency Strategy.
  • READ_ONLY
  • NONSTRICT_READ_WRITE
  • READ_WRITE
  • TRANSACTIONAL
Q- What are the core interfaces of hibernate?
Hibernate framework has as mentioned below the core interfaces:
  • Configuration.
  • SessionFactory.
  • Session.
  • Query.
  • Criteria.
  • Transaction.
Hibernate Framework Architecture Diagram.

Hibernate support two type of cache one is first level cache and second level cache.
first-level cache :- first-level cache is associated with session in hibernate. It is enabled by default.
Fecond-level cache :- second-level cache is associated with sessionFactory in hibernate. It is not enabled by default.

Question : How to enable second level cache in hibernate ? 
Answer: Using hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">toor</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
        <property name="hibernate.cache.use_query_cache">true</property>
        <property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
 
        <mapping class="com.examples.jcg.entity.Person" />
        <mapping class="com.examples.jcg.entity.Account" />
 
    </session-factory>
</hibernate-configuration>
To enable the Second Level Cache in hibernate, we have to use the below property 
hibernate.cache.use_second_level_cache  set it to true
hibernate.cache.use_query_cacheproperty is used to select the underlying Cache as we are using EhCacheRegionFactory 
hibernate.cache.use_query_cache set to true.To enable the Query Cache
Note that Query Cache stores the Keys of the entities not the entire Object Values. 

Question : How to enable second level cache in hibernate spring boot ? 
Question : What is object state in Hibernate?
Answer : There are three state of an object/entity in Hibernate.
  • Transient : When we create an object it is in transient state OR if it has just been instantiated using the new operator OR we can say if an instance is not associated with any persistent context and it is never been associated with any persistent context then it is called transient state.
  • Persistent : An object that is associated with the hibernate session (persistent context) is called as persistent object. OR we can say As the object associated with the Session, it entered in the persistent state. ·
  • Detached : When an instance was earlier associated with persistent context mean associated with hibername session and now no longer associated. its called to be in detached state. 
Question : What happens If both hibernate. properties and hibernate. cfg. xml files are present in the classpath then hibernate.
Answer:  hibernate.cfg.xml overrides to hibernate.properties that means settings of hibernate.cfg.xml will be used.

Qusetion: Does hibernate require persistent classes(POJO) to implement serializable?
Answer: Hibernate does not require serializable at all. however when object are stored in httpSession or pass by value using RMT serializable is necessary.

Question: What is dirty checking in hibernate?
Answer: Hibernate automatically detects object state changes in order to synchronize the updated state with the database, this is called dirty checking.
An important note is that hibernate will compare objects by values excepts for collections which are compare by identity. So due to this reason we should return the exactly same collection instance as hibernate pass to setter method to prevent unnecessary database update

Question: How can we avoid dirty checking in Hibernate?
Answer: To stop dirty checking we nedd to change the default configuration of FlushMode from auto to manual by setting FlushMode. MANUAL

Question: What is hibernate proxy?
Question: What is mean by light object mapping?
Question: What is hibernate tuning?
Question: What is version checking in hibernate?
Question: Explain about version field?
Question: What is EntityManager in hibernate?
Answer:
  • The EntityManager interface is an API that used to manages the lifecycle of an entity instance.
  • Because entities can not persist themselves on the relational database. annotations are used to declare a POJO class as an entity. 
  • An entity define its mapping and relationships with the table of database in the relational database.
  • In JPA, EntityManager interface is used to allow applications to manage (CURD operations) for entities in the relational database.
  • An   EntityManager object   is used to manages a set of entities that defined by a persistence unit.
  • In an appliction each EntityManager instance is associated with a persistence context
  • A persistence context is a set of entities. 
  • Any persistent context identity the unique entity instance. Within a persistence context, entities are managed.
  • The entity manager tracks all entity objects within a persistence context of any operation (CURD operation) that are made on entity object, and flushes these changes to the database.
  • Once a persistence context is closed (i.e changes done), all entity object instances become in detachedstate  from the persistence context then its become no longer managed by associated entity manager.
  • Once any object is detached from a persistence context, it will no longer be managed by entity manager, after that any changes to this object instance will not be synchronized with the database.
Managed and unmanaged entities
Note: Any entity object instance is either managed (i.e. attached)  or unmanaged (i.e detached) by an entity manager
  • Managed Or Attached: While an entity is attached to an entity manager, and make any changes to the entity,  synchronizes with the database are monitored by entity manager and entity manager is decides to flush its state.
  • Unmanaged  Or Detached:  While an entity is detached, and there is no more associated with a persistence context, it is called unmanaged. Its state changes are not tracked by the entity manager.

Q- What is EntityManagerFactory in hibernate?



Related Tutorials

No comments:

Post a Comment