From fa5b52ccf3af8e672445f383016f669b68c6aacf Mon Sep 17 00:00:00 2001 From: Mykola Skrypalov Date: Wed, 3 Apr 2024 00:32:34 +0300 Subject: [PATCH] used cascades where needed --- .../basesyntax/dao/impl/CommentDaoImpl.java | 51 +++++++++++++++++-- .../basesyntax/dao/impl/MessageDaoImpl.java | 51 +++++++++++++++++-- .../dao/impl/MessageDetailsDaoImpl.java | 29 +++++++++-- .../basesyntax/dao/impl/SmileDaoImpl.java | 35 +++++++++++-- .../core/basesyntax/dao/impl/UserDaoImpl.java | 51 +++++++++++++++++-- .../java/core/basesyntax/model/Comment.java | 4 ++ .../java/core/basesyntax/model/Message.java | 13 +++++ .../core/basesyntax/model/MessageDetails.java | 7 +++ src/main/java/core/basesyntax/model/User.java | 13 +++++ src/main/resources/hibernate.cfg.xml | 10 ++-- 10 files changed, 239 insertions(+), 25 deletions(-) diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index 5176c951f..5fdb7b6c8 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -3,7 +3,9 @@ import core.basesyntax.dao.CommentDao; import core.basesyntax.model.Comment; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class CommentDaoImpl extends AbstractDao implements CommentDao { public CommentDaoImpl(SessionFactory sessionFactory) { @@ -11,22 +13,61 @@ public CommentDaoImpl(SessionFactory sessionFactory) { } @Override - public Comment create(Comment entity) { - return null; + public Comment create(Comment comment) { + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(comment); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException(); + } finally { + if (session != null) { + session.close(); + } + } + return comment; } @Override public Comment get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(Comment.class, id); + } catch (Exception e) { + throw new RuntimeException(); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("FROM Comment", Comment.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException(); + } } @Override - public void remove(Comment entity) { + public void remove(Comment comment) { + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(comment); + transaction.commit(); + } catch (Exception e) { + throw new RuntimeException(); + } finally { + if (session != null) { + session.close(); + } + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java index 9b899fa95..fcc2a914d 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -3,7 +3,9 @@ import core.basesyntax.dao.MessageDao; import core.basesyntax.model.Message; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class MessageDaoImpl extends AbstractDao implements MessageDao { public MessageDaoImpl(SessionFactory sessionFactory) { @@ -11,22 +13,61 @@ public MessageDaoImpl(SessionFactory sessionFactory) { } @Override - public Message create(Message entity) { - return null; + public Message create(Message message) { + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(message); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException(); + } finally { + if (session != null) { + session.close(); + } + } + return message; } @Override public Message get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(Message.class, id); + } catch (Exception e) { + throw new RuntimeException(); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("FROM Message", Message.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException(); + } } @Override - public void remove(Message entity) { + public void remove(Message message) { + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(message); + transaction.commit(); + } catch (Exception e) { + throw new RuntimeException(); + } finally { + if (session != null) { + session.close(); + } + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java index fb96d35aa..cecb53475 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java @@ -2,7 +2,9 @@ import core.basesyntax.dao.MessageDetailsDao; import core.basesyntax.model.MessageDetails; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class MessageDetailsDaoImpl extends AbstractDao implements MessageDetailsDao { public MessageDetailsDaoImpl(SessionFactory sessionFactory) { @@ -10,12 +12,33 @@ public MessageDetailsDaoImpl(SessionFactory sessionFactory) { } @Override - public MessageDetails create(MessageDetails entity) { - return null; + public MessageDetails create(MessageDetails messageDetails) { + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(messageDetails); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException(); + } finally { + if (session != null) { + session.close(); + } + } + return messageDetails; } @Override public MessageDetails get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(MessageDetails.class, id); + } catch (Exception e) { + throw new RuntimeException(); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java index 559248cda..4df0d42f2 100644 --- a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java @@ -3,7 +3,9 @@ import core.basesyntax.dao.SmileDao; import core.basesyntax.model.Smile; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class SmileDaoImpl extends AbstractDao implements SmileDao { public SmileDaoImpl(SessionFactory sessionFactory) { @@ -11,17 +13,42 @@ public SmileDaoImpl(SessionFactory sessionFactory) { } @Override - public Smile create(Smile entity) { - return null; + public Smile create(Smile smile) { + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(smile); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException(); + } finally { + if (session != null) { + session.close(); + } + } + return smile; } @Override public Smile get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(Smile.class, id); + } catch (Exception e) { + throw new RuntimeException(); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("FROM Smile", Smile.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException(); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 338d8248b..9dc5974a7 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -3,7 +3,9 @@ import core.basesyntax.dao.UserDao; import core.basesyntax.model.User; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class UserDaoImpl extends AbstractDao implements UserDao { public UserDaoImpl(SessionFactory sessionFactory) { @@ -11,22 +13,61 @@ public UserDaoImpl(SessionFactory sessionFactory) { } @Override - public User create(User entity) { - return null; + public User create(User user) { + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(user); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException(); + } finally { + if (session != null) { + session.close(); + } + } + return user; } @Override public User get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(User.class, id); + } catch (Exception e) { + throw new RuntimeException(); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("FROM User", User.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException(); + } } @Override - public void remove(User entity) { + public void remove(User user) { + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(user); + transaction.commit(); + } catch (Exception e) { + throw new RuntimeException(); + } finally { + if (session != null) { + session.close(); + } + } } } diff --git a/src/main/java/core/basesyntax/model/Comment.java b/src/main/java/core/basesyntax/model/Comment.java index 9fba0c920..920d32934 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +1,19 @@ package core.basesyntax.model; +import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; import java.util.List; +@Entity public class Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + @OneToMany private List smiles; public Long getId() { diff --git a/src/main/java/core/basesyntax/model/Message.java b/src/main/java/core/basesyntax/model/Message.java index 672b5d485..fbc2c1362 100644 --- a/src/main/java/core/basesyntax/model/Message.java +++ b/src/main/java/core/basesyntax/model/Message.java @@ -1,8 +1,21 @@ package core.basesyntax.model; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToOne; +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; + +@Entity public class Message { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + @OneToOne + @Cascade({CascadeType.REMOVE, CascadeType.PERSIST}) private MessageDetails messageDetails; public Long getId() { diff --git a/src/main/java/core/basesyntax/model/MessageDetails.java b/src/main/java/core/basesyntax/model/MessageDetails.java index 185023dd5..d988e46f0 100644 --- a/src/main/java/core/basesyntax/model/MessageDetails.java +++ b/src/main/java/core/basesyntax/model/MessageDetails.java @@ -1,8 +1,15 @@ package core.basesyntax.model; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; import java.time.LocalDateTime; +@Entity public class MessageDetails { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String sender; private LocalDateTime sentTime; diff --git a/src/main/java/core/basesyntax/model/User.java b/src/main/java/core/basesyntax/model/User.java index 0141eef00..75abf2089 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,10 +1,23 @@ package core.basesyntax.model; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; import java.util.List; +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; +@Entity public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; + @OneToMany(fetch = FetchType.EAGER) + @Cascade(CascadeType.PERSIST) private List comments; public Long getId() { diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index cba4560a4..64ceca605 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -8,11 +8,15 @@ org.hibernate.dialect.MySQL8Dialect jdbc:mysql://localhost/cinema?serverTimezone=UTC com.mysql.cj.jdbc.Driver - ma_admin - 1234 + root + $miskPass@ true update - + + + + +