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
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
- Eclipse Mars.1 Release (4.5.1)
- Maven
- JDK 1.8
- Hibernate 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
- SessionFactory
- Session
- 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
Great content thanks for sharing this informative blog which provided me technical information keep posting.
ReplyDeleteBest Devops Training in pune
Microsoft azure training in Bangalore
Power bi training in Chennai
I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog.
ReplyDeleterpa training in bangalore
best rpa training in bangalore
rpa training in pune | rpa course in bangalore
rpa training in chennai
smm panel
ReplyDeletesmm panel
İs İlanlari
instagram takipçi satın al
HIRDAVATÇI BURADA
beyazesyateknikservisi.com.tr
servis
tiktok jeton hilesi