Software Developer
This is how to hibernate connects to the relational database to execute queries:
Hibernate follows the layered architecture and has the following layers:
This is the first object one has to create to establish a database connection using hibernate. It should be ideally created once, during application initialization. The configuration object provides the following:
Configuration object created in step 1 is used to create a session factory object which makes hibernate configuration ready using the provided configuration file and makes way for session object to be created. Since the session factory is a heavy object, it is usually created once during the starting phase of the application. There is a need for multiple session factory object in case connections to multiple databases needs to be established. Also, the session factory is a thread-safe object.
The session object establishes a physical connection with the database. It is a lightweight object and should be created each time interaction with the database is required. If an object needs to be persisted or needs to be retrieved, it can only be done using the session object. Session objects should be closed as soon as the required operation is completed because they do not thread-safe.
It is an optional object and represents a unit of work done with the database. A transaction object ensures that either all operations must execute or none of them must execute. It is a single-threaded and short-lived object.
This object uses structured query language (SQL) or Hibernate Query Language (HQL) to fetch data from the database and instantiate objects. A Query object can be used to limit output returned from the query, bind query parameters, and execute the query.
Here we will execute some queries that will make things more clear. Let us consider an Entity Employee having class structured as:
Package com.edubca.hibernatetest;
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable
{
private static final long serialVersionUID = -1798070786993123455L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "empID")
private Integer empID;
@Column(name = "NAME")
private String empName;
@Column(name = "SALARY")
private Integer salary;
//Getters and setters
}
Hibernate requires an XML file called hibernate.cfg.xml that looks like:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
<property name="hibernate.connection.password"> edubca</property>
<property name="hibernate.connection.username">edubcauser</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.edubca.hibernatetest.Employee"></mapping>
</session-factory>
</hibernate-configuration>
Below is the code to show how insertion and retrieval take place into the database using hibernate:
//Create Configuration object
Configuration con=new AnnotationConfiguration().configure(new File("hibernate.cgf.xml"));
// create session factory using configuration
SessionFactory fact=conf.buildSessionFactory();
//get session from session factory
Session session=fact.openSession();
//Instantiate and populate Employee entity object
Employee emp=new Employee();
emp.setempID(1);
emp.setempName(“Yash”);
emp.setSalary(40000);
Employee emp1=new Employee();
emp1.setempID(2);
emp1.setempName(“Aman”);
emp1.setSalary(42000);
//persist emp object
session.save(emp);
//persist emp1 object
session.save(emp1);
//retrieve data from database
Query query=session.createQuery(“from Employee”);
List<Employee> list= query.list();
For(Employee e : list){
System.out.println(“Employee with ID ” + e.getempID() + “ has Name ” + e.getempName() + “ has salary ” + e.getsalary());
}