Hibernate Tutorial


  1. What is hibernate ?
    Hibernate is an ORM tool used for data transfer, between Java application and database, in the form of object. Hibernate is developed by Gavin King.
  2. Why not JDBC ?
    1.  Code written in jdbc are database dependent.
    2.  Does not have cache support.
    3.  Data is not secured.
  3. Why Hibernate ?
    Hibernate provides following features :
    1. Auto DDL support
    2. Hibernate query language support(Database independent  query)
    3. Cache support
    4. Primary key generator
    5. Validations support
    6. Exception Free(There is no compile time exception)
    7. ORM support

  4. State of objects during CRUD operation
    If we are doing CRUD operation (e.g. Create, Insert, Update, Delete) then we have to pass object. There are three types of object in Hibernate.
                  1. Transient object
                  2. Persistent object
                  3. Detached object
  5. What is Cascade Type in hibernate ?
    There is concept of association(One To One,One To Many,Many To One,Many To Many) between entity classes. If we want to do changes in the parent entity and expecting same action should happen on child entity then we go for cascading concept.
    There are six type of cascading in hibernate:-
    1. CascadeType.PERSIST :- If we save parent entity data then child entity data will save automatically but if delete parent entity data then child will not be deleted.
    2.
     CascadeType.MERGE :- cascade type merge means that related entities are merged when the owning entity is merged.
    3. 
    CascadeType.REFRESH :- cascade type refresh does the same thing for the refresh() operation.
    4. 
    CascadeType.REMOVE :- cascade type remove removes all related entities association with this setting when the owning entity is deleted.
    5. 
    CascadeType.DETACH :- cascade type detach detaches all related entities if a “manual detach” occurs.
    6. CascadeType.ALL : cascade type all is shorthand for all of the above cascade operations.
  6. What is difference between update and merge method ?
    Suppose we are creating a session and load an object, and store in local object type of variable and then after we close the session then if we edit the state of object and try to save using update method then it will throw exception NonUniqueObjectException
    In this case to persist the object we need to use merge method.
    Code:-
    public class MergeUpdateClientTest{
    public static void main(String arg[]){
    Employee employee1=null;
    try(Session session=HibernateUtil.getSessionFactory().openSession()){
    employee1= session.get(Employee.class, 1);
    System.out.println(employee1);
    }catch(HibernateException e1){
    e1.printStackTrace();
    }

    employee1.setSalary(95000);
    try(Session session=HibernateUtil.getSessionFactory().openSession()){
    session.beginTransaction();
    Employee employee2= session.get(Employee.class, 1);
    session.update(employee1); //Will throw NonUniqueObjectException
    session.merge(employee1);//Data will be updated successfully 
    session.getTransaction().commit();
    }catch(HibernateException e1){
    e1.printStackTrace();
    }
    }}
  7. How to complete a transaction in Hibernate?
    There are two distinct actions if you’re looking to complete a transaction in hibernate:
    • Commit
    • Rollback

    Once a transaction is committed, the transaction data is written to the database. In case there is a rollback, the data exchange is flushed and never written or updated to the database.
  8. Can you touch upon the different types of relationships available in Hibernate mapping?
    Hibernate consists of three different types of relationships:
    • One-to-One mapping
    • One-to-Many mapping
    • Many-to-Many mapping
  9. What is the purpose of Session.beginTransaction()?
     
    Hibernate keeps a log of every data exchange with the help of a transaction. Thereon, in case a new exchange of date is about to get initiated, the function Session.beginTransaction is executed in order to begin the transaction.

Comments

Popular posts from this blog

Sealed Classes and Interfaces in Java

Introduction of RabbitMQ

RabbitMQ Installation on Windows