diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index 5176c951f..cb388cf17 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -3,7 +3,9 @@ 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; public class CommentDaoImpl extends AbstractDao implements CommentDao { public CommentDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +14,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); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java index 9b899fa95..f1b61b7a3 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -3,7 +3,9 @@ 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; public class MessageDaoImpl extends AbstractDao implements MessageDao { public MessageDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +14,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); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 338d8248b..ff1671a06 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -3,7 +3,9 @@ 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; public class UserDaoImpl extends AbstractDao implements UserDao { public UserDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +14,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); + } } } diff --git a/src/main/java/core/basesyntax/model/Comment.java b/src/main/java/core/basesyntax/model/Comment.java index 9fba0c920..855ca6c80 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +1,25 @@ 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..469ec4e01 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.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 { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + + @OneToOne(cascade = jakarta.persistence.CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "message_details_id") private MessageDetails messageDetails; public Long getId() { diff --git a/src/main/java/core/basesyntax/model/User.java b/src/main/java/core/basesyntax/model/User.java index 0141eef00..fa8261a44 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,10 +1,22 @@ 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.OneToMany; import java.util.List; +@Entity public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; + + @OneToMany(cascade = jakarta.persistence.CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "user_id") private List comments; public Long getId() {