From 7e193cd4ebfefba14c12ec7663cc75a385522041 Mon Sep 17 00:00:00 2001 From: Danial Date: Fri, 12 Apr 2024 19:24:21 +0300 Subject: [PATCH] That wasn`t so easy how i think) --- .../basesyntax/dao/impl/CommentDaoImpl.java | 65 ++++++++++++++++++- .../basesyntax/dao/impl/MessageDaoImpl.java | 62 +++++++++++++++++- .../dao/impl/MessageDetailsDaoImpl.java | 29 ++++++++- .../basesyntax/dao/impl/SmileDaoImpl.java | 45 ++++++++++++- .../core/basesyntax/dao/impl/UserDaoImpl.java | 60 +++++++++++++++-- .../java/core/basesyntax/model/Comment.java | 21 ++++++ .../java/core/basesyntax/model/Message.java | 23 +++++++ .../core/basesyntax/model/MessageDetails.java | 18 +++++ .../java/core/basesyntax/model/Smile.java | 2 + src/main/java/core/basesyntax/model/User.java | 30 +++++++++ src/main/resources/hibernate.cfg.xml | 9 ++- 11 files changed, 347 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..46202eae0 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.EntityNotFoundException; +import java.util.ArrayList; 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,76 @@ 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 (RuntimeException e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can`t create to DB" + entity.toString(),e); + } finally { + if (session != null) { + session.close(); + + } + } + return entity; } @Override public Comment get(Long id) { - return null; + try (Session session = factory.openSession()) { + Comment comment = session.get(Comment.class,id); + if (comment != null) { + return comment; + } else { + throw new EntityNotFoundException("Can`t find by id in Db " + id); + } + } catch (RuntimeException e) { + throw new RuntimeException("Can't get from DB comment with id = " + id, e); + + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + List comments = session.createQuery("from Comment").list(); + if (comments != null) { + return comments; + } else { + return new ArrayList<>(); + } + } catch (RuntimeException e) { + throw new RuntimeException("Can`t get from DB all comments", e); + } } @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 (RuntimeException e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Cant remove from Db" + entity.toString(),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..c4ef64c37 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.EntityNotFoundException; +import java.util.ArrayList; 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,73 @@ 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 (RuntimeException e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can`t create massage in to the DB",e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public Message get(Long id) { - return null; + try (Session session = factory.openSession()) { + Message message = session.get(Message.class,id); + if (message != null) { + return message; + } else { + throw new EntityNotFoundException("Can`t find by id" + id); + } + } catch (RuntimeException e) { + throw new RuntimeException("Can`t get from DB" + id,e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + List entities = session.createQuery("from Message").list(); + if (entities != null) { + return entities; + } else { + return new ArrayList<>(); + } + } catch (RuntimeException e) { + throw new RuntimeException("Can't get from DB all messages ", e); + } } @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 (RuntimeException e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can`t delete from DB" + entity.toString(),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..8e422808d 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 (RuntimeException e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can`t create to DB" + entity,e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public MessageDetails get(Long id) { - return null; + try (Session session = factory.openSession()) { + MessageDetails entity = session.get(MessageDetails.class, id); + return entity; + + } catch (RuntimeException e) { + throw new RuntimeException("Can't get from DB messageDetails with 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..742920c22 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.EntityNotFoundException; +import java.util.ArrayList; 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,51 @@ 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 (RuntimeException e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Cant create on DB", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public Smile get(Long id) { - return null; + try (Session session = factory.openSession()) { + Smile smile = session.get(Smile.class,id); + if (smile != null) { + return smile; + } else { + throw new EntityNotFoundException("Can`t found id in DB"); + } + } catch (RuntimeException e) { + throw new RuntimeException("can`t get id in db" + id,e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + List smiles = session.createQuery("from Smile").list(); + if (smiles != null) { + return smiles; + } else { + return new ArrayList<>(); + } + } catch (RuntimeException e) { + throw new RuntimeException("Can't get from DB all 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..41888c3b0 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.EntityNotFoundException; +import java.util.ArrayList; 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,69 @@ 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 (RuntimeException e) { + throw new RuntimeException("Can`t create user" + entity,e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public User get(Long id) { - return null; + try (Session session = factory.openSession()) { + User user = session.get(User.class,id); + if (user != null) { + return user; + } else { + throw new EntityNotFoundException("Can`t find id in DB"); + } + } catch (RuntimeException e) { + throw new RuntimeException("Can`t find id in DB" + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + List users = session.createQuery("from User").list(); + if (users != null) { + return users; + } else { + return new ArrayList<>(); + } + } catch (RuntimeException e) { + throw new RuntimeException("Can't get from DB all users", e); + } } @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 (RuntimeException e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove from DB user = " + entity.toString(), 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..8ccc8af8f 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +1,27 @@ package core.basesyntax.model; +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.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(fetch = FetchType.EAGER) + @JoinTable(name = "coments_smile", + joinColumns = @JoinColumn(name = "comment_id"), + inverseJoinColumns = @JoinColumn(name = "smile_id")) private List smiles; public Long getId() { @@ -35,4 +47,13 @@ public List getSmiles() { public void setSmiles(List smiles) { this.smiles = smiles; } + + @Override + public String toString() { + return "Comment{" + + "id=" + id + + ", content='" + content + '\'' + + ", smiles=" + smiles + + '}'; + } } diff --git a/src/main/java/core/basesyntax/model/Message.java b/src/main/java/core/basesyntax/model/Message.java index 672b5d485..d42e04f43 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.ALL) private MessageDetails messageDetails; public Long getId() { @@ -28,4 +41,14 @@ public MessageDetails getMessageDetails() { public void setMessageDetails(MessageDetails messageDetails) { this.messageDetails = messageDetails; } + + @Override + public String toString() { + return "Message{" + "id=" + + id + ", content='" + + content + '\'' + + ", messageDetails=" + + messageDetails + + '}'; + } } diff --git a/src/main/java/core/basesyntax/model/MessageDetails.java b/src/main/java/core/basesyntax/model/MessageDetails.java index 185023dd5..0c7fac78a 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 = "messageDetails") public class MessageDetails { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String sender; private LocalDateTime sentTime; @@ -30,4 +39,13 @@ public LocalDateTime getSentTime() { public void setSentTime(LocalDateTime sentTime) { this.sentTime = sentTime; } + + @Override + public String toString() { + return "MessageDetails{" + + "id=" + id + + ", sender='" + sender + '\'' + + ", sentTime=" + sentTime + + '}'; + } } diff --git a/src/main/java/core/basesyntax/model/Smile.java b/src/main/java/core/basesyntax/model/Smile.java index 4395eb0f7..35f80d8db 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 = "smile") 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..4bc2e0917 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,10 +1,30 @@ package core.basesyntax.model; +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.ManyToMany; +import jakarta.persistence.Table; import java.util.List; +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; +@Entity +@Table(name = "users") public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; + @ManyToMany(fetch = FetchType.EAGER) // (cascade = {CascadeType.PERSIST, CascadeType.MERGE}) + @Cascade(value = {CascadeType.PERSIST}) + @JoinTable(name = "users_comments", + joinColumns = @JoinColumn(name = "user_id"), + inverseJoinColumns = @JoinColumn(name = "comment_id")) private List comments; public Long getId() { @@ -30,4 +50,14 @@ public List getComments() { public void setComments(List comments) { this.comments = comments; } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", comments=" + comments + + '}'; + } + } diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index cba4560a4..3d68197f4 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -8,11 +8,16 @@ org.hibernate.dialect.MySQL8Dialect jdbc:mysql://localhost/cinema?serverTimezone=UTC com.mysql.cj.jdbc.Driver - ma_admin - 1234 + root + 123456 true update + + + + +