From c5ac23be37a214de77a2fdcdea80c648a0834a6b Mon Sep 17 00:00:00 2001 From: codemakerai-dev Date: Sat, 23 Sep 2023 23:37:58 -0700 Subject: [PATCH 1/2] Demo --- example/OrderDao.java | 110 ++++++++++++++++++++++++++++++++++++++++ example/PaymentDao.java | 47 +++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 example/OrderDao.java create mode 100644 example/PaymentDao.java diff --git a/example/OrderDao.java b/example/OrderDao.java new file mode 100644 index 0000000..35775f2 --- /dev/null +++ b/example/OrderDao.java @@ -0,0 +1,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; + } + } +} diff --git a/example/PaymentDao.java b/example/PaymentDao.java new file mode 100644 index 0000000..6dfe035 --- /dev/null +++ b/example/PaymentDao.java @@ -0,0 +1,47 @@ +public class PaymentDao { + + private static final Logger logger = LoggerFactory.getLogger(PaymentDao.class); + + private final Session session; + + public PaymentDao(Session session) { + this.session = session; + } + + /** + * Saves the given {@link Payment} in the database. + * + * @param payment the {@link Payment} to save + * @throws HibernateException if an error occurs while saving the payment + */ + public void save(Payment payment) { + + } + + /** + * Updates the given Payment object in the database. + * + * @param payment The Payment object to be updated. + * @throws HibernateException If an error occurs while updating the payment. + */ + public void update(Payment payment) { + } + + /** + * Retrives the {@link Payment} from the database transactionally. + * + * @param paymentId the {@link Payment} to update + * @throws HibernateException if an error occurs while saving the payment + */ + public Payment get(String id) { + } + + /** + * Deletes the given {@link Payment} to the database. + * + * @param payment the {@link Payment} to update + * @throws HibernateException if an error occurs while saving the payment + */ + public void delete(Payment payment) { + } +} \ No newline at end of file From 1c1b15bfa4edf3ccc05270081dc8e10073910326 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 18:19:38 +0000 Subject: [PATCH 2/2] Updated PR base on comments from code review by codemakerai-dev --- example/OrderDao.java | 8 +++++++- example/PaymentDao.java | 22 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/example/OrderDao.java b/example/OrderDao.java index 35775f2..a5bd73b 100644 --- a/example/OrderDao.java +++ b/example/OrderDao.java @@ -35,11 +35,17 @@ public OrderDao(Session session) { */ public void save(Order order) { checkNotNull(order, "Order cannot be null"); - + + Transaction transaction = null; try { + transaction = session.beginTransaction(); session.save(order); + transaction.commit(); logger.info("Order saved successfully"); } catch (HibernateException e) { + if (transaction != null) { + transaction.rollback(); + } logger.error("Error occurred while saving order", e); throw e; } diff --git a/example/PaymentDao.java b/example/PaymentDao.java index 6dfe035..ac5c64e 100644 --- a/example/PaymentDao.java +++ b/example/PaymentDao.java @@ -13,9 +13,21 @@ public PaymentDao(Session session) { * * @param payment the {@link Payment} to save * @throws HibernateException if an error occurs while saving the payment + * @throws IllegalArgumentException if payment is null */ public void save(Payment payment) { - + if (payment == null) { + throw new IllegalArgumentException("Payment cannot be null"); + } + + try { + Transaction transaction = session.beginTransaction(); + session.save(payment); + transaction.commit(); + logger.info("Payment saved successfully"); + } catch (Exception e) { + logger.error("Error occurred while saving payment: " + e.getMessage()); + } } /** @@ -25,6 +37,14 @@ public void save(Payment payment) { * @throws HibernateException If an error occurs while updating the payment. */ public void update(Payment payment) { + try { + Transaction transaction = session.beginTransaction(); + session.update(payment); + transaction.commit(); + logger.info("Payment updated successfully"); + } catch (Exception e) { + logger.error("Error occurred while updating payment: " + e.getMessage()); + } } /**