diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index c0ec1141f..42be95fc2 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -2,10 +2,8 @@ import core.basesyntax.dao.CommentDao; import core.basesyntax.model.Comment; - import java.util.ArrayList; import java.util.List; - import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; @@ -51,7 +49,6 @@ public Comment get(Long id) { } } - public List getAll() { try (Session session = factory.openSession()) { String hql = "FROM Comment"; diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java index 4458c4c74..af2b48a35 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -2,10 +2,8 @@ import core.basesyntax.dao.MessageDao; import core.basesyntax.model.Message; - import java.util.ArrayList; import java.util.List; - import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; @@ -51,7 +49,6 @@ public Message get(Long id) { } } - @Override public List getAll() { try (Session session = factory.openSession()) { diff --git a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java index b258e76bf..777dc4e0f 100644 --- a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java @@ -2,10 +2,8 @@ import core.basesyntax.dao.SmileDao; import core.basesyntax.model.Smile; - import java.util.ArrayList; import java.util.List; - import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; @@ -38,7 +36,6 @@ public Smile create(Smile entity) { return entity; } - @Override public Smile get(Long id) { try (Session session = factory.openSession()) { diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index f3708fc23..1ce5281cb 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -2,10 +2,8 @@ import core.basesyntax.dao.UserDao; 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; @@ -23,13 +21,13 @@ public User create(User entity) { try { session = factory.openSession(); transaction = session.beginTransaction(); - session.save(entity); + session.persist(entity); transaction.commit(); - } catch (Exception e) { + } catch (RuntimeException e) { if (transaction != null) { transaction.rollback(); } - throw new RuntimeException("Cannot create User " + entity); + throw new RuntimeException("Error while creating entity"); } finally { if (transaction != null) { session.close(); @@ -65,6 +63,27 @@ public List getAll() { @Override public void remove(User entity) { - + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + User user = session.get(User.class, entity.getId()); + if (user != null) { + session.delete(user); + } else { + throw new RuntimeException("Entity not found with id: " + entity.getId()); + } + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Failed to delete entity " + entity, e); + } finally { + if (session != null && session.isOpen()) { + session.close(); + } + } } } diff --git a/src/main/java/core/basesyntax/model/Comment.java b/src/main/java/core/basesyntax/model/Comment.java index a8ec08ca4..014cee1c4 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,20 +1,22 @@ package core.basesyntax.model; +import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; -import jakarta.persistence.Id; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; -import jakarta.persistence.OneToMany; - +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; import java.util.List; + @Entity public class Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; - @OneToMany + @ManyToMany(cascade = CascadeType.MERGE) private List smiles; + public Long getId() { return id; } diff --git a/src/main/java/core/basesyntax/model/Message.java b/src/main/java/core/basesyntax/model/Message.java index 87d9e3f20..3bcdb7e0c 100644 --- a/src/main/java/core/basesyntax/model/Message.java +++ b/src/main/java/core/basesyntax/model/Message.java @@ -1,9 +1,9 @@ package core.basesyntax.model; import jakarta.persistence.Entity; -import jakarta.persistence.Id; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; import jakarta.persistence.OneToOne; import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; diff --git a/src/main/java/core/basesyntax/model/MessageDetails.java b/src/main/java/core/basesyntax/model/MessageDetails.java index 89b3d2e2d..d988e46f0 100644 --- a/src/main/java/core/basesyntax/model/MessageDetails.java +++ b/src/main/java/core/basesyntax/model/MessageDetails.java @@ -4,8 +4,8 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; - import java.time.LocalDateTime; + @Entity public class MessageDetails { @Id diff --git a/src/main/java/core/basesyntax/model/Smile.java b/src/main/java/core/basesyntax/model/Smile.java index 4395eb0f7..c118a0ab6 100644 --- a/src/main/java/core/basesyntax/model/Smile.java +++ b/src/main/java/core/basesyntax/model/Smile.java @@ -4,7 +4,9 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.Table; +@Table(name = "smiles") @Entity public class Smile { @Id diff --git a/src/main/java/core/basesyntax/model/User.java b/src/main/java/core/basesyntax/model/User.java index 8f66c714f..7134c0f64 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,21 +1,32 @@ package core.basesyntax.model; +import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; -import jakarta.persistence.Id; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; -import jakarta.persistence.OneToMany; - +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; +import java.util.ArrayList; import java.util.List; + @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; - @OneToMany + @ManyToMany( + cascade = { + CascadeType.MERGE, + CascadeType.PERSIST}, + fetch = FetchType.EAGER) private List comments; + public User() { + this.comments = new ArrayList<>(); + } + public Long getId() { return id; }