From e9453a487ac35a89d912f0e4431b5f1c82b681b0 Mon Sep 17 00:00:00 2001 From: Khrystyna Date: Tue, 12 Mar 2024 11:56:55 +0200 Subject: [PATCH] implemented methods and added annotations --- .../basesyntax/dao/impl/CommentDaoImpl.java | 64 ++++++++++++++++-- .../basesyntax/dao/impl/MessageDaoImpl.java | 65 +++++++++++++++++-- .../dao/impl/MessageDetailsDaoImpl.java | 27 +++++++- .../basesyntax/dao/impl/SmileDaoImpl.java | 46 ++++++++++++- .../core/basesyntax/dao/impl/UserDaoImpl.java | 64 ++++++++++++++++-- .../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 | 7 +- 11 files changed, 298 insertions(+), 19 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..3d743f8a9 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -2,8 +2,12 @@ import core.basesyntax.dao.CommentDao; import core.basesyntax.model.Comment; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; 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 +16,73 @@ 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 ex) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't create entity: " + entity); + } 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 ex) { + throw new RuntimeException("Can't get comment with id: " + id); + } } @Override public List getAll() { - return null; + Session session = null; + List commentList = null; + try { + session = factory.openSession(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Comment.class); + criteriaQuery.from(Comment.class); + commentList = session.createQuery(criteriaQuery).getResultList(); + } catch (Exception ex) { + throw new RuntimeException("Can't get all comments from DB"); + } finally { + if (session != null) { + session.close(); + } + } + return commentList; } @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 ex) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove entity: " + entity); + } 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..d68437eff 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 jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; 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 +16,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(); + } catch (Exception ex) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't create message: " + entity); + } 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 ex) { + throw new RuntimeException("Can't get message with id: " + id); + } } @Override public List getAll() { - return null; + List messagesList = null; + Session session = null; + try { + session = factory.openSession(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery messageCriteriaQuery = + criteriaBuilder.createQuery(Message.class); + messageCriteriaQuery.from(Message.class); + messagesList = session.createQuery(messageCriteriaQuery).getResultList(); + } catch (Exception ex) { + throw new RuntimeException("Can't get all messages from DB"); + } finally { + if (session != null) { + session.close(); + } + } + return messagesList; } @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 ex) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove entity: " + 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..bdd454c2c 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 = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception ex) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't create message details entity: " + entity); + } 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 ex) { + throw new RuntimeException("Can't get smile with id: " + id); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java index 559248cda..af02dd2cd 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 jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; 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 +16,52 @@ 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 ex) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't create smile: " + entity); + } 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 ex) { + throw new RuntimeException("Can't get smile with id: " + id); + } } @Override public List getAll() { - return null; + List smileList = null; + Session session = null; + try { + session = factory.openSession(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery smileCriteriaQuery = criteriaBuilder.createQuery(Smile.class); + smileCriteriaQuery.from(Smile.class); + smileList = session.createQuery(smileCriteriaQuery).getResultList(); + } catch (Exception ex) { + throw new RuntimeException("Can't get all smiles from DB"); + } finally { + if (session != null) { + session.close(); + } + } + return smileList; } } diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 338d8248b..ce459a2aa 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -2,8 +2,12 @@ import core.basesyntax.dao.UserDao; import core.basesyntax.model.User; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; 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 +16,73 @@ 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 ex) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't create user: " + entity); + } 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 ex) { + throw new RuntimeException("Can't get user with id: " + id); + } } @Override public List getAll() { - return null; + List userList = null; + Session session = null; + try { + session = factory.openSession(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(User.class); + criteriaQuery.from(User.class); + userList = session.createQuery(criteriaQuery).getResultList(); + } catch (Exception ex) { + throw new RuntimeException("Can't get all users from DB"); + } finally { + if (session != null) { + session.close(); + } + } + return userList; } @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 ex) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove entity: " + 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..338d6a8d2 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.CascadeType; +import jakarta.persistence.Entity; 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(cascade = {CascadeType.DETACH}) 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..dc17c925b 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 = "message") public class Message { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) 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..c1dc4d633 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.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..20cb5c832 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -5,7 +5,6 @@ - org.hibernate.dialect.MySQL8Dialect jdbc:mysql://localhost/cinema?serverTimezone=UTC com.mysql.cj.jdbc.Driver ma_admin @@ -13,6 +12,10 @@ true update - + + + + +