Thursday, December 24, 2015

Hibernate 5 Maven example

Hello!

Here in this post we will be looking into the basic setup of "Hibernate 5".
Hibernate 5 is being recently released to explore its feature we started setting up work space and the desired libraries needed for "Hibernate 5".

Here in this post we will be using
  1. Eclipse Mars.1 Release (4.5.1)
  2. Maven
  3. JDK 1.8
  4. Hibernate 5
  5. MySQL
You can also download/clone the complete workspace from GitHub : - https://github.com/rahulwagh/hibernate5.git

If you are not able to clone the repository than download the complete workspace and import in eclipse using following instruction 

File - > Import - > General - > Existing project into workspace


Maven Dependencies for Hibernate 5 

  
   org.hibernate
   hibernate-core
   5.0.2.Final
  
  
   mysql
   mysql-connector-java
   5.1.37
  
Here in this post we will try to insert an employee entry into the "employee" table which we have created.
Next step is to create an "Employee" @Entity

@Employee
package com.hibernate.tutorial.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "employee")
public class Employee {

 @Id
 @Column(name = "id")
 Long id;

 @Column(name="employee_name")
 String employeeName;

 @Column(name="employee_address")
 String employeeAddress;

 public Employee(Long id, String employeeName, String employeeAddress) {
  this.id = id;
  this.employeeName = employeeName;
  this.employeeAddress = employeeAddress;
 }

 public Employee() {

 }

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getEmployeeName() {
  return employeeName;
 }

 public void setEmployeeName(String employeeName) {
  this.employeeName = employeeName;
 }

 public String getEmployeeAddress() {
  return employeeAddress;
 }

 public void setEmployeeAddress(String employeeAddress) {
  this.employeeAddress = employeeAddress;
 }

}

Next we need to write a code to insert a record into the table. Refer to following code where we have created following instances

  1. SessionFactory 
  2. Session
  3. Transaction
package com.hibernate.tutorial.mainclass;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.hibernate.tutorial.entity.Employee;

public class Hibernate5InsertTest {

 public static void main(String[] args) {
  SessionFactory sessionFactory;
  sessionFactory = new Configuration().configure().buildSessionFactory();

  Session session = sessionFactory.openSession();

  Transaction tx = session.beginTransaction();

  Employee emp = new Employee();
  emp.setId(new Long(1));
  emp.setEmployeeName("Rahul Wagh");
  emp.setEmployeeAddress("Indore, India");
  session.save(emp);
  tx.commit();
  session.close();
 }
}


SQL Script for "employee" table  : -

CREATE TABLE employee(
id INT NOT NULL AUTO_INCREMENT,
employee_name VARCHAR(100) NOT NULL,
employee_address VARCHAR(40) NOT NULL,
 PRIMARY KEY ( id));
Hope this article will help you to setup your Hibernate 5 Workspace.

For any issue please post your comments and leave your feedback

0 comments:

Post a Comment