1
1
package com .tamco .hibernate ;
2
2
3
+ import org .hibernate .Criteria ;
3
4
import org .hibernate .Session ;
4
5
import org .hibernate .SessionFactory ;
5
6
import org .hibernate .Transaction ;
6
7
import org .hibernate .cfg .Configuration ;
8
+ import org .hibernate .criterion .Order ;
9
+
10
+ import java .util .List ;
7
11
8
12
public class Hibernate {
9
13
@@ -17,12 +21,51 @@ public static void main(String[] args) {
17
21
configuration .addAnnotatedClass (BankAccount .class );
18
22
sessionFactory = configuration .buildSessionFactory ();
19
23
20
- savePersonWithBankAcounts ();
24
+ // Save some people with bank accounts
25
+ savePeopleWithBankAccounts ();
26
+
27
+ // Order people by salary
28
+ orderPeopleBySalary ();
29
+
30
+ // Clear database
31
+ deletePeople ();
32
+ }
33
+
34
+ private static void deletePeople () {
35
+ Session session = null ;
36
+ Transaction transaction = null ;
37
+
38
+ try {
39
+ session = sessionFactory .openSession ();
40
+ transaction = session .beginTransaction ();
41
+
42
+ Criteria criteria = session .createCriteria (Person .class );
43
+ List <Person > people = criteria .list ();
44
+ for (Person person : people ) {
45
+ session .delete (person );
46
+ }
47
+
48
+ transaction .commit ();
49
+ System .out .println ("The database has been cleared" );
50
+
51
+ } catch (Exception e ) {
52
+ if (transaction != null ) {
53
+ transaction .rollback ();
54
+ }
55
+ e .printStackTrace ();
56
+
57
+ } finally {
58
+ if (session != null ) {
59
+ session .close ();
60
+ System .out .println ("Always close the session after done with database\n ***************************************" );
61
+ }
62
+ }
21
63
}
22
64
23
- private static void savePersonWithBankAcounts () {
65
+ private static void savePeopleWithBankAccounts () {
24
66
Session session = null ;
25
67
Transaction transaction = null ;
68
+
26
69
try {
27
70
session = sessionFactory .openSession ();
28
71
transaction = session .beginTransaction ();
@@ -38,14 +81,19 @@ private static void savePersonWithBankAcounts() {
38
81
session .save (account1 );
39
82
session .save (account2 );
40
83
84
+ Person john = new Person ("John Wick" , 10000 );
85
+ BankAccount account3 = new BankAccount ("100013" , 1000000 );
86
+ account3 .setPerson (john );
87
+ session .save (john );
88
+ session .save (account3 );
89
+
41
90
transaction .commit ();
42
91
System .out .println ("Person with bank accounts has been saved!" );
43
92
44
93
} catch (Exception e ) {
45
94
if (transaction != null ) {
46
95
transaction .rollback ();
47
96
}
48
- e .printStackTrace ();
49
97
System .out .println ("Person with bank accounts save failed! " + e .getMessage ());
50
98
51
99
} finally {
@@ -55,4 +103,35 @@ private static void savePersonWithBankAcounts() {
55
103
System .out .println ("Always close the session after done with database\n ***************************************" );
56
104
}
57
105
}
106
+
107
+ private static void orderPeopleBySalary () {
108
+ Session session = null ;
109
+ Transaction transaction = null ;
110
+
111
+ try {
112
+ session = sessionFactory .openSession ();
113
+ transaction = session .beginTransaction ();
114
+
115
+ Criteria criteria = session .createCriteria (Person .class );
116
+ criteria .addOrder (Order .desc ("salary" ));
117
+ List <Person > people = criteria .list ();
118
+ for (Person person : people ) {
119
+ System .out .println ("Person[" + person .getName () + ", " + person .getSalary () + "]" );
120
+ }
121
+
122
+ transaction .commit ();
123
+ System .out .println ("People have been ordered" );
124
+
125
+ } catch (Exception e ) {
126
+ if (transaction != null ) {
127
+ transaction .rollback ();
128
+ }
129
+
130
+ } finally {
131
+ if (session != null ) {
132
+ session .close ();
133
+ }
134
+ System .out .println ("Always close the session after done with database\n ***************************************" );
135
+ }
136
+ }
58
137
}
0 commit comments