From bb7f879102f01fcf2b09a1540a020824aab94076 Mon Sep 17 00:00:00 2001 From: Nikita Khilobok Date: Thu, 10 Oct 2024 14:07:40 +0300 Subject: [PATCH] hw is done, please check --- .../java/core/basesyntax/HibernateUtil.java | 2 +- .../basesyntax/dao/impl/CommentDaoImpl.java | 52 +++++++++++++-- .../basesyntax/dao/impl/MessageDaoImpl.java | 63 +++++++++++++++++-- .../dao/impl/MessageDetailsDaoImpl.java | 33 +++++++++- .../basesyntax/dao/impl/SmileDaoImpl.java | 45 ++++++++++++- .../core/basesyntax/dao/impl/UserDaoImpl.java | 63 +++++++++++++++++-- .../java/core/basesyntax/model/Comment.java | 18 ++++++ .../java/core/basesyntax/model/Message.java | 28 +++++++++ .../core/basesyntax/model/MessageDetails.java | 18 ++++++ .../java/core/basesyntax/model/Smile.java | 2 + src/main/java/core/basesyntax/model/User.java | 25 ++++++++ src/main/resources/hibernate.cfg.xml | 13 ++-- 12 files changed, 339 insertions(+), 23 deletions(-) diff --git a/src/main/java/core/basesyntax/HibernateUtil.java b/src/main/java/core/basesyntax/HibernateUtil.java index 06ceb9098..70099d741 100644 --- a/src/main/java/core/basesyntax/HibernateUtil.java +++ b/src/main/java/core/basesyntax/HibernateUtil.java @@ -4,7 +4,7 @@ import org.hibernate.cfg.Configuration; public class HibernateUtil { - private static SessionFactory sessionFactory = initSessionFactory(); + private static final SessionFactory sessionFactory = initSessionFactory(); private HibernateUtil() { } diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index 5176c951f..5b88e6f70 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -2,8 +2,11 @@ import core.basesyntax.dao.CommentDao; import core.basesyntax.model.Comment; +import core.basesyntax.model.Smile; 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 +15,62 @@ public CommentDaoImpl(SessionFactory sessionFactory) { @Override public Comment create(Comment entity) { - return null; + Transaction transaction = null; + try (Session session = factory.openSession()) { + transaction = session.beginTransaction(); + + if (entity.getSmiles() != null) { + for (Smile smile : entity.getSmiles()) { + Smile existingSmile = session.get(Smile.class, smile.getId()); + if (existingSmile == null) { + throw new RuntimeException("Smile with id " + + smile.getId() + + " does not exist"); + } + } + } + + session.persist(entity); + transaction.commit(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Failed to save comment: " + 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("Failed to get comment with id: " + id); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("FROM Comment", Comment.class).list(); + } catch (Exception e) { + throw new RuntimeException("Failed to get all comments", e); + } } @Override public void remove(Comment entity) { - + Transaction transaction = null; + try (Session session = factory.openSession()) { + transaction = session.beginTransaction(); + session.remove(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Failed to remove comment: " + entity); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java index 9b899fa95..b3b89e5a5 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,74 @@ 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(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Failed to save message: " + entity); + } finally { + if (session != null) { + session.close(); + } + } } @Override public Message get(Long id) { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.get(Message.class, id); + } catch (Exception e) { + throw new RuntimeException("Failed to get message with id: " + id); + } finally { + if (session != null) { + session.close(); + } + } } @Override public List getAll() { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.createQuery("FROM Message", Message.class).list(); + } catch (Exception e) { + throw new RuntimeException("Failed to get all messages", 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("Failed to remove message: " + entity); + } 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..d8033cd53 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,38 @@ 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(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Failed to save message details: " + entity); + } finally { + if (session != null) { + session.close(); + } + } } @Override public MessageDetails get(Long id) { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.get(MessageDetails.class, id); + } catch (Exception e) { + throw new RuntimeException("Failed to get message details with id: " + id); + } finally { + if (session != null) { + session.close(); + } + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java index 559248cda..b476e1cc4 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,53 @@ 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(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Failed to save smile: " + entity); + } finally { + if (session != null) { + session.close(); + } + } } @Override public Smile get(Long id) { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.get(Smile.class, id); + } catch (Exception e) { + throw new RuntimeException("Failed to get smile with id: " + id); + } finally { + if (session != null) { + session.close(); + } + } } @Override public List getAll() { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.createQuery("FROM Smile", Smile.class).list(); + } catch (Exception e) { + throw new RuntimeException("Failed to get all smiles", 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..259bc304a 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,74 @@ 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(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Failed to save user: " + entity); + } finally { + if (session != null) { + session.close(); + } + } } @Override public User get(Long id) { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.get(User.class, id); + } catch (Exception e) { + throw new RuntimeException("Failed to get message with id: " + id); + } finally { + if (session != null) { + session.close(); + } + } } @Override public List getAll() { - return null; + Session session = null; + try { + session = factory.openSession(); + return session.createQuery("FROM User", User.class).list(); + } catch (Exception e) { + throw new RuntimeException("Failed to get all users", 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("Failed to remove user: " + entity); + } 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..7ebde0492 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +1,24 @@ package core.basesyntax.model; +import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +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 + @JoinColumn(name = "comment_id") private List smiles; public Long getId() { @@ -35,4 +44,13 @@ public List getSmiles() { public void setSmiles(List smiles) { this.smiles = smiles; } + + @Override + public String toString() { + return "Comment{" + + "id=" + id + + ", content='" + content + '\'' + + ", smiles=" + smiles + + '}'; + } } diff --git a/src/main/java/core/basesyntax/model/Message.java b/src/main/java/core/basesyntax/model/Message.java index 672b5d485..aef1bcc98 100644 --- a/src/main/java/core/basesyntax/model/Message.java +++ b/src/main/java/core/basesyntax/model/Message.java @@ -1,8 +1,27 @@ 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.JoinColumn; +import jakarta.persistence.JoinTable; +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.PERSIST, CascadeType.REMOVE}) + @JoinTable(name = "message_details", + joinColumns = @JoinColumn(name = "message"), + inverseJoinColumns = @JoinColumn(name = "details")) private MessageDetails messageDetails; public Long getId() { @@ -28,4 +47,13 @@ public MessageDetails getMessageDetails() { public void setMessageDetails(MessageDetails messageDetails) { this.messageDetails = messageDetails; } + + @Override + public String toString() { + return "Message{" + + "id=" + id + + ", content='" + content + '\'' + + ", messageDetails=" + messageDetails + + '}'; + } } diff --git a/src/main/java/core/basesyntax/model/MessageDetails.java b/src/main/java/core/basesyntax/model/MessageDetails.java index 185023dd5..9ee8509a5 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; @@ -30,4 +39,13 @@ public LocalDateTime getSentTime() { public void setSentTime(LocalDateTime sentTime) { this.sentTime = sentTime; } + + @Override + public String toString() { + return "MessageDetails{" + + "id=" + id + + ", sender='" + sender + '\'' + + ", sentTime=" + 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..91dbc90ac 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,10 +1,26 @@ 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.JoinColumn; +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(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) + @JoinColumn(name = "user_id") private List comments; public Long getId() { @@ -30,4 +46,13 @@ public List getComments() { public void setComments(List comments) { this.comments = comments; } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", comments=" + comments + + '}'; + } } diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index cba4560a4..c2b96bd35 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -5,14 +5,17 @@ - org.hibernate.dialect.MySQL8Dialect - jdbc:mysql://localhost/cinema?serverTimezone=UTC + jdbc:mysql://localhost/cascades?serverTimezone=UTC com.mysql.cj.jdbc.Driver - ma_admin - 1234 + root + Nk102030! true update - + + + + +