diff --git a/src/main/java/core/basesyntax/dao/CommentDao.java b/src/main/java/core/basesyntax/dao/CommentDao.java index 8a9617dea..47d802ef0 100644 --- a/src/main/java/core/basesyntax/dao/CommentDao.java +++ b/src/main/java/core/basesyntax/dao/CommentDao.java @@ -1,6 +1,18 @@ package core.basesyntax.dao; import core.basesyntax.model.Comment; +import java.util.List; public interface CommentDao extends GenericDao { + @Override + Comment create(Comment entity); + + @Override + Comment get(Long id); + + @Override + List getAll(); + + @Override + void remove(Comment entity); } diff --git a/src/main/java/core/basesyntax/dao/MessageDao.java b/src/main/java/core/basesyntax/dao/MessageDao.java index 63e197bd4..39fffd4c8 100644 --- a/src/main/java/core/basesyntax/dao/MessageDao.java +++ b/src/main/java/core/basesyntax/dao/MessageDao.java @@ -1,6 +1,18 @@ package core.basesyntax.dao; import core.basesyntax.model.Message; +import java.util.List; public interface MessageDao extends GenericDao { + @Override + Message create(Message entity); + + @Override + Message get(Long id); + + @Override + List getAll(); + + @Override + void remove(Message entity); } diff --git a/src/main/java/core/basesyntax/dao/UserDao.java b/src/main/java/core/basesyntax/dao/UserDao.java index 0c66364d5..0d1a3e85c 100644 --- a/src/main/java/core/basesyntax/dao/UserDao.java +++ b/src/main/java/core/basesyntax/dao/UserDao.java @@ -1,7 +1,18 @@ package core.basesyntax.dao; import core.basesyntax.model.User; +import java.util.List; public interface UserDao extends GenericDao { + @Override + User create(User entity); + @Override + User get(Long id); + + @Override + List getAll(); + + @Override + void remove(User entity); } diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index 5176c951f..5456cc2e9 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,63 @@ 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 (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add new comment ", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @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 by id: " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createNativeQuery("SELECT * FROM comment", Comment.class) + .getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get 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 (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove comment ", 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..944bfaed8 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,63 @@ 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 (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add new message ", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @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 a message by id: " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createNativeQuery("SELECT * FROM message", Message.class) + .getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get 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 (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove message", 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..be86633c7 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,32 @@ 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 (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add new message details ", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public MessageDetails get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(MessageDetails.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get message details by 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..a5cec14ae 100644 --- a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java @@ -3,7 +3,9 @@ import core.basesyntax.dao.SmileDao; import core.basesyntax.model.Smile; 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 +14,41 @@ 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 (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add new smile ", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public Smile get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(Smile.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get smile by id: " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createNativeQuery("SELECT * FROM smile", Smile.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get 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..42ebc566d 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,62 @@ 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 (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add new user ", e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @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 by id: " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createNativeQuery("SELECT * FROM user", User.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Can't get 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 (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove user ", 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..163b6ef47 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +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.OneToMany; +import jakarta.persistence.Table; import java.util.List; +@Entity +@Table(name = "comment") public class Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + @OneToMany 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..dee9b7525 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.PERSIST,CascadeType.REMOVE}) 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/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..2e468c0fa 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.OneToMany; +import jakarta.persistence.Table; import java.util.List; +@Entity +@Table(name = "user") public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; + @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST) private List comments; public Long getId() { diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index cba4560a4..25f00774a 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -5,10 +5,10 @@ - org.hibernate.dialect.MySQL8Dialect + org.hibernate.dialect.MySQLDialect jdbc:mysql://localhost/cinema?serverTimezone=UTC com.mysql.cj.jdbc.Driver - ma_admin + root 1234 true update