From e7c66c586f3d95c004d3f94f617fbaf951fdba0c Mon Sep 17 00:00:00 2001 From: Igor Gunder Date: Sun, 27 Oct 2024 19:48:21 +0200 Subject: [PATCH] task done --- .../basesyntax/dao/impl/CommentDaoImpl.java | 55 +++++++++++++++-- .../basesyntax/dao/impl/MessageDaoImpl.java | 52 ++++++++++++++-- .../dao/impl/MessageDetailsDaoImpl.java | 27 ++++++++- .../basesyntax/dao/impl/SmileDaoImpl.java | 36 ++++++++++- .../core/basesyntax/dao/impl/UserDaoImpl.java | 60 +++++++++++++++++-- .../java/core/basesyntax/model/Comment.java | 9 ++- .../java/core/basesyntax/model/Message.java | 14 +++++ .../core/basesyntax/model/MessageDetails.java | 7 +++ src/main/java/core/basesyntax/model/User.java | 11 ++++ src/main/resources/hibernate.cfg.xml | 11 +++- 10 files changed, 261 insertions(+), 21 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..82095f4d0 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,66 @@ public CommentDaoImpl(SessionFactory sessionFactory) { @Override public Comment create(Comment entity) { - return null; + Session session = factory.openSession(); + Transaction transaction = null; + try { + transaction = session.beginTransaction(); + session.save(entity); + transaction.commit(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't insert comment entity", e); + } finally { + session.close(); + } } @Override public Comment get(Long id) { - return null; + Session session = factory.openSession(); + try { + Comment comment = session.get(Comment.class, id); + return comment; + } catch (Exception e) { + throw new RuntimeException("Can't get comment by id" + id, e); + } finally { + session.close(); + } } @Override public List getAll() { - return null; + Session session = factory.openSession(); + try { + return session.createQuery("FROM Comment", Comment.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get list of comments", e); + } finally { + session.close(); + } } @Override public void remove(Comment entity) { - + Session session = factory.openSession(); + Transaction transaction = null; + try { + transaction = session.beginTransaction(); + Comment comment = session.get(Comment.class, entity.getId()); + if (comment != null) { + session.remove(comment); + } + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't delete comment entity", e); + } finally { + 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..77cb66aa6 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,63 @@ public MessageDaoImpl(SessionFactory sessionFactory) { @Override public Message create(Message entity) { - return null; + Session session = factory.openSession(); + Transaction transaction = null; + try { + transaction = session.beginTransaction(); + session.save(entity); + transaction.commit(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't insert message entity", e); + } finally { + session.close(); + } } @Override public Message get(Long id) { - return null; + Session session = factory.openSession(); + try { + Message message = session.get(Message.class, id); + return message; + } catch (Exception e) { + throw new RuntimeException("Can't get message by id " + id, e); + } finally { + session.close(); + } } @Override public List getAll() { - return null; + Session session = factory.openSession(); + try { + return session.createQuery("FROM Message", Message.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get list of messages", e); + } finally { + session.close(); + } } @Override public void remove(Message entity) { - + Session session = factory.openSession(); + Transaction transaction = null; + try { + transaction = session.beginTransaction(); + session.remove(session.get(Message.class, entity.getId())); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't delete message entity", e); + } finally { + 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..ef8a700f9 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,32 @@ public MessageDetailsDaoImpl(SessionFactory sessionFactory) { @Override public MessageDetails create(MessageDetails entity) { - return null; + Session session = factory.openSession(); + Transaction transaction = null; + try { + transaction = session.beginTransaction(); + session.save(entity); + transaction.commit(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't insert message details entity", e); + } finally { + session.close(); + } } @Override public MessageDetails get(Long id) { - return null; + Session session = factory.openSession(); + try { + return session.get(MessageDetails.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get message details by id " + id, e); + } finally { + 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..efddc022b 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,44 @@ public SmileDaoImpl(SessionFactory sessionFactory) { @Override public Smile create(Smile entity) { - return null; + Session session = factory.openSession(); + Transaction transaction = null; + try { + transaction = session.beginTransaction(); + session.save(entity); + transaction.commit(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't insert smile entity", e); + } finally { + session.close(); + } } @Override public Smile get(Long id) { - return null; + Session session = factory.openSession(); + try { + return session.get(Smile.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get smail by id " + id, e); + } finally { + session.close(); + } } @Override public List getAll() { - return null; + Session session = factory.openSession(); + try { + return session.createQuery("FROM Smile", Smile.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get list of comments", e); + } finally { + 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..cb24bebd4 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -1,9 +1,12 @@ package core.basesyntax.dao.impl; import core.basesyntax.dao.UserDao; +import core.basesyntax.model.Comment; 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 +15,70 @@ public UserDaoImpl(SessionFactory sessionFactory) { @Override public User create(User entity) { - return null; + Session session = factory.openSession(); + Transaction transaction = null; + try { + transaction = session.beginTransaction(); + if (entity.getComments() != null) { + for (Comment comment : entity.getComments()) { + session.save(comment); + } + } + session.save(entity); + transaction.commit(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't insert user entity", e); + } finally { + session.close(); + } } @Override public User get(Long id) { - return null; + Session session = factory.openSession(); + try { + return session.get(User.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get smail by id " + id, e); + } finally { + session.close(); + } } @Override public List getAll() { - return null; + Session session = factory.openSession(); + try { + return session.createQuery("FROM User", User.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get list of comments", e); + } finally { + session.close(); + } } @Override public void remove(User entity) { - + Session session = factory.openSession(); + Transaction transaction = null; + try { + transaction = session.beginTransaction(); + User user = session.get(User.class, entity.getId()); + if (user != null) { + session.remove(user); + } + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't delete comment entity", e); + } finally { + session.close(); + } } } diff --git a/src/main/java/core/basesyntax/model/Comment.java b/src/main/java/core/basesyntax/model/Comment.java index 9fba0c920..18be155f8 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,16 +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 java.util.ArrayList; import java.util.List; +@Entity public class Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; - private List smiles; + @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER) + private List smiles = new ArrayList<>(); public Long getId() { return id; diff --git a/src/main/java/core/basesyntax/model/Message.java b/src/main/java/core/basesyntax/model/Message.java index 672b5d485..279274dbb 100644 --- a/src/main/java/core/basesyntax/model/Message.java +++ b/src/main/java/core/basesyntax/model/Message.java @@ -1,8 +1,22 @@ 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.OneToOne; + +@Entity public class Message { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @JoinColumn(name = "message_details_id") 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..b51baf73d 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,10 +1,21 @@ 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 java.util.List; +@Entity public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; + @OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) private List comments; public Long getId() { diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index cba4560a4..383b039ad 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -6,13 +6,18 @@ org.hibernate.dialect.MySQL8Dialect - jdbc:mysql://localhost/cinema?serverTimezone=UTC + jdbc:mysql://localhost:3306/test?serverTimezone=UTC com.mysql.cj.jdbc.Driver - ma_admin - 1234 + root + Stas2012 true update + + + + +