Hibernate Interview Questions and Answers
Hibernate or Hibernate ORM is a framework and a mapping tool for Java. It is an object-relational mapping tool that gives an object-oriented domain model to the relational database. The structure is able to handle the problems of object-relational impedance mismatch by the replacement of persistent and direct database access with the functions of high-level object handling functions. There are several Hibernate Interview Questions which must be learned by the students to give their best shot and get the desired job.
Advantages of Hibernate
- Database independent can work with DB2, SQL, MySQL, Oracle, etc.
- Get the benefits of OOP Concepts such as encapsulation, inheritance, etc.
- No requirement of hitting database for related queries, directly cache it and use it
- Easy to write, support and read
- Lazy loading is possible
Most Frequently Asked Hibernate Interview Questions
S.no | Hibernate Save() | SaveorUpdate method() |
---|---|---|
1. | Work to generate a new identifier and then inserting the record into the database | It either can UPDATE or INSERT because as per the existence of record.save() method can be failed with the persistence of the primary key |
2. | Used for bringing a transient object to a persisting state | It can bring both new and existing, i.e., transient and detached into a persistent state or can be said that this method can be used for re-attaching a detached object into Session |
Write the following queries-
1. Use CreateNativeSQL Method for calling a procedure
- Query query = session.createSQLQuery (“CALL GetAllFoos ()”).addEntity;
- List
allFoos= query.list();
2. Use @NamedNativeQueries to call a stored procedure
3. Use @NamedStoreProcedureQuery to call a stored procedure
Use @NamedNativeQueries to call a stored procedure
@NamedNativeQueries({
@NamedNativeQuery(
name = "callGetAllFoos",
query = "CALL GetAllFoos()",
resultClass = Foo.class)
})
@Entity
public class Foo implements Serializable {
// Model definition
}
Use @NamedStoreProcedureQuery to call a stored procedure
@NamedStoredProcedureQuery(
name="GetAllFoos",
procedureName="GetAllFoos",
resultClasses = { Foo.class }
)
@Entity
public class Foo implements Serializable {
// Model Definition
}
In Hibernate, criteria and criterion are two important concepts related to data retrieval from a database.
Criteria: Criteria refers to a query that is used to retrieve data from a database using the Hibernate Criteria API. Criteria provide a flexible and dynamic approach to creating complex queries for retrieving data from a database.
Criterion: Criterion refers to a specific condition that is used to filter the data in a criteria query. For example, you can use a criterion to filter the data based on a specific attribute value, range of values, or a combination of values.
In other words, criteria are the overall query and criterion is a specific condition within the query that is used to filter the data.
S.no | SessionFactory objects | Session |
---|---|---|
1. | There is one SessionFactory object per application | There is one Session object per client |
2. | Used for creation and management and it is threaded safe | Sessions Session is not threaded safe and give CRUD interface to the mapped classes |
Hibernate use SQL databases for regularly testing by regularly handling in the Hibernate product lifecycle or Hibernate developers:
- Oracle 11g, 11g RAC
- MySQL 5.1, 5.5.
- PostgreSQL 8.4, 9.1
- DB2 9.7 or above.
- Microsoft SQL Server 2008.
- Sybase ASE 15.5 (jConnect 6.0)
Hibernate offers retrieval facilities and data query services along with generating SQL calls and relaxing the developer from handling the project manually and conversion of the object of the set of the result. These are the reasons which make Hibernate an object-relational model for the mapping tool for Java language.
Hibernate object states are as follows-
- Detached Object State
- Persistent Object State
- Transient Object State
S.no | Persistent Objects | Transient Objects |
---|---|---|
1. | In this state, the database has the saved objects | The objects are not kept in the database in this state of Hibernation |
2. | Example- An entity from a repository comes under persistent objects | Until persisted, the creation of a new entity is called transients |
Transactions are required for protecting and maintaining the data and their consistency in Hibernate. Java application program framework, can modify several things in the databases, but transactions are needed for retaining the database and for this, there is an availability of Transaction Interface.
Hibernate supports several types of database relationships, including one-to-one, one-to-many, many-to-one, and many-to-many relationships. These relationships can be implemented in Hibernate by using annotations or XML mapping files.
Types of Database Relationships
- One-to-One Relationship
- One-to-Many Relationship
- Many-to-One Relationship
- Many-to-Many Relationship
1. One-to-One Relationship: To implement a one-to-one relationship in Hibernate, we can use the @OneToOne annotation on the primary key side of the relationship. The join column is specified using the @JoinColumn annotation.
Example:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToOne
@JoinColumn(name = "address_id")
private Address address;
}
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String street;
private String city;
private String state;
}
2. One-to-Many Relationship: To implement a one-to-many relationship in Hibernate, we can use the @OneToMany annotation on the many side of the relationship. The mappedBy attribute specifies the inverse side of the relationship.
Example:
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy = "department")
private List
}
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
}
3. Many-to-One Relationship: To implement a many-to-one relationship in Hibernate, we can use the @ManyToOne annotation on the many side of the relationship. The join column is specified using the @JoinColumn annotation.
Example:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
}
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy = "department")
private List
}
4. Many-to-Many Relationship: To implement a many-to-many relationship in Hibernate, we can use the @ManyToMany annotation. A join table is required to map the relationship.
Example:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "employee_project",
joinColumns = { @JoinColumn
Development History
Hibernate ORM was developed by Gavin King in 2001 as a replacement to using EJB2-style entity beans. The latest version is Hibernate ORM 5.4.0, and it was released in December 2018.