Hibernate Interview Questions

Difference between execute, executeUpdate and executeQuery in JDBC?

  • execute(): This is used to execute direct SQL DDL statement which modifies DB objects such as Table creation and returns boolean specifying whether the result can be retrieved.
  • executeUpdate(): This is used for executing DML statements such as insert, update, delete and return integer mentioning how many rows have been affected.
  • executeQuery: This is used to execute a select query which returns tabular data in return as a resultSet().
  • Hibernate difference between get session.get() and session.load() ? 

    Both load() and get method() belongs to Session class ans used to get data from a table using primary key or id.

    session.get() session.load()
    If the particular element is not present in database it throws null pointer Error. If the particular element is not present in the database it throws object Not Found Exception
    get() method directly hits the database and gets data about the particular object. load() method creates a proxy object for every query and loads data from there.
    This is eager initialization and does not save any cache, so u need to make database calls each time. This is lazy initialization and stores data in a cache using a proxy, hence we need not make DB call each time.

    How to create a composite primary key in hibernate?

    A composite primary key is a combination of 2 columns which together works as a primary key.
    The 2 annotations used for creating composite key are the @EmbededId and @IdClass annotation

      Below are the rules we need to follow for the composite Primary key class.
    • It must be serializable.
    • It must be public and should have a no-arg constructor.
    • It must oveeride equals() and hashcode() method.
    • Class is annotated with @IdClass annotation.

    What is an association in hibernate

    Association mapping in hibernate is the relation between 2 tables. Below are the details of the association we use. All of them can be mapped as uni or bi-directional.

    • One-to-One Association(@OneToOne)
    • many-to-One Association(@ManyToOne)
    • many-to-many Association(@ManyToMany)

    Confused about the difference between association and inheritance in hibernate, check below link from

    StackOverflowDifference between association and inheritance in Hibernate

    Describe the hibernate life cycle?

    Hibernate object has mainly 3 states or scope. They are as below

    1. Transient : This state is when we create an object of our POJO class with the new keyword, it exists in heap memory and independent of hibernate hence does not affect the database.
    2. Persistence: In this state object gets associated with Session operator hence it is called persistence object as we can save or persists it. Here modification of data affects database.The session object supports method like save(),persist(),saveOrUpdate(),lock() etc.
    3. Detached : On this state we close the session and object is no more associated with the session, hence modification will not affect change in the database.

    Hibernate how to get data page wise?

    There are few ways for pagination in hibernate. Below are some of them.
    Using Session criteria : Criteria criteria = session.createCriteria(xyz.class); criteria.setMaxResults(pageSize);
    Using query.setMaxResults(100) :
    Session session = sessionFactory.openSession(); Query query = sess.createQuery("From xyz"); query.setMaxResults(100);
    Using ScrollableResults :
    This is used with HQL and keeps cache hence if we use it will have less database calls.Below is an example.

    ScrollableResults resultScroll = query.scroll(ScrollMode.FORWARD_ONLY);
    int pageSize = 100;
    int i=0;
    while(pageSize >i++){
    resultScroll.get(0);
    }

    Why does Hibernate require no argument constructor?

    Here is a detailed discussion in StackOverflow

    Difference between CrudRespository and JPA Repository?

    JpaRepository in Spring internally extends CrudRespository.So it has all the properties of CRUDRepository plus other features as below.

    • No Need to access Entity Manager Factory we can directly call Repository method rather than calling Entity Manager Factory.
    • It helps access any type of data stores.

    How to prevent dirty read in hibernate?

    To prevent dirty reads, the database engine must hide uncommitted changes from all other transactions same time.

    Below are 2 ways to prevent them.
    • You can allow conflicts to occur, but you need to detect them using an optimistic locking mechanism (e.g. logical clock, MVCC) Detailed read
    • You can allow conflicts to occur, but you need to detect them using an optimistic locking mechanism (e.g. logical clock, MVCC)Detailed read
    Previous
    Next Post »

    Pages