From e31ba622403d964b33b870917528d2039aa19211 Mon Sep 17 00:00:00 2001 From: Milosz Date: Thu, 6 Jun 2024 12:28:44 +0200 Subject: [PATCH] Solved hibernate-cascade task Added all required annotations Implemented all dao interfaces --- .../basesyntax/dao/impl/CommentDaoImpl.java | 60 +++++++++++++++++-- .../basesyntax/dao/impl/MessageDaoImpl.java | 60 +++++++++++++++++-- .../dao/impl/MessageDetailsDaoImpl.java | 27 ++++++++- .../basesyntax/dao/impl/SmileDaoImpl.java | 42 ++++++++++++- .../core/basesyntax/dao/impl/UserDaoImpl.java | 60 +++++++++++++++++-- .../java/core/basesyntax/model/Comment.java | 11 ++++ .../java/core/basesyntax/model/Message.java | 13 ++++ .../core/basesyntax/model/MessageDetails.java | 9 +++ src/main/java/core/basesyntax/model/User.java | 19 ++++++ 9 files changed, 284 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..9c6388973 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -2,8 +2,14 @@ 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 org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class CommentDaoImpl extends AbstractDao implements CommentDao { public CommentDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +18,67 @@ public CommentDaoImpl(SessionFactory sessionFactory) { @Override public Comment create(Comment entity) { - return null; + Session session = null; + Transaction tx = null; + try { + session = factory.openSession(); + tx = session.beginTransaction(); + session.persist(entity); + tx.commit(); + } catch (Exception e) { + if (tx != null) { + tx.rollback(); + } + throw new RuntimeException("Can't save comment: " + 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("Can't get comment by id: " + id, e); + } } @Override public List getAll() { - return null; + CriteriaBuilder criteriaBuilder = factory.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Comment.class); + Root comment = criteriaQuery.from(Comment.class); + criteriaQuery.select(comment); + try (Session session = factory.openSession()) { + Query query = session.createQuery(criteriaQuery); + return query.getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get comments from db", e); + } } @Override public void remove(Comment entity) { - + Session session = null; + Transaction tx = null; + try { + session = factory.openSession(); + tx = session.beginTransaction(); + session.remove(entity); + tx.commit(); + } catch (Exception e) { + if (tx != null) { + tx.rollback(); + } + throw new RuntimeException("Can't remove comment: " + 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..65850d028 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -2,8 +2,14 @@ import core.basesyntax.dao.MessageDao; import core.basesyntax.model.Message; +import jakarta.persistence.Query; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; 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 +18,67 @@ public MessageDaoImpl(SessionFactory sessionFactory) { @Override public Message create(Message entity) { - return null; + Session session = null; + Transaction tx = null; + try { + session = factory.openSession(); + tx = session.beginTransaction(); + session.persist(entity); + tx.commit(); + } catch (Exception e) { + if (tx != null) { + tx.rollback(); + } + throw new RuntimeException("Can't save message: " + 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("Can't get message by id: " + id, e); + } } @Override public List getAll() { - return null; + CriteriaBuilder criteriaBuilder = factory.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Message.class); + Root message = criteriaQuery.from(Message.class); + criteriaQuery.select(message); + try (Session session = factory.openSession()) { + Query query = session.createQuery(criteriaQuery); + return query.getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get messages from db", e); + } } @Override public void remove(Message entity) { - + Session session = null; + Transaction tx = null; + try { + session = factory.openSession(); + tx = session.beginTransaction(); + session.remove(entity); + tx.commit(); + } catch (Exception e) { + if (tx != null) { + tx.rollback(); + } + throw new RuntimeException("Can't remove message: " + 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..b9b0e93ba 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 tx = null; + try { + session = factory.openSession(); + tx = session.beginTransaction(); + session.persist(entity); + tx.commit(); + } catch (Exception e) { + if (tx != null) { + tx.rollback(); + } + throw new RuntimeException("Can't save message details: " + 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("Can't get message details by id: " + 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..610b50b1f 100644 --- a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java @@ -2,8 +2,14 @@ import core.basesyntax.dao.SmileDao; import core.basesyntax.model.Smile; +import jakarta.persistence.Query; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; 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 +18,46 @@ public SmileDaoImpl(SessionFactory sessionFactory) { @Override public Smile create(Smile entity) { - return null; + Session session = null; + Transaction tx = null; + try { + session = factory.openSession(); + tx = session.beginTransaction(); + session.persist(entity); + tx.commit(); + } catch (Exception e) { + if (tx != null) { + tx.rollback(); + } + throw new RuntimeException("Can't save smile: " + 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("Can't get smile by id: " + id, e); + } } @Override public List getAll() { - return null; + CriteriaBuilder criteriaBuilder = factory.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(Smile.class); + Root smile = criteriaQuery.from(Smile.class); + criteriaQuery.select(smile); + try (Session session = factory.openSession()) { + Query query = session.createQuery(criteriaQuery); + return query.getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get smiles from db", e); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 338d8248b..25771daeb 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -2,8 +2,14 @@ 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 org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class UserDaoImpl extends AbstractDao implements UserDao { public UserDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +18,67 @@ public UserDaoImpl(SessionFactory sessionFactory) { @Override public User create(User entity) { - return null; + Session session = null; + Transaction tx = null; + try { + session = factory.openSession(); + tx = session.beginTransaction(); + session.persist(entity); + tx.commit(); + } catch (Exception e) { + if (tx != null) { + tx.rollback(); + } + throw new RuntimeException("Can't save user: " + 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("Can't get user by id: " + id, e); + } } @Override public List getAll() { - return null; + CriteriaBuilder criteriaBuilder = factory.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery(User.class); + Root user = criteriaQuery.from(User.class); + criteriaQuery.select(user); + try (Session session = factory.openSession()) { + Query query = session.createQuery(criteriaQuery); + return query.getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get users from db", e); + } } @Override public void remove(User entity) { - + Session session = null; + Transaction tx = null; + try { + session = factory.openSession(); + tx = session.beginTransaction(); + session.remove(entity); + tx.commit(); + } catch (Exception e) { + if (tx != null) { + tx.rollback(); + } + throw new RuntimeException("Can't remove user: " + 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..6c5c96da1 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +1,26 @@ 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.JoinTable; +import jakarta.persistence.ManyToMany; +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; + @ManyToMany() + @JoinTable(name = "comment_smiles", + joinColumns = @JoinColumn(name = "comment_id"), + inverseJoinColumns = @JoinColumn(name = "smile_id")) 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..ca0de7951 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.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/User.java b/src/main/java/core/basesyntax/model/User.java index 0141eef00..49bab64ca 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,10 +1,29 @@ 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.JoinTable; +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) + @JoinTable(name = "user_comments", + joinColumns = @JoinColumn(name = "user_id"), + inverseJoinColumns = @JoinColumn(name = "comment_id")) private List comments; public Long getId() {