From f2292149885b30c89623ef5a8515f7c95ca5f95a Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 17 Jul 2024 00:16:13 +0200 Subject: [PATCH] Done --- .../basesyntax/dao/impl/CommentDaoImpl.java | 54 +++++++++++++++-- .../basesyntax/dao/impl/MessageDaoImpl.java | 59 +++++++++++++++++-- .../dao/impl/MessageDetailsDaoImpl.java | 29 ++++++++- .../basesyntax/dao/impl/SmileDaoImpl.java | 47 ++++++++++++++- .../core/basesyntax/dao/impl/UserDaoImpl.java | 54 +++++++++++++++-- .../java/core/basesyntax/model/Comment.java | 9 +++ .../java/core/basesyntax/model/Message.java | 12 ++++ .../core/basesyntax/model/MessageDetails.java | 7 +++ src/main/java/core/basesyntax/model/User.java | 13 ++++ 9 files changed, 267 insertions(+), 17 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..07b0e6d78 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -2,8 +2,15 @@ import core.basesyntax.dao.CommentDao; import core.basesyntax.model.Comment; +import jakarta.persistence.Query; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; import java.util.List; +import java.util.Optional; +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 +19,60 @@ 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("Can't create entity to DB", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public Comment get(Long id) { - return null; + Optional comment; + try (Session session = factory.openSession()) { + comment = Optional.ofNullable(session.get(Comment.class, id)); + } catch (Exception e) { + throw new RuntimeException("Can't get user from DB", e); + } + return comment.get(); } @Override public List getAll() { - return null; + Session session = factory.openSession(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Comment.class); + Root employeeRoot = criteriaQuery.from(Comment.class); + criteriaQuery.select(employeeRoot); + Query query = session.createQuery(criteriaQuery); + return query.getResultList(); } @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("Can't remove entity from DB", e); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java index 9b899fa95..dee50b240 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -2,8 +2,12 @@ import core.basesyntax.dao.MessageDao; import core.basesyntax.model.Message; +import java.util.ArrayList; import java.util.List; +import java.util.Optional; +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 +16,68 @@ 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("Can't create entity to DB", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public Message get(Long id) { - return null; + Optional message; + try (Session session = factory.openSession()) { + message = Optional.ofNullable(session.get(Message.class, id)); + } catch (Exception e) { + throw new RuntimeException("Can't get user from DB", e); + } + return message.get(); } @Override public List getAll() { - return null; + List messages = new ArrayList<>(); + Optional message; + int i = 1; + try (Session session = factory.openSession()) { + message = Optional.ofNullable(session.get(Message.class, i)); + while (message.isPresent()) { + message = Optional.ofNullable(session.get(Message.class, i)); + messages.add(message.get()); + i++; + message = Optional.ofNullable(session.get(Message.class, i)); + } + } catch (Exception e) { + throw new RuntimeException("Can't get user from DB", e); + } + return messages; } @Override public void remove(Message 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("Can't remove entity from DB", e); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java index fb96d35aa..83f952885 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,34 @@ 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 create entity to DB", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public MessageDetails get(Long id) { - return null; + MessageDetails messageDetails; + try (Session session = factory.openSession()) { + messageDetails = session.get(MessageDetails.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get user from DB", e); + } + return messageDetails; } } diff --git a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java index 559248cda..0f561f857 100644 --- a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java @@ -2,8 +2,12 @@ import core.basesyntax.dao.SmileDao; import core.basesyntax.model.Smile; +import java.util.ArrayList; import java.util.List; +import java.util.Optional; +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 +16,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(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't create entity to DB", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public Smile get(Long id) { - return null; + Optional smile; + try (Session session = factory.openSession()) { + smile = Optional.ofNullable(session.get(Smile.class, id)); + } catch (Exception e) { + throw new RuntimeException("Can't get user from DB", e); + } + return smile.get(); } @Override public List getAll() { - return null; + List smiles = new ArrayList<>(); + Optional smile; + int i = 1; + try (Session session = factory.openSession()) { + smile = Optional.ofNullable(session.get(Smile.class, i)); + while (smile.isPresent()) { + smile = Optional.ofNullable(session.get(Smile.class, i)); + smiles.add(smile.get()); + i++; + smile = Optional.ofNullable(session.get(Smile.class, i)); + } + } catch (Exception e) { + throw new RuntimeException("Can't get user from DB", e); + } + return smiles; } } diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 338d8248b..69e683119 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -2,8 +2,15 @@ import core.basesyntax.dao.UserDao; import core.basesyntax.model.User; +import jakarta.persistence.Query; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; import java.util.List; +import java.util.Optional; +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 +19,60 @@ 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 create entity to DB", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public User get(Long id) { - return null; + Optional user; + try (Session session = factory.openSession()) { + user = Optional.ofNullable(session.get(User.class, id)); + } catch (Exception e) { + throw new RuntimeException("Can't get user from DB", e); + } + return user.get(); } @Override public List getAll() { - return null; + Session session = factory.openSession(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(User.class); + Root employeeRoot = criteriaQuery.from(User.class); + criteriaQuery.select(employeeRoot); + Query query = session.createQuery(criteriaQuery); + return query.getResultList(); } @Override public void remove(User 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("Can't remove entity from DB", e); + } } } diff --git a/src/main/java/core/basesyntax/model/Comment.java b/src/main/java/core/basesyntax/model/Comment.java index 9fba0c920..9b67c3b92 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.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 Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + + @OneToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH, + CascadeType.REFRESH}, fetch = FetchType.EAGER) + 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..efe567bc8 100644 --- a/src/main/java/core/basesyntax/model/Message.java +++ b/src/main/java/core/basesyntax/model/Message.java @@ -1,8 +1,20 @@ 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; + +@Entity 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..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..866f55d4f 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 java.util.List; +@Entity public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; + + @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, + CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.EAGER) private List comments; public Long getId() {