|
| 1 | +package ai.codemaker.demo.dao; |
| 2 | + |
| 3 | +import org.hibernate.HibernateException; |
| 4 | +import org.hibernate.Session; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | + |
| 8 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Date; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public class OrderDao { |
| 15 | + |
| 16 | + private final Session session; |
| 17 | + |
| 18 | + private static final Logger logger = LoggerFactory.getLogger(OrderDao.class); |
| 19 | + |
| 20 | + /** |
| 21 | + * Constructor for OrderDao class. |
| 22 | + * |
| 23 | + * @param session The session to be used for the OrderDao. |
| 24 | + */ |
| 25 | + public OrderDao(Session session) { |
| 26 | + this.session = session; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Saves the given {@link Order} in the database. |
| 31 | + * |
| 32 | + * @param order the {@link Order} to save |
| 33 | + * @throws NullPointerException If the order object is null. |
| 34 | + * @throws HibernateException if an error occurs while saving the order |
| 35 | + */ |
| 36 | + public void save(Order order) { |
| 37 | + checkNotNull(order, "Order cannot be null"); |
| 38 | + |
| 39 | + try { |
| 40 | + session.save(order); |
| 41 | + logger.info("Order saved successfully"); |
| 42 | + } catch (HibernateException e) { |
| 43 | + logger.error("Error occurred while saving order", e); |
| 44 | + throw e; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Updates the given Order object in the database. |
| 50 | + * |
| 51 | + * @param order The Order object to be updated. |
| 52 | + * @throws NullPointerException If the order object is null. |
| 53 | + * @throws HibernateException If an error occurs while updating the order. |
| 54 | + */ |
| 55 | + public void update(Order order) { |
| 56 | + checkNotNull(order, "Order object cannot be null"); |
| 57 | + |
| 58 | + try { |
| 59 | + session.beginTransaction(); |
| 60 | + session.update(order); |
| 61 | + session.getTransaction().commit(); |
| 62 | + } catch (HibernateException e) { |
| 63 | + logger.error("Error occurred while updating order: " + e.getMessage()); |
| 64 | + session.getTransaction().rollback(); |
| 65 | + throw e; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Retrives the {@link Order} from the database transactionally. |
| 71 | + * |
| 72 | + * @param orderId the {@link Order} to update |
| 73 | + * @throws NullPointerException If the orderId is null. |
| 74 | + * @throws HibernateException if an error occurs while saving the order |
| 75 | + */ |
| 76 | + public void get(String orderId) { |
| 77 | + checkNotNull(orderId, "orderId cannot be null"); |
| 78 | + |
| 79 | + try { |
| 80 | + session.beginTransaction(); |
| 81 | + Order order = session.get(Order.class, orderId); |
| 82 | + session.getTransaction().commit(); |
| 83 | + return order; |
| 84 | + } catch (HibernateException e) { |
| 85 | + logger.error("Error retrieving order from database", e); |
| 86 | + throw e; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Deletes the given order from the database. |
| 92 | + * |
| 93 | + * @param order the order to be deleted |
| 94 | + * @throws NullPointerException if the order is null |
| 95 | + * @throws HibernateException if an error occurs while deleting the order |
| 96 | + */ |
| 97 | + public void delete(Order order) { |
| 98 | + checkNotNull(order, "Order cannot be null"); |
| 99 | + |
| 100 | + try { |
| 101 | + session.beginTransaction(); |
| 102 | + session.delete(order); |
| 103 | + session.getTransaction().commit(); |
| 104 | + } catch (HibernateException e) { |
| 105 | + logger.error("Error occurred while deleting order: {}", e.getMessage()); |
| 106 | + session.getTransaction().rollback(); |
| 107 | + throw e; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments