From a7889f70191a4806c1bb618efa27be7db52801d5 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:38:43 +0300 Subject: [PATCH 01/20] User.java --- src/main/java/core/basesyntax/model/User.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/core/basesyntax/model/User.java b/src/main/java/core/basesyntax/model/User.java index 0141eef00..bbfb437de 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,10 +1,17 @@ package core.basesyntax.model; +import jakarta.persistence.*; import java.util.List; +@Entity public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; + + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "user_id") private List comments; public Long getId() { From b0c886fd6b41666c7d221baf404d88beda734b8b Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:40:02 +0300 Subject: [PATCH 02/20] Comment.java --- src/main/java/core/basesyntax/model/Comment.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/core/basesyntax/model/Comment.java b/src/main/java/core/basesyntax/model/Comment.java index 9fba0c920..a0f51e603 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +1,19 @@ package core.basesyntax.model; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; 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() { From 2f6b24a928149c654938bcf13d2bce8007c5caa7 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:41:11 +0300 Subject: [PATCH 03/20] Message.java --- src/main/java/core/basesyntax/model/Message.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/core/basesyntax/model/Message.java b/src/main/java/core/basesyntax/model/Message.java index 672b5d485..04be6935d 100644 --- a/src/main/java/core/basesyntax/model/Message.java +++ b/src/main/java/core/basesyntax/model/Message.java @@ -1,8 +1,16 @@ package core.basesyntax.model; +import jakarta.persistence.*; + +@Entity public class Message { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + + @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "message_details_id") private MessageDetails messageDetails; public Long getId() { From cca04291ca33c8917cc91761ff5ee4daa4ab2c1e Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:42:05 +0300 Subject: [PATCH 04/20] CommentDaoImpl.java --- .../basesyntax/dao/impl/CommentDaoImpl.java | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 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..60d857685 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -2,8 +2,11 @@ import core.basesyntax.dao.CommentDao; import core.basesyntax.model.Comment; -import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; + +import java.util.List; public class CommentDaoImpl extends AbstractDao implements CommentDao { public CommentDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +15,50 @@ public CommentDaoImpl(SessionFactory sessionFactory) { @Override public Comment create(Comment entity) { - return null; + Transaction transaction = null; + try (Session session = factory.openSession()) { + transaction = session.beginTransaction(); + session.save(entity); + transaction.commit(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't insert Comment entity", e); + } } @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 entity by id", e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("from Comment", Comment.class).list(); + } catch (Exception e) { + throw new RuntimeException("Can't get all Comment entities", e); + } } @Override public void remove(Comment entity) { - + Transaction transaction = null; + try (Session session = factory.openSession()) { + transaction = session.beginTransaction(); + session.delete(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't delete Comment entity", e); + } } } From 3aa5ca51f17ad2de005fce646f7a0633726c3185 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:42:23 +0300 Subject: [PATCH 05/20] UserDaoImpl.java --- .../core/basesyntax/dao/impl/UserDaoImpl.java | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 338d8248b..0c069afe1 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -2,8 +2,11 @@ import core.basesyntax.dao.UserDao; import core.basesyntax.model.User; -import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; + +import java.util.List; public class UserDaoImpl extends AbstractDao implements UserDao { public UserDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +15,50 @@ public UserDaoImpl(SessionFactory sessionFactory) { @Override public User create(User entity) { - return null; + Transaction transaction = null; + try (Session session = factory.openSession()) { + transaction = session.beginTransaction(); + session.save(entity); + transaction.commit(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't insert User entity", e); + } } @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 entity by id", e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("from User", User.class).list(); + } catch (Exception e) { + throw new RuntimeException("Can't get all User entities", e); + } } @Override public void remove(User entity) { - + Transaction transaction = null; + try (Session session = factory.openSession()) { + transaction = session.beginTransaction(); + session.delete(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't delete User entity", e); + } } } From e9aef0ac5af1d441a01dbc8a8b39de9bcfcbc39a Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:42:45 +0300 Subject: [PATCH 06/20] MessageDaoImpl.java --- .../basesyntax/dao/impl/MessageDaoImpl.java | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java index 9b899fa95..85fccd9e6 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -2,8 +2,11 @@ import core.basesyntax.dao.MessageDao; import core.basesyntax.model.Message; -import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; + +import java.util.List; public class MessageDaoImpl extends AbstractDao implements MessageDao { public MessageDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +15,50 @@ public MessageDaoImpl(SessionFactory sessionFactory) { @Override public Message create(Message entity) { - return null; + Transaction transaction = null; + try (Session session = factory.openSession()) { + transaction = session.beginTransaction(); + session.save(entity); + transaction.commit(); + return entity; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't insert Message entity", e); + } } @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 entity by id", e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("from Message", Message.class).list(); + } catch (Exception e) { + throw new RuntimeException("Can't get all Message entities", e); + } } @Override public void remove(Message entity) { - + Transaction transaction = null; + try (Session session = factory.openSession()) { + transaction = session.beginTransaction(); + session.delete(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't delete Message entity", e); + } } } From a1e3e98fc2b46c8b4132b81e980e1af083e8c237 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:45:07 +0300 Subject: [PATCH 07/20] CommentDaoImpl.java From 65c11d0e9d60f48fda0a27d0cc02b92162ce8ad5 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:45:23 +0300 Subject: [PATCH 08/20] UserDaoImpl.java From c0d8b4152627e5d6938cab6a62528ac1ed8fd855 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:46:14 +0300 Subject: [PATCH 09/20] Comment.java --- src/main/java/core/basesyntax/model/Comment.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/model/Comment.java b/src/main/java/core/basesyntax/model/Comment.java index a0f51e603..855ca6c80 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,6 +1,12 @@ package core.basesyntax.model; -import jakarta.persistence.*; +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 From c1bcde69c5dd7131a3892d0b23d084c6755d3258 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:46:29 +0300 Subject: [PATCH 10/20] Message.java --- src/main/java/core/basesyntax/model/Message.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/model/Message.java b/src/main/java/core/basesyntax/model/Message.java index 04be6935d..469ec4e01 100644 --- a/src/main/java/core/basesyntax/model/Message.java +++ b/src/main/java/core/basesyntax/model/Message.java @@ -1,6 +1,11 @@ package core.basesyntax.model; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToOne; @Entity public class Message { @@ -9,7 +14,7 @@ public class Message { private Long id; private String content; - @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true) + @OneToOne(cascade = jakarta.persistence.CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "message_details_id") private MessageDetails messageDetails; From 41b5055cb646059e0a48d8f42840db6718604067 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:46:46 +0300 Subject: [PATCH 11/20] User.java --- src/main/java/core/basesyntax/model/User.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/model/User.java b/src/main/java/core/basesyntax/model/User.java index bbfb437de..fa8261a44 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,6 +1,11 @@ package core.basesyntax.model; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +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 @@ -10,7 +15,7 @@ public class User { private Long id; private String username; - @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) + @OneToMany(cascade = jakarta.persistence.CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "user_id") private List comments; From 96d8bccd72885bf8b4411d14ec6247dc0024aeaf Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:48:24 +0300 Subject: [PATCH 12/20] CommentDaoImpl.java From 9a079046bef6f857cb1a0f09358225998e085be7 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:48:59 +0300 Subject: [PATCH 13/20] MessageDaoImpl.java From 885329628214ada12b46226c91e8451ad58ba776 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:51:05 +0300 Subject: [PATCH 14/20] CommentDaoImpl.java --- src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index 60d857685..2656df952 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -5,7 +5,6 @@ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; - import java.util.List; public class CommentDaoImpl extends AbstractDao implements CommentDao { From d947a0a6953248be2c1072207dccca0534f7240e Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:51:23 +0300 Subject: [PATCH 15/20] UserDaoImpl.java --- src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 0c069afe1..334fe8ec7 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -5,7 +5,6 @@ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; - import java.util.List; public class UserDaoImpl extends AbstractDao implements UserDao { From 24dcca76722874b62e08bbd73c67588aca0aa0e6 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:51:42 +0300 Subject: [PATCH 16/20] MessageDaoImpl.java --- src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java index 85fccd9e6..6ff9b6dcb 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -5,7 +5,6 @@ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; - import java.util.List; public class MessageDaoImpl extends AbstractDao implements MessageDao { From f1c95199d9049e8adebbcbbb698cd9df6bca2822 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:56:31 +0300 Subject: [PATCH 17/20] CommentDaoImpl.java From 8849642f8694d9bfcd3cbc1f5d553f73bb0d4e72 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:56:50 +0300 Subject: [PATCH 18/20] UserDaoImpl.java --- src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 334fe8ec7..ff1671a06 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -2,10 +2,10 @@ import core.basesyntax.dao.UserDao; import core.basesyntax.model.User; +import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; -import java.util.List; public class UserDaoImpl extends AbstractDao implements UserDao { public UserDaoImpl(SessionFactory sessionFactory) { From 5735e9c3526694277612858a446039bcc571b227 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:57:09 +0300 Subject: [PATCH 19/20] MessageDaoImpl.java --- src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java index 6ff9b6dcb..f1b61b7a3 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -2,10 +2,10 @@ import core.basesyntax.dao.MessageDao; import core.basesyntax.model.Message; +import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; -import java.util.List; public class MessageDaoImpl extends AbstractDao implements MessageDao { public MessageDaoImpl(SessionFactory sessionFactory) { From ec8964953a576c0652571682c18555cac005b378 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Wed, 10 Jul 2024 10:58:43 +0300 Subject: [PATCH 20/20] CommentDaoImpl.java --- src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index 2656df952..cb388cf17 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -2,10 +2,10 @@ import core.basesyntax.dao.CommentDao; import core.basesyntax.model.Comment; +import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; -import java.util.List; public class CommentDaoImpl extends AbstractDao implements CommentDao { public CommentDaoImpl(SessionFactory sessionFactory) {