Hibernate Interview Questions – Part 3

Hibernate Interview Questions – Part 3

Hibernate Interview Questions – Part 1Part 2

Explain One – to – One Mapping:
Example:
Consider we have 2 tables, Employee and Employee Address. We save the employee details at one table like his name, employee number etc and we save employee address in another table. We will have a one to one relationship for specific employee from employee table to specific records in Employee Address table.

Consider there is an employee named Alpha in Employee table and let the primary key for Alpha be 1. Now the table Employee will have an column that links to Employee Address table and that Employee Address will point to a record in Employee Address table.

Detailed Explanation Here


Explain OneToMany Mapping:
Example:
Consider an employee might have more than one address, So as OnetoMany mapping points out, A single employee can have more than one address in another table. (Detail Here)


Explain ManytoMany Mapping:
let us consider 2 entities – Employee and Address. An employee can have more than one address let us assume permanent and temporary address, Same way more than one employee would have stayed in the temporary address. This scenario shows Employee can have more than one address and an address can have more than one employee. This is ManyToMany mapping. (Details Here)


Explain Table per Concrete Class:
In this method, a table is created for each class in the inheritance. Each table will have its own property columns and also has its base class properties. We will have repeated columns of base class in all sub classes


Explain Table per Class:
In this method, a single table is created for all classes both Base and Subclass. We have to define a column Discriminator column to differentiate object of base and subclass.(Detailed Explanation Here)


Explain Table per Subclass:
This method, creates a table for each subclass. The difference between table per concrete class and Table per subclass is the Inherited tables will not create columns of base class. It uses foreign key and points to base class for base class properties(Detailed Explanation Here)


 

Read More

Hibernate Interview Questions – Part 1

Hibernate Interview Questions – Part 1
(Hibernate Interview Questions Part 2)

What is Hibernate?
It is a open source and light weight ORM  tool
It is used to store, retrieve and manipulate the database.


What is ORM?
ORM stands for Object/Relational Mapping
Matching the object with data in database


What is Impedance Mismatch?
Object oriented applications use relational database to store and retrieve the data.
Object oriented application is in class while relational database represent the data in tabular format.
We can access class using Object but in order to access the data in relational database we need to use query.
The differences between Object Oriented and Relational Database is known as Impedance Mismatch



What are the types of Impedance Mismatch?
Granularity:
Difference between number of classes mapped with database tables
Subtypes:
Differences in relationship among the classes and tables in database
Identity:
Refers to how the objects are differentiated in application and relational database.(In database object is differentiated with Primary key, as in Object model it is distinguished based on object identity and equality)
Association:
Difference in linking classes in application and linking of tables in database.(One entity can have reference to another entity, but in relational database linking is done with help of foreign keys)

We use ORM to overcome impedance mismatch by mapping data representations of Object model having data types to relational data model with SQL data types


What are the Features of Hibernate?
ORM
– matches object model and relational database
Supports inheritance, polymorphism, abstraction and collections framework
Transparent Persistence – changes made in object are automatically detected and updated in database
Database Independent
Provides HQL similar to SQL


Explain Hibernate Architecture:
Consists of,
Configuration class:
 An instance of this class is created once during the initialization. This is used for reading and parsing the properties required to connect database and application
SessionFactory Interface:
Created using an object of Configuration class. This is used to create and open session to communicate with database. This is configured separately for each database being connected
Session Interface:
 Created with SessionFactory object and is used to communicate with database
Transaction Interface:
Created with help of Session Object and is used to perform logical unit of work from database
Query Interface:
Used to create query to perform operations in database
Criteria Interface:
Used to create query to perform database operations with conditions


How to create Hibernate Session?
configure():
 This method loads hibernate.cfg.xml file and initializes the object of Configuration class with configuration properties specified in xml file
getProperties():
 Used to fetch the properties  for configuration that are configured in configure()
applySettings():
Uses serviceRegistryBuilder to apply settings fetched from getProperties().
buildSessionFactory():
 Uses configuration object from Configure() and instantiate new SessionFactory Object. (ServiceRegistry object as parameter)

(Hibernate Interview Questions Part 2)


Read More

Hibernate Database Operations – FirstResult and MaxResult

Hibernate Database Operations – FirstResult and MaxResult

In this article we are going to see FirstResult and MaxResult in hibernate.

FirstResult – Suppose if we like to retrieve records from database from 6th record, then we can set setFirstResult(6).

MaxResult – Suppose if we need to retrieve only 10 records from database, then we can use setMaxResult(10).

FetchSize – This is almost similar to MaxResult, We can set number of rows to be retrieved. The difference is that optimization, Change in way of sending the results (Like chunks, buffered etc) – Not Supported by Most of the Database Drivers.

Now let us see an example,

structure1

hibernate.cfg.xml

Employee.java

NewHibernateUtil.java

Emp_DAO.java

E_Main.java

Here, We are inserting 10 records into the database,

op1

Then we are setting the FirstResult(5), It will print the records from 6. We are also setting MaxResult(3) so it should print the 3 records (Ename6, Ename7, Ename8)

op2

 

 

 

Read More

Criteria Queries Implementation – Hibernate

Criteria Queries Implementation – Hibernate
Hibernate provides org.hibernate.criteria interface to create Criteria Queries.

Consider an example, We have an entity class named “Employee”  then our criteria query will look like,

Criteria criteria = session.createCriteria(Employee.class);

Adding Restrictions:
To use condition-based criteria queries in hibernate application we need to add restrictions to the query. Here let us see some examples of adding restrictions to Hibernate Queries.

Before proceeding to example, let us see some of the standard Restrictions.

.add(Restrictions.eq(Propertyname, object value) – This will compare the property name equals to the object value

.add(Restrictions.gt(Propertyname, object value) – This will return the results when the value of that property is greater than the specified value

.add(Restrictions.lt(Propertyname, object value) – This will return the results when the value of that property is lesser than the specified value

.add(Restrictions.ge(Propertyname, object value) – This will return the results when the value of that property is greater than or equal to the specified value

.add(Restrictions.le(Propertyname, object value) – This will return the results when the value of that property is lesser than or equal to the specified value

.add(Restrictions.like(Propertyname, object value) – This will compare the propertywith specified pattern

.add(Restrictions.between(Propertyname, object low, object high) – This method compares the property within the range of low and high

Now let us see an example,

Structure
structure1

Lets create a table in Database for this example and enter some values,

table

hibernate.cfg.xml

hibernate.reveng.xml

NewHibernateUtil.java

CriteriaExample.java

CriteriaExample.hbm.xml

Controller.java


Output:

This is print the records that matches the city name Berlin

eq

Output 2:

Prints records which has money property value more than 15000

gt

Output 3:

Prints records which has money property value less than or equal to 15000

le

Output 4:

Prints records whose city name property contains “A”

like

 Output 5:

Prints records whose money property value is between 15000 and 22000

between

 

 

Read More

CRUD Operations in Hibernate

CRUD Operations in Hibernate
So far we have seen Joined class, table per hierarchy and Mappings (OneToOne, OneToMany, ManyToMany)

Now let us see the CRUD Operations (Create, Read, Update and Delete)

Structure:
structure

We are going to add 2 employee’s, Retrieve one employee, Update and delete one employee record

hibernate.cfg.xml

NewHibernateUtil.java

employee.java

execution.java

Output:
Inserting Values 

op1

op2

Reading Values
op3

Update Values
op4

op5

Delete Values
op6

op7

Note:
Run each operations separately (comment other operations) – Don’t run all the operations simultaneously.

 

Read More