Hibernate Curd Operation(Insert/Select/Update/Delete) Operation:--------------------------
==============================================================================
// Employee.java
//Employee.hbm.xml
//hibernate.cfg.xml
// EmployeeInsert.java
//EmployeeSelect.java
//EmployeeUpdate.java
// EmployeeDelete.java
//EmployeeAllData.java
======================================================================
step:--
Create the java project
Add jar files for hibernate
Create the Persistent class
Create the mapping file for Persistent class
Create the Configuration file
Create the class that retrieves or stores the persistent object
Run the application
==========================================================================================
jar file required........
antlr-2.7.5H3.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.1.jar
hibernate3.jar
jta.jar
postgresql-9.3-1103.jdbc3.jar
========================================================================
===============================================================================
// Employee.java
package govind;
public class Employee {
private int empid;
private String empname;
private String empaddress;
private String email;
private float empsal;
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public String getEmpaddress() {
return empaddress;
}
public void setEmpaddress(String empaddress) {
this.empaddress = empaddress;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public float getEmpsal() {
return empsal;
}
public void setEmpsal(float empsal) {
this.empsal = empsal;
}
}
================================================================================
//Employee.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="govind.Employee" table="employeehibernate" >
<id name="empid" column="empid" >
<generator class="assigned" />
</id>
<property name="empname" length= "50" column="empname"/>
<property name="empaddress" length= "50" column="empaddress" />
<property name="email" length= "50" column="email"/>
<property name="empsal" length= "20" column="empsal" />
</class>
</hibernate-mapping>
================================================================================
//hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5433/java</property>
<property name="connection.username">postgres</property>
<property name="connection.password">manager</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">30</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="govind/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
================================================================================
// EmployeeInsert.java
package govind;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class EmployeeInsert {
public static void main(String[] args) {
Configuration cf=new Configuration();
cf.configure("/govind/hibernate.cfg.xml");
SessionFactory sf = cf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Employee e=new Employee();
e.setEmpid(1);
e.setEmpname("govind");
e.setEmpaddress("bihar");
e.setEmail("govind@gmail.com");
e.setEmpsal(20000.00f);
session.save(e);
tx.commit();
System.out.println("Insert Data Successfully");
session.close();
sf.close();
}
}
===============================================================================
// EmployeeInsert.java
package govind;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class EmployeeInsert {
public static void main(String[] args) {
Configuration cf=new Configuration();
cf.configure("/govind/hibernate.cfg.xml");
SessionFactory sf = cf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Employee e=new Employee();
e.setEmpid(1);
e.setEmpname("govind");
e.setEmpaddress("bihar");
e.setEmail("govind@gmail.com");
e.setEmpsal(20000.00f);
session.save(e);
tx.commit();
System.out.println("Insert Data Successfully");
session.close();
sf.close();
}
}
===============================================================================
// EmployeeSelect.java
package govind;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class EmployeeSelect {
public static void main(String[] args) {
Configuration cf=new Configuration();
cf.configure("/govind/hibernate.cfg.xml");
SessionFactory sf = cf.buildSessionFactory();
Session session = sf.openSession();
//Transaction tx = session.beginTransaction();//no need
Employee e = (Employee)session.load(Employee.class, new Integer(3));//if emp id is not available inside database then throw ObjectNotFoundException
//Employee e = (Employee)session.get(Employee.class, new Integer(10));//if emp id is not available inside database then throw NullPointerException
System.out.println(e.getEmpid());
System.out.println(e.getEmpname());
System.out.println(e.getEmpaddress());
System.out.println(e.getEmail());
System.out.println(e.getEmpsal());
session.close();
//tx.commit();//no need
System.out.println("Retrieve data Successfully based on id");
}
}
================================================================================
//EmployeeUpdate.java
package govind;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class EmployeeUpdate {
public static void main(String[] args) {
Configuration cf=new Configuration();
cf.configure("/govind/hibernate.cfg.xml");
SessionFactory sf = cf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Employee e = (Employee)session.get(Employee.class, new Integer(1));
e.setEmpname("govindkhan");
e.setEmpsal(45000.00f);
session.update(e);
session.flush();
tx.commit();
session.close();
System.out.println("Update data successfully");
}
}
================================================================================
// EmployeeDelete.java
package govind;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class EmployeeDelete {
public static void main(String[] args) {
Configuration cf=new Configuration();
cf.configure("/govind/hibernate.cfg.xml");
SessionFactory sf = cf.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Employee e = (Employee)session.get(Employee.class, new Integer(1));
session.delete(e);
session.flush();
tx.commit();
session.close();
System.out.println("delete data successfully");
}
}
================================================================================
EmployeeAllData.java
package govind;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class EmployeeAllData {
public static void main(String[] args) {
Configuration cf=new Configuration();
cf.configure("/govind/hibernate.cfg.xml");
SessionFactory sf = cf.buildSessionFactory();
Session session = sf.openSession();
//Transaction tx = session.beginTransaction();
Query qry = session.createQuery("from Employee ");//Employee is class name not database name it is not sql it is hql.
List l =qry.list();
System.out.println("Total Number Of Records : "+l.size());
Iterator it = l.iterator();
while(it.hasNext())
{
Employee e = (Employee)it.next();
System.out.println("Employee id : "+e.getEmpid());
System.out.println("Employee Name : "+e.getEmpname());
System.out.println("Employee Address : "+e.getEmpaddress());
System.out.println("Employee Email : "+e.getEmpname());
System.out.println("Employee Salary : "+e.getEmpsal());
System.out.println("----------------------");
}
//tx.commit();
session.close();
System.out.println("Retrieve all data successfully");
}
}
===============================================================================
o/p view:-
case 1:-insertdata
Hibernate: insert into employeehibernate (empname, empaddress, email, empsal, empid) values (?, ?, ?, ?, ?)
Insert Data Successfully
==============================================================================================
case 2:- selectdata based on id
3
Hibernate: select employee0_.empid as empid0_0_, employee0_.empname as empname0_0_, employee0_.empaddress as empaddress0_0_, employee0_.email as email0_0_, employee0_.empsal as empsal0_0_ from employeehibernate employee0_ where employee0_.empid=?
mani
bihar
mani@gmail.com
55000.0
Retrieve data Successfully based on id
===============================================================================================
case 3:- updatedata
Hibernate: select employee0_.empid as empid0_0_, employee0_.empname as empname0_0_, employee0_.empaddress as empaddress0_0_, employee0_.email as email0_0_, employee0_.empsal as empsal0_0_ from employeehibernate employee0_ where employee0_.empid=?
Hibernate: update employeehibernate set empname=?, empaddress=?, email=?, empsal=? where empid=?
Update data successfully
================================================================================================
case 4:deletedata:
Hibernate: select employee0_.empid as empid0_0_, employee0_.empname as empname0_0_, employee0_.empaddress as empaddress0_0_, employee0_.email as email0_0_, employee0_.empsal as empsal0_0_ from employeehibernate employee0_ where employee0_.empid=?
Hibernate: delete from employeehibernate where empid=?
delete data successfully
==============================================================================================
case 5: select all data:
Hibernate: select employee0_.empid as empid0_, employee0_.empname as empname0_, employee0_.empaddress as empaddress0_, employee0_.email as email0_, employee0_.empsal as empsal0_ from employeehibernate employee0_
Total Number Of Records : 11
Employee id : 2
Employee Name : ajit
Employee Address : bihar
Employee Email : ajit
Employee Salary : 50000.0
----------------------
Employee id : 3
Employee Name : mani
Employee Address : bihar
Employee Email : mani
Employee Salary : 55000.0
----------------------
Employee id : 4
Employee Name : sanjay
Employee Address : mp
Employee Email : sanjay
Employee Salary : 51000.0
----------------------
Employee id : 5
Employee Name : rishabh
Employee Address : up
Employee Email : rishabh
Employee Salary : 53000.0
----------------------
Employee id : 6
Employee Name : pooja
Employee Address : mp
Employee Email : pooja
Employee Salary : 54000.0
----------------------
Employee id : 7
Employee Name : shraddha
Employee Address : mp
Employee Email : shraddha
Employee Salary : 58000.0
----------------------
Employee id : 8
Employee Name : saumya
Employee Address : mp
Employee Email : saumya
Employee Salary : 59000.0
----------------------
Employee id : 9
Employee Name : sachin
Employee Address : mp
Employee Email : sachin
Employee Salary : 60000.0
----------------------
Employee id : 10
Employee Name : vivek
Employee Address : mp
Employee Email : vivek
Employee Salary : 60000.0
----------------------
Employee id : 11
Employee Name : ajay
Employee Address : bihar
Employee Email : ajay
Employee Salary : 33000.0
----------------------
Employee id : 1
Employee Name : govind
Employee Address : bihar
Employee Email : govind
Employee Salary : 33000.0
----------------------
Retrieve all data successfully
No comments:
Post a Comment