From 0f879c787eee87fa7a7a10c2048b580580867f26 Mon Sep 17 00:00:00 2001 From: Pavlo Betsa Date: Tue, 2 Apr 2024 17:41:24 +0300 Subject: [PATCH] hibernate-cascade-solution V1.0 --- pom.xml | 10 ++-- .../basesyntax/dao/impl/CommentDaoImpl.java | 50 ++++++++++++++-- .../basesyntax/dao/impl/MessageDaoImpl.java | 53 +++++++++++++++-- .../dao/impl/MessageDetailsDaoImpl.java | 30 +++++++++- .../basesyntax/dao/impl/SmileDaoImpl.java | 39 +++++++++++-- .../core/basesyntax/dao/impl/UserDaoImpl.java | 57 +++++++++++++++++-- .../exception/DataProcessingException.java | 11 ++++ .../java/core/basesyntax/model/Comment.java | 8 +++ .../java/core/basesyntax/model/Message.java | 13 +++++ .../core/basesyntax/model/MessageDetails.java | 7 +++ src/main/java/core/basesyntax/model/User.java | 13 +++++ src/main/resources/hibernate.cfg.xml | 11 +++- 12 files changed, 271 insertions(+), 31 deletions(-) create mode 100644 src/main/java/core/basesyntax/exception/DataProcessingException.java diff --git a/pom.xml b/pom.xml index 774bb70e9..9a94f5218 100644 --- a/pom.xml +++ b/pom.xml @@ -25,14 +25,14 @@ test - org.hibernate + org.hibernate.orm hibernate-core - 6.2.7.Final + 6.4.4.Final - mysql - mysql-connector-java - 8.0.33 + com.mysql + mysql-connector-j + 8.3.0 org.hsqldb diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index 5176c951f..73ed2e015 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -1,9 +1,13 @@ package core.basesyntax.dao.impl; import core.basesyntax.dao.CommentDao; +import core.basesyntax.exception.DataProcessingException; import core.basesyntax.model.Comment; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.query.Query; public class CommentDaoImpl extends AbstractDao implements CommentDao { public CommentDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +16,59 @@ public CommentDaoImpl(SessionFactory sessionFactory) { @Override public Comment create(Comment entity) { - return null; + Transaction transaction = null; + Session session = 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 DataProcessingException("Can't create new comment", e); + } finally { + if (session != null) { + session.close(); + } + } } @Override public Comment get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(Comment.class, id); + } catch (Exception e) { + throw new DataProcessingException("Can't get comment by id: " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + String hql = "FROM Comment"; + Query fromComment = session.createQuery(hql, Comment.class); + return fromComment.getResultList(); + } catch (Exception e) { + throw new DataProcessingException("Can't get 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 DataProcessingException("Can't remove comment", 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..a2eca8d75 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -1,9 +1,13 @@ package core.basesyntax.dao.impl; import core.basesyntax.dao.MessageDao; +import core.basesyntax.exception.DataProcessingException; import core.basesyntax.model.Message; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.query.Query; public class MessageDaoImpl extends AbstractDao implements MessageDao { public MessageDaoImpl(SessionFactory sessionFactory) { @@ -11,22 +15,59 @@ public MessageDaoImpl(SessionFactory sessionFactory) { } @Override - public Message create(Message entity) { - return null; + public Message create(Message message) { + Transaction transaction = null; + Session session = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(message); + transaction.commit(); + return message; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Can't write new message", e); + } finally { + if (session != null) { + session.close(); + } + } } @Override public Message get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(Message.class, id); + } catch (Exception e) { + throw new DataProcessingException("Can't get message by id: " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + String hql = "FROM Message"; + Query fromComment = session.createQuery(hql, Message.class); + return fromComment.getResultList(); + } catch (Exception e) { + throw new DataProcessingException("Can't get messages ", e); + } } @Override - public void remove(Message entity) { - + public void remove(Message message) { + Transaction transaction = null; + try (Session session = factory.openSession()) { + transaction = session.beginTransaction(); + session.remove(message); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Can't remove message, everyone will see that=)", 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..e16d9ae15 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java @@ -1,8 +1,11 @@ package core.basesyntax.dao.impl; import core.basesyntax.dao.MessageDetailsDao; +import core.basesyntax.exception.DataProcessingException; 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) { @@ -10,12 +13,33 @@ public MessageDetailsDaoImpl(SessionFactory sessionFactory) { } @Override - public MessageDetails create(MessageDetails entity) { - return null; + public MessageDetails create(MessageDetails messageDetails) { + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(messageDetails); + transaction.commit(); + return messageDetails; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Can`t create new messageDetails", e); + } finally { + if (session != null) { + session.close(); + } + } } @Override public MessageDetails get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(MessageDetails.class, id); + } catch (Exception e) { + throw new DataProcessingException("Can`t get smile with this 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..bf7fa7694 100644 --- a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java @@ -1,9 +1,13 @@ package core.basesyntax.dao.impl; import core.basesyntax.dao.SmileDao; +import core.basesyntax.exception.DataProcessingException; import core.basesyntax.model.Smile; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.query.Query; public class SmileDaoImpl extends AbstractDao implements SmileDao { public SmileDaoImpl(SessionFactory sessionFactory) { @@ -11,17 +15,44 @@ public SmileDaoImpl(SessionFactory sessionFactory) { } @Override - public Smile create(Smile entity) { - return null; + public Smile create(Smile smile) { + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(smile); + transaction.commit(); + return smile; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Can`t create new smile"); + } finally { + if (session != null) { + session.close(); + } + } } @Override public Smile get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(Smile.class, id); + } catch (Exception e) { + throw new DataProcessingException("Can`t get smile with this ID: " + id); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + String hql = "FROM Smile"; + Query fromSmile = session.createQuery(hql, Smile.class); + return fromSmile.getResultList(); + } catch (Exception e) { + throw new DataProcessingException("Can`t get smiles", 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..5f69e98e2 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -1,9 +1,14 @@ package core.basesyntax.dao.impl; import core.basesyntax.dao.UserDao; +import core.basesyntax.exception.DataProcessingException; import core.basesyntax.model.User; +import java.util.ArrayList; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.query.Query; public class UserDaoImpl extends AbstractDao implements UserDao { public UserDaoImpl(SessionFactory sessionFactory) { @@ -11,22 +16,62 @@ public UserDaoImpl(SessionFactory sessionFactory) { } @Override - public User create(User entity) { - return null; + public User create(User user) { + Transaction transaction = null; + Session session = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(user); + transaction.commit(); + return user; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Can't create new user", e); + } finally { + if (session != null) { + session.close(); + } + } } @Override public User get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(User.class, id); + } catch (Exception e) { + throw new DataProcessingException("Can't get message by id: " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + String hql = "FROM Comment"; + Query fromUser = session.createQuery(hql, User.class); + if (fromUser != null) { + return fromUser.getResultList(); + } + return new ArrayList<>(); + } catch (Exception e) { + throw new DataProcessingException("Can't get comments ", e); + } } @Override - public void remove(User entity) { - + public void remove(User user) { + Transaction transaction = null; + try (Session session = factory.openSession()) { + transaction = session.beginTransaction(); + session.remove(user); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Can't remove user", e); + } } } diff --git a/src/main/java/core/basesyntax/exception/DataProcessingException.java b/src/main/java/core/basesyntax/exception/DataProcessingException.java new file mode 100644 index 000000000..1620cae33 --- /dev/null +++ b/src/main/java/core/basesyntax/exception/DataProcessingException.java @@ -0,0 +1,11 @@ +package core.basesyntax.exception; + +public class DataProcessingException extends RuntimeException { + public DataProcessingException(String message) { + super(message); + } + + public DataProcessingException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/core/basesyntax/model/Comment.java b/src/main/java/core/basesyntax/model/Comment.java index 9fba0c920..a903b2818 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +1,23 @@ 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 java.util.List; +@Entity public class Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + @ManyToMany + @JoinTable(name = "comment_smile", 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..986ccf879 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.FetchType; +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(fetch = FetchType.LAZY, cascade = + {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}) 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..e5d912808 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.JoinColumn; +import jakarta.persistence.OneToMany; import java.util.List; +@Entity public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; + @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST) + @JoinColumn(name = "comment_id") private List comments; public Long getId() { diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index cba4560a4..c22be5cfa 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/hibernate?serverTimezone=UTC com.mysql.cj.jdbc.Driver - ma_admin - 1234 + root + MySQL1234 true update + + + + +