• +91 9723535972
  • info@interviewmaterial.com

Hibernate Interview Questions and Answers

Hibernate Interview Questions and Answers

Question - 91 : - Can you tell something about Table Per Class Strategy.

Answer - 91 : -

Table Per Class Strategy is another type of inheritance mapping strategy where each class in the hierarchy has a corresponding mapping database table. For example, the InterviewBitContractEmployee class details are stored in the interviewbit_contract_employee table and InterviewBitPermanentEmployee class details are stored in interviewbit_permanent_employee tables respectively. As the data is stored in different tables, there will be no need for a discriminator column as done in a single table strategy.

Hibernate provides @Inheritance annotation which takes strategy as the parameter. This is used for defining what strategy we would be using. By giving them value, InheritanceType.TABLE_PER_CLASS, it signifies that we are using a table per class strategy for mapping.

The code snippet will be as shown below:

InterviewBitEmployee class:

@Entity(name = "interviewbit_employee")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@NoArgsConstructor
@AllArgsConstructor
public class InterviewBitEmployee {
   @Id
   @Column(name = "employee_id")
   private String employeeId;
   private String fullName;
   private String email;
}
InterviewBitContractEmployee class:

@Entity(name = "interviewbit_contract_employee")
@Table(name = "interviewbit_contract_employee")
@NoArgsConstructor
@AllArgsConstructor
public class InterviewBitContractEmployee extends InterviewBitEmployee {
   private LocalDate contractStartDate;
   private LocalDate contractEndDate;
   private String agencyName;
}
InterviewBitPermanentEmployee class:

@Entity(name = "interviewbit_permanent_employee")
@Table(name = "interviewbit_permanent_employee")
@NoArgsConstructor
@AllArgsConstructor
public class InterviewBitPermanentEmployee extends InterviewBitEmployee {
   private LocalDate workStartDate;
   private int numberOfLeaves;
}
Disadvantages:

This type of strategy offers less performance due to the need for additional joins to get the data.
This strategy is not supported by all JPA providers.
Ordering is tricky in some cases since it is done based on a class and later by the ordering criteria.

Question - 92 : - Can you tell something about Named SQL Query

Answer - 92 : -

A named SQL query is an expression represented in the form of a table. Here, SQL expressions to select/retrieve rows and columns from one or more tables in one or more databases can be specified. This is like using aliases to the queries.

In hibernate, we can make use of @NameQueries and @NameQuery annotations.

@NameQueries annotation is used for defining multiple named queries.
@NameQuery annotation is used for defining a single named query.
Code Snippet: We can define Named Query as shown below

@NamedQueries(  
   {  
       @NamedQuery(  
       name = "findIBEmployeeByFullName",  
       query = "from InterviewBitEmployee e where e.fullName = :fullName"  
       )  
   }  
)  
:fullName refers to the parameter that is programmer defined and can be set using the query.setParameter method while using the named query.

Usage:

TypedQuery query = session.getNamedQuery("findIBEmployeeByFullName");    
query.setParameter("fullName","Hibernate");   
List ibEmployees = query.getResultList();
The getNamedQuery method takes the name of the named query and returns the query instance.

Question - 93 : - What are the benefits of NamedQuery?

Answer - 93 : -

In order to understand the benefits of NamedQuery, let's first understand the disadvantage of HQL and SQL. The main disadvantage of having HQL and SQL scattered across data access objects is that it makes the code unreadable. Hence, as good practice, it is recommended to group all HQL and SQL codes in one place and use only their reference in the actual data access code. In order to achieve this, Hibernate gives us named queries.

A named query is a statically defined query with a predefined unchangeable query string. They are validated when the session factory is created, thus making the application fail fast in case of an error.

Question - 94 : - What does session.lock() method in hibernate do?

Answer - 94 : -

session.lock() method is used to reattach a detached object to the session. session.lock() method does not check for any data synchronization between the database and the object in the persistence context and hence this reattachment might lead to loss of data synchronization.

Question - 95 : - What is lazy loading in hibernate?

Answer - 95 : -

Lazy loading in hibernate improves the performance. It loads the child objects on demand.

Since Hibernate 3, lazy loading is enabled by default, and you don't need to do lazy="true". It means not to load the child objects when the parent is loaded.

Question - 96 : - Is it possible to perform collection mapping with One-to-One and Many-to-One?

Answer - 96 : -

No, collection mapping can only be performed with One-to-Many and Many-to-Many.

Question - 97 : - How many types of association mapping are possible in hibernate?

Answer - 97 : -

There can be 4 types of association mapping in hibernate.

  • One to One
  • One to Many
  • Many to One
  • Many to Many

Question - 98 : - How to make an immutable class in hibernate?

Answer - 98 : -

If you mark a class as mutable="false", the class will be treated as an immutable class. By default, it is mutable="true".

Question - 99 : - What are the inheritance mapping strategies?

Answer - 99 : -

There are 3 ways of inheritance mapping in hibernate.

  • Table per hierarchy
  • Table per concrete class
  • Table per subclass

Question - 100 : - What are the states of the object in hibernate?

Answer - 100 : -

There are 3 states of the object (instance) in hibernate.

  • Transient: The object is in a transient state if it is just created but has no primary key (identifier) and not associated with a session.
  • Persistent: The object is in a persistent state if a session is open, and you just saved the instance in the database or retrieved the instance from the database.
  • Detached: The object is in a detached state if a session is closed. After detached state, the object comes to persistent state if you call lock() or update() method.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners