Skip to content

Commit 840c369

Browse files
committed
Hibernate demo code
1 parent 8fe788b commit 840c369

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

code/hibernate-demo/db/schema.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ CREATE TABLE IF NOT EXISTS bank_account(
1111
account VARCHAR(36) NOT NULL,
1212
balance INTEGER NOT NULL,
1313
person_id VARCHAR(36) NOT NULL,
14-
FOREIGN KEY (person_id) REFERENCES person(id)
14+
FOREIGN KEY (person_id) REFERENCES person(id),
15+
CONSTRAINT account_unq UNIQUE(account)
1516
) CHARACTER SET utf8;

code/hibernate-demo/src/main/java/com/tamco/hibernate/BankAccount.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class BankAccount {
1313
@GenericGenerator(name = "uuid", strategy = "uuid2")
1414
private String id;
1515

16-
@Column(name = "account")
16+
@Column(name = "account", unique = true)
1717
private String account;
1818

1919
@Column(name = "balance")

code/hibernate-demo/src/main/java/com/tamco/hibernate/Hibernate.java

+82-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.tamco.hibernate;
22

3+
import org.hibernate.Criteria;
34
import org.hibernate.Session;
45
import org.hibernate.SessionFactory;
56
import org.hibernate.Transaction;
67
import org.hibernate.cfg.Configuration;
8+
import org.hibernate.criterion.Order;
9+
10+
import java.util.List;
711

812
public class Hibernate {
913

@@ -17,12 +21,51 @@ public static void main(String[] args) {
1721
configuration.addAnnotatedClass(BankAccount.class);
1822
sessionFactory = configuration.buildSessionFactory();
1923

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+
}
2163
}
2264

23-
private static void savePersonWithBankAcounts() {
65+
private static void savePeopleWithBankAccounts() {
2466
Session session = null;
2567
Transaction transaction = null;
68+
2669
try {
2770
session = sessionFactory.openSession();
2871
transaction = session.beginTransaction();
@@ -38,14 +81,19 @@ private static void savePersonWithBankAcounts() {
3881
session.save(account1);
3982
session.save(account2);
4083

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+
4190
transaction.commit();
4291
System.out.println("Person with bank accounts has been saved!");
4392

4493
} catch (Exception e) {
4594
if (transaction != null) {
4695
transaction.rollback();
4796
}
48-
e.printStackTrace();
4997
System.out.println("Person with bank accounts save failed! " + e.getMessage());
5098

5199
} finally {
@@ -55,4 +103,35 @@ private static void savePersonWithBankAcounts() {
55103
System.out.println("Always close the session after done with database\n***************************************");
56104
}
57105
}
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+
}
58137
}

0 commit comments

Comments
 (0)