From 2fed18e047e6bdbc278818248e146b08ff9ea781 Mon Sep 17 00:00:00 2001 From: Oleksandr Samoylenko Date: Tue, 2 Apr 2024 14:40:54 +0300 Subject: [PATCH] Solution was added --- pom.xml | 4 +- .../basesyntax/dao/impl/CommentDaoImpl.java | 58 ++++++++++++++++-- .../basesyntax/dao/impl/MessageDaoImpl.java | 58 ++++++++++++++++-- .../dao/impl/MessageDetailsDaoImpl.java | 28 ++++++++- .../basesyntax/dao/impl/SmileDaoImpl.java | 40 ++++++++++++- .../core/basesyntax/dao/impl/UserDaoImpl.java | 59 +++++++++++++++++-- .../java/core/basesyntax/model/Comment.java | 7 +++ .../java/core/basesyntax/model/Message.java | 13 ++++ .../core/basesyntax/model/MessageDetails.java | 9 +++ .../java/core/basesyntax/model/Smile.java | 2 + src/main/java/core/basesyntax/model/User.java | 13 ++++ src/main/resources/hibernate.cfg.xml | 12 ++-- 12 files changed, 280 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index 774bb70e9..4ee6272f4 100644 --- a/pom.xml +++ b/pom.xml @@ -25,9 +25,9 @@ test - org.hibernate + org.hibernate.orm hibernate-core - 6.2.7.Final + 6.4.4.Final mysql diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index 5176c951f..e164c4d22 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) { @@ -12,21 +14,69 @@ public CommentDaoImpl(SessionFactory sessionFactory) { @Override public Comment create(Comment entity) { - return null; + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException(String.format("Can't add comment to DB ", entity), e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @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( + String.format("Cant get comment by id = %d from DB", id), e); + } } @Override public List getAll() { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.createQuery("FROM Comment", Comment.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get all smiles from DB ", e); + } finally { + if (session != null) { + session.close(); + } + } } @Override public void remove(Comment entity) { - + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove comment from DB " + entity, e); + } 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..cb5e00f29 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) { @@ -12,21 +14,69 @@ public MessageDaoImpl(SessionFactory sessionFactory) { @Override public Message create(Message entity) { - return null; + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException(String.format("Can't add message to DB ", entity), e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @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( + String.format("Cant get message by id = %d from DB", id), e); + } } @Override public List getAll() { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.createQuery("FROM Message", Message.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get all smiles from DB ", e); + } finally { + if (session != null) { + session.close(); + } + } } @Override public void remove(Message entity) { - + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove message from DB " + entity, e); + } 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..547e3076c 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) { @@ -11,11 +13,33 @@ public MessageDetailsDaoImpl(SessionFactory sessionFactory) { @Override public MessageDetails create(MessageDetails entity) { - return null; + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add message details to DB " + entity, e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @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( + String.format("Cant get message details by id = %d from DB", id), e); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java index 559248cda..5b317b192 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) { @@ -12,16 +14,48 @@ public SmileDaoImpl(SessionFactory sessionFactory) { @Override public Smile create(Smile entity) { - return null; + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add smile to DB " + entity, e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @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( + String.format("Cant get smile by id = %d from DB", id), e); + } } @Override public List getAll() { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.createQuery("FROM Smile", Smile.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get all smiles from DB ", e); + } finally { + if (session != null) { + session.close(); + } + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 338d8248b..edbefdae8 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) { @@ -12,21 +14,70 @@ public UserDaoImpl(SessionFactory sessionFactory) { @Override public User create(User entity) { - return null; + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add user to DB " + entity, e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @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( + String.format("Cant get user by id = %d from DB", id), e); + } } @Override public List getAll() { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.createQuery("FROM User", User.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get all smiles from DB ", e); + } finally { + if (session != null) { + session.close(); + } + } } @Override public void remove(User entity) { - + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove user from DB " + entity, e); + } 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..0ebf845d9 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +1,22 @@ 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 jakarta.persistence.Table; import java.util.List; +@Entity +@Table(name = "comments") public class Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + @OneToMany(fetch = FetchType.LAZY) 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..355b465d3 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.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; + +@Entity +@Table(name = "messages") public class Message { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + @OneToOne(cascade = CascadeType.ALL) 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..11895cbe1 100644 --- a/src/main/java/core/basesyntax/model/MessageDetails.java +++ b/src/main/java/core/basesyntax/model/MessageDetails.java @@ -1,8 +1,17 @@ package core.basesyntax.model; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; import java.time.LocalDateTime; +@Entity +@Table(name = "message_details") 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/Smile.java b/src/main/java/core/basesyntax/model/Smile.java index 4395eb0f7..9218c0f8d 100644 --- a/src/main/java/core/basesyntax/model/Smile.java +++ b/src/main/java/core/basesyntax/model/Smile.java @@ -4,8 +4,10 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.Table; @Entity +@Table(name = "smiles") public class Smile { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/core/basesyntax/model/User.java b/src/main/java/core/basesyntax/model/User.java index 0141eef00..ac9f3dc2b 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.CascadeType; +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 jakarta.persistence.Table; import java.util.List; +@Entity +@Table(name = "users") public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; + @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, 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..073eb39d7 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -6,13 +6,17 @@ org.hibernate.dialect.MySQL8Dialect - jdbc:mysql://localhost/cinema?serverTimezone=UTC + jdbc:mysql://localhost/ticket_app?serverTimezone=UTC com.mysql.cj.jdbc.Driver - ma_admin - 1234 + root + password true update - + + + + +