Skip to content

Commit feccfd7

Browse files
author
Juraj Veverka
committed
added proper postgesql setup, implemented delete
1 parent 051a2fb commit feccfd7

File tree

6 files changed

+83
-11
lines changed

6 files changed

+83
-11
lines changed

hibernate-demo/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ Create user to access the database
2626
./createdb username
2727
./psql -U username
2828
\password
29+
2930
# set password for username
3031
SELECT * FROM pg_catalog.pg_tables;
32+
3133
# create database userdata
3234
create database userdata;
35+
GRANT ALL PRIVILEGES ON DATABASE userdata TO username;
36+
37+
# select data from userdata database and table userdata
38+
\c userdata;
39+
select * from userdata;
3340
```

hibernate-demo/src/main/java/itx/examples/hibernate/App.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import itx.examples.hibernate.entities.UserData;
44
import itx.examples.hibernate.services.DataAccessService;
55
import itx.examples.hibernate.services.DataAccessServiceImpl;
6+
import itx.examples.hibernate.utils.HibernateUtil;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
89

@@ -12,16 +13,26 @@ public class App {
1213

1314
final private static Logger LOG = LoggerFactory.getLogger(App.class);
1415

15-
public static void main(String[] args) {
16-
DataAccessService dataAccessService = new DataAccessServiceImpl();
16+
public static void main(String[] args) throws Exception {
17+
DataAccessService dataAccessService = new DataAccessServiceImpl(HibernateUtil.getPostgresql10Properties());
18+
19+
//1. create and save new user
1720
UserData userData = new UserData("Juraj", "Veverka", "[email protected]");
1821
dataAccessService.saveUserData(userData);
1922
LOG.info("UserData saved");
23+
24+
//2. get all users
2025
Collection<UserData> userDataCollection = dataAccessService.getUserData();
2126
LOG.info("UserData [{}]", userDataCollection.size());
27+
28+
//3. delete all users
2229
userDataCollection.forEach(u->{
2330
LOG.info("UserData: {}", u);
31+
dataAccessService.deleteUserData(u.getId());
2432
});
33+
34+
//4. close the service
35+
dataAccessService.close();
2536
}
2637

2738
}

hibernate-demo/src/main/java/itx/examples/hibernate/entities/UserData.java

+15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class UserData {
2020
@Column(name = "email")
2121
private String email;
2222

23+
public UserData(long id) {
24+
this.id = id;
25+
}
26+
2327
public UserData() {
2428
}
2529

@@ -67,4 +71,15 @@ public String getEmail() {
6771
public void setEmail(String email) {
6872
this.email = email;
6973
}
74+
75+
@Override
76+
public String toString() {
77+
return "UserData{" +
78+
"id=" + id +
79+
", firstName='" + firstName + '\'' +
80+
", lastName='" + lastName + '\'' +
81+
", email='" + email + '\'' +
82+
'}';
83+
}
84+
7085
}

hibernate-demo/src/main/java/itx/examples/hibernate/services/DataAccessService.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
import java.util.Collection;
66

7-
public interface DataAccessService {
7+
public interface DataAccessService extends AutoCloseable {
88

99
void saveUserData(UserData userData);
1010

1111
Collection<UserData> getUserData();
1212

13+
void deleteUserData(long id);
14+
1315
}

hibernate-demo/src/main/java/itx/examples/hibernate/services/DataAccessServiceImpl.java

+38-4
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,70 @@
55
import org.hibernate.Session;
66
import org.hibernate.SessionFactory;
77
import org.hibernate.Transaction;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
810

911
import java.util.Collection;
12+
import java.util.Properties;
1013

1114
public class DataAccessServiceImpl implements DataAccessService {
1215

16+
final private static Logger LOG = LoggerFactory.getLogger(DataAccessServiceImpl.class);
17+
1318
private final SessionFactory sessionFactory;
1419

15-
public DataAccessServiceImpl() {
16-
this.sessionFactory = HibernateUtil.getSessionFactory();
20+
public DataAccessServiceImpl(Properties properties) {
21+
this.sessionFactory = HibernateUtil.getSessionFactory(properties);
1722
}
1823

1924
@Override
2025
public void saveUserData(UserData userData) {
2126
Transaction transaction = null;
22-
try(Session session = this.sessionFactory.openSession()) {
27+
try (Session session = this.sessionFactory.openSession()) {
2328
transaction = session.beginTransaction();
29+
LOG.info("save user data, TX started");
2430
session.save(userData);
2531
transaction.commit();
32+
LOG.info("save user data, TX committed");
2633
} catch (Exception e) {
34+
LOG.error("Error: ", e);
2735
if (transaction != null) {
36+
LOG.info("user data rollback, TX rollback");
2837
transaction.rollback();
2938
}
3039
}
3140
}
3241

3342
@Override
3443
public Collection<UserData> getUserData() {
35-
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
44+
try (Session session = this.sessionFactory.openSession()) {
3645
return session.createQuery("from UserData", UserData.class).list();
3746
}
3847
}
3948

49+
@Override
50+
public void deleteUserData(long id) {
51+
Transaction transaction = null;
52+
try (Session session = this.sessionFactory.openSession()) {
53+
transaction = session.beginTransaction();
54+
LOG.info("delete user data id={}, TX started", id);
55+
//Delete a transient object
56+
UserData userData = new UserData(id);
57+
session.delete(userData);
58+
transaction.commit();
59+
LOG.info("delete user data id={}, TX committed", id);
60+
} catch (Exception e) {
61+
LOG.error("Error: ", e);
62+
if (transaction != null) {
63+
LOG.info("user data rollback, TX rollback");
64+
transaction.rollback();
65+
}
66+
}
67+
}
68+
69+
@Override
70+
public void close() throws Exception {
71+
this.sessionFactory.close();
72+
}
73+
4074
}

hibernate-demo/src/main/java/itx/examples/hibernate/utils/HibernateUtil.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ public final class HibernateUtil {
1414
private HibernateUtil() {
1515
}
1616

17-
public static SessionFactory getSessionFactory() {
18-
Configuration configuration = new Configuration();
17+
public static Properties getPostgresql10Properties() {
1918
Properties properties = new Properties();
20-
2119
properties.put(Environment.DRIVER, "org.postgresql.Driver");
2220
// jdbc:postgresql://host:port/database
2321
properties.put(Environment.URL, "jdbc:postgresql://127.0.0.1:5432/userdata");
@@ -26,7 +24,12 @@ public static SessionFactory getSessionFactory() {
2624
properties.put(Environment.DIALECT, "org.hibernate.dialect.PostgreSQL10Dialect");
2725
properties.put(Environment.SHOW_SQL, "true");
2826
properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
29-
properties.put(Environment.HBM2DDL_AUTO, "create-drop");
27+
properties.put(Environment.HBM2DDL_AUTO, "update");
28+
return properties;
29+
}
30+
31+
public static SessionFactory getSessionFactory(Properties properties) {
32+
Configuration configuration = new Configuration();
3033

3134
configuration.setProperties(properties);
3235
configuration.addAnnotatedClass(UserData.class);

0 commit comments

Comments
 (0)