Hibernate Interview Question

Q1: What is ORM? 

Object Relational Mapping (ORM): ORM is a theoretical approach that provides the concept of converting an object to a DB record and vice-version. If follows are concepts i.e., mapping it will be done in between the java class and the Database table.


Q2: What is Hibernate? what is the feature of Hibernate?

HIBERNATE: Hibernate is an ORM Framework. It follows java API’s like JDBC, JNDI, JTA.

 JDBC: Java Database Connectivity.

 JNDI: Java Naming Directory Interface. 

JTA: Java Transaction API.

And also uses XML (non-Java Technology). 

Hibernate provides an In-built Persistency API to perform Single Row Operations without SQL by the programmer. Like insert (Save(), Delete(), Update()), read (get() or load()). 

 Hibernate supports Transaction Management begin Transaction, Commit, rollback transaction, etc...(in-built). 

 Hibernate supports Connection Pooling (Multiple Connections are Maintained as a memory for re-using). 

 Hibernate provides HQL (Hibernate Query Language) which is DB Independent.

  Dialect is a class/concept that generates SQL queries for Databases. Ex: Oracle Dialect, MYSQL Dialect, etc…. 

 Hibernate Supports collection Mapping and using, like List, Set, Map, etc…

  Hibernate provides combinations of designing of DB tables for Inheritance and Associations.

  Hibernate provides Caching for Reducing DB Network calls between programs to Database. 


Q3: what is the difference between get() and load() method?

Get(): This method defined in session API. It returns DB record to java object based on primary key in serilizable format. 

Example: get method takes two parameters, class notation and primary key value.

 Format 1: get(String, Serilizable) : object. 

Example: object ob=session.get(“com.app.Student”,101); 

Format 2: get(class,Serilizable) : object. 

Example: object ob=ses.get(“Student.class”,101); 

Object ob=null; 

Note: 

1. In case of No data found based on given primary key input, and then get method returns null data.     2. It is also called early loading



Load(): This method hits the session cache to get the object, if the object is found then returns an object, else it returns an exception. 

Exception: ObjectNotFounException: No row with the given identifier exists. 

Example: for load method Student std=(Student)ses.load(student.class,101);

Note: load() also called lazy loading, when we use load() it goes to the database and creates a proxy object and returns to us, actual loading happens at the time when a returned object gets started using. If that time data is not available it throws the exception.

Q4: What is Hibernate Generator?

To generate, a primary key instead of reading from the end-user, either a predefined class or user-defined class is used even though the value is provided by the end client (or) program. Hibernate Generator will omit the value. 

Types of Generators: In Hibernate, Generators are categorized into two types. 

1. Pre-define Generator 

2. User-defined Generator


Pre-define Generator: 

i) Sequence Generator (oracle sequence)

ii) Identity Generator 

iii) increment Generator (max+1). 

iv) hilow Generator, Assigned, native.


User-define Generator: A Programmer can define his own generation logic for the primary key value. 

To define user define generator. 

Step 1: create a class that implement identifier generator. IdentifierGenerator (org.hibernate.id.IdentiferGenerator) 

Step 2: override generation method.

Step 3: Define Generation Logic and return value. 


Q5: What is Hibernate Mapping?

Hibernate Mapping: 

 I) One-to-one (non-collection) 

II) one-to-many(collection dependent) 

III) many-to-many(non-collection) 

IV) many-to-many(collection dependent)

Note: to know more about mapping please visit my Hibernate Mapping blog





Comments