diff --git a/pom.xml b/pom.xml
index 774bb70e9..8d3160341 100644
--- a/pom.xml
+++ b/pom.xml
@@ -40,6 +40,11 @@
2.7.2
test
+
+ com.mysql
+ mysql-connector-j
+ 8.2.0
+
diff --git a/src/main/java/core/basesyntax/dao/UserDao.java b/src/main/java/core/basesyntax/dao/UserDao.java
index 0c66364d5..bad4cdee0 100644
--- a/src/main/java/core/basesyntax/dao/UserDao.java
+++ b/src/main/java/core/basesyntax/dao/UserDao.java
@@ -3,5 +3,4 @@
import core.basesyntax.model.User;
public interface UserDao extends GenericDao {
-
}
diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java
index 5176c951f..f28bf0ac0 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 jakarta.persistence.Table;
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 +15,70 @@ 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("cant create comment: "
+ + entity.getContent());
+ } 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("Cant get comment with id: " + id);
+ }
}
@Override
public List getAll() {
- return null;
+ String tableName = null;
+ try (Session session = factory.openSession()) {
+ Table tableAnnotation = Comment.class.getAnnotation(Table.class);
+ tableName = tableAnnotation != null ? tableAnnotation.name()
+ : Comment.class.getSimpleName();
+ return session.createNativeQuery("SELECT * FROM comments",
+ Comment.class)
+ .getResultList();
+ } catch (Exception e) {
+ throw new RuntimeException("Cant get all data from " + tableName);
+ }
}
@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("Cant remove comment: "
+ + entity.getContent());
+ } 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..4ff21e93e 100644
--- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
+++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
@@ -1,9 +1,13 @@
package core.basesyntax.dao.impl;
import core.basesyntax.dao.MessageDao;
+import core.basesyntax.model.Comment;
import core.basesyntax.model.Message;
+import jakarta.persistence.Table;
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,72 @@ 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();
+ if (entity.getMessageDetails() != null) {
+ session.persist(entity.getMessageDetails());
+ }
+ session.persist(entity);
+ transaction.commit();
+ } catch (Exception e) {
+ if (transaction != null) {
+ transaction.rollback();
+ }
+ throw new RuntimeException("Cant create message: "
+ + entity.getContent());
+ } 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("Cant get message with id: " + id);
+ }
}
@Override
public List getAll() {
- return null;
+ String tableName = null;
+ try (Session session = factory.openSession()) {
+ Table tableAnnotation = Comment.class.getAnnotation(Table.class);
+ tableName = tableAnnotation != null ? tableAnnotation.name()
+ : Comment.class.getSimpleName();
+ return session.createNativeQuery("SELECT * FROM messages", Message.class)
+ .getResultList();
+ } catch (Exception e) {
+ throw new RuntimeException("Cant get all data from " + tableName);
+ }
}
@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("Cant remove message: "
+ + entity.getContent());
+ } 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..9c2d6f0cf 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("cant create message detail: " + entity.getSender());
+ } 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("Cant get message detail with id: " + id);
+ }
}
}
diff --git a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
index 559248cda..a417e100e 100644
--- a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
+++ b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
@@ -1,9 +1,13 @@
package core.basesyntax.dao.impl;
import core.basesyntax.dao.SmileDao;
+import core.basesyntax.model.Comment;
import core.basesyntax.model.Smile;
+import jakarta.persistence.Table;
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,46 @@ 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("Cant create smile: " + entity);
+ } 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("Cant get smile with id: " + id);
+ }
}
@Override
public List getAll() {
- return null;
+ String tableName = null;
+ try (Session session = factory.openSession()) {
+ Table tableAnnotation = Comment.class.getAnnotation(Table.class);
+ tableName = tableAnnotation != null ? tableAnnotation.name()
+ : Comment.class.getSimpleName();
+ return session.createNativeQuery("SELECT * FROM smiles", Smile.class)
+ .getResultList();
+ } catch (Exception e) {
+ throw new RuntimeException("Cant get all data from " + tableName);
+ }
}
}
diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
index 338d8248b..407b8a5bc 100644
--- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
+++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
@@ -1,9 +1,14 @@
package core.basesyntax.dao.impl;
import core.basesyntax.dao.UserDao;
+import core.basesyntax.model.Comment;
import core.basesyntax.model.User;
+import jakarta.persistence.Table;
import java.util.List;
+import org.hibernate.Hibernate;
+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 +17,71 @@ 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("cant create user: "
+ + entity.getUsername());
+ } 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);
+ Hibernate.initialize(user.getComments());
+ return user;
+ } catch (Exception e) {
+ throw new RuntimeException("Cant get user with id: " + id, e);
+ }
}
@Override
public List getAll() {
- return null;
+ String tableName = null;
+ try (Session session = factory.openSession()) {
+ Table tableAnnotation = Comment.class.getAnnotation(Table.class);
+ tableName = tableAnnotation != null ? tableAnnotation.name()
+ : Comment.class.getSimpleName();
+ return session.createNativeQuery("SELECT * FROM users", User.class)
+ .getResultList();
+ } catch (Exception e) {
+ throw new RuntimeException("Cant get all data from " + tableName);
+ }
}
@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("Cant remove user: "
+ + entity.getUsername(), 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..e884c1802 100644
--- a/src/main/java/core/basesyntax/model/Comment.java
+++ b/src/main/java/core/basesyntax/model/Comment.java
@@ -1,15 +1,28 @@
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.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(cascade = {CascadeType.DETACH, CascadeType.REFRESH})
+ @JoinTable(name = "comments_smiles",
+ 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..b6a16ca2f 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 = "messages")
public class Message {
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
+ @OneToOne(cascade = 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..16e5a4ee7 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 = "messages_details")
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..9218c0f8d 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 = "smiles")
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..4f04c2aff 100644
--- a/src/main/java/core/basesyntax/model/User.java
+++ b/src/main/java/core/basesyntax/model/User.java
@@ -1,10 +1,29 @@
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.JoinColumn;
+import jakarta.persistence.JoinTable;
+import jakarta.persistence.ManyToMany;
+import jakarta.persistence.Table;
import java.util.List;
+@Entity
+@Table(name = "users")
public class User {
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
+ @ManyToMany(cascade = {CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.EAGER)
+ @JoinTable(name = "users_comments",
+ joinColumns = @JoinColumn(name = "user_id"),
+ inverseJoinColumns = @JoinColumn(name = "comment_id")
+ )
private List comments;
public Long getId() {
diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml
index cba4560a4..fdcabe596 100644
--- a/src/main/resources/hibernate.cfg.xml
+++ b/src/main/resources/hibernate.cfg.xml
@@ -6,13 +6,18 @@
org.hibernate.dialect.MySQL8Dialect
- jdbc:mysql://localhost/cinema?serverTimezone=UTC
+ jdbc:mysql://localhost:3306/test4?serverTimeZone=UTC
com.mysql.cj.jdbc.Driver
- ma_admin
- 1234
+ root
+ Focus1432__
true
update
+
+
+
+
+