-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
127 lines (97 loc) · 3.07 KB
/
Test.java
File metadata and controls
127 lines (97 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.jwt.hibernate.Employee;
import com.jwt.hibernate.util.HibernateUtil;
public class Test {
public static void main(String[] args) {
Main main = new Main();
main.saveEmployee("Mukesh", "CEO", 100000, 893965);
main.saveEmployee("Ravi", "Manager", 50000, 996654);
main.saveEmployee("Amit", "PM", 45000, 93445);
main.retriveEmployee();
main.deleteEmployee();
main.updateEmployee();
}
public void saveEmployee(String name, String city, int sal, int phone) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Employee employee = new Employee();
employee.setName("name");
employee.setSal(sal);
employee.setCity("city");
employee.setPhone(phone);
session.save(employee);
transaction.commit();
System.out.println("Records inserted sucessessfully");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void retriveEmployee()
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
List employee = session.createQuery("from Employee").list();
for (Iterator iterator = employee.iterator(); iterator.hasNext();) {
Employee employee1 = (Employee) iterator.next();
System.out.println(employee1.getName() + " "
+ employee1.getCity() + " " + employee1.getSal()
+ " " + employee1.getPhone());
}
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void deleteEmployee() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
String queryString = "from Employee where phone = :phone";
Query query = session.createQuery(queryString);
query.setInteger("phone", 893965);
Employee employee = (Employee) query.uniqueResult();
session.delete(employee);
System.out.println("Employee records deleted!");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void updateEmployee() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
String queryString = "from Employee where sal = :sal";
Query query = session.createQuery(queryString);
query.setInteger("sal", 50000);
Employee employee = (Employee) query.uniqueResult();
employee.setSal(60000);
session.update(employee);
System.out.println("Employee records updated!");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}