-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderDao.java
More file actions
110 lines (95 loc) · 3.29 KB
/
OrderDao.java
File metadata and controls
110 lines (95 loc) · 3.29 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
package ai.codemaker.demo.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class OrderDao {
private final Session session;
private static final Logger logger = LoggerFactory.getLogger(OrderDao.class);
/**
* Constructor for OrderDao class.
*
* @param session The session to be used for the OrderDao.
*/
public OrderDao(Session session) {
this.session = session;
}
/**
* Saves the given {@link Order} in the database.
*
* @param order the {@link Order} to save
* @throws NullPointerException If the order object is null.
* @throws HibernateException if an error occurs while saving the order
*/
public void save(Order order) {
checkNotNull(order, "Order cannot be null");
try {
session.save(order);
logger.info("Order saved successfully");
} catch (HibernateException e) {
logger.error("Error occurred while saving order", e);
throw e;
}
}
/**
* Updates the given Order object in the database.
*
* @param order The Order object to be updated.
* @throws NullPointerException If the order object is null.
* @throws HibernateException If an error occurs while updating the order.
*/
public void update(Order order) {
checkNotNull(order, "Order object cannot be null");
try {
session.beginTransaction();
session.update(order);
session.getTransaction().commit();
} catch (HibernateException e) {
logger.error("Error occurred while updating order: " + e.getMessage());
session.getTransaction().rollback();
throw e;
}
}
/**
* Retrives the {@link Order} from the database transactionally.
*
* @param orderId the {@link Order} to update
* @throws NullPointerException If the orderId is null.
* @throws HibernateException if an error occurs while saving the order
*/
public void get(String orderId) {
checkNotNull(orderId, "orderId cannot be null");
try {
session.beginTransaction();
Order order = session.get(Order.class, orderId);
session.getTransaction().commit();
return order;
} catch (HibernateException e) {
logger.error("Error retrieving order from database", e);
throw e;
}
}
/**
* Deletes the given order from the database.
*
* @param order the order to be deleted
* @throws NullPointerException if the order is null
* @throws HibernateException if an error occurs while deleting the order
*/
public void delete(Order order) {
checkNotNull(order, "Order cannot be null");
try {
session.beginTransaction();
session.delete(order);
session.getTransaction().commit();
} catch (HibernateException e) {
logger.error("Error occurred while deleting order: {}", e.getMessage());
session.getTransaction().rollback();
throw e;
}
}
}