Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task done #1066

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -12,21 +14,66 @@ public CommentDaoImpl(SessionFactory sessionFactory) {

@Override
public Comment create(Comment entity) {
return null;
Session session = factory.openSession();
Transaction transaction = null;
try {
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);
} finally {
session.close();
}
}

@Override
public Comment get(Long id) {
return null;
Session session = factory.openSession();
try {
Comment comment = session.get(Comment.class, id);
return comment;
} catch (Exception e) {
throw new RuntimeException("Can't get comment by id" + id, e);
} finally {
session.close();
}
}

@Override
public List<Comment> getAll() {
return null;
Session session = factory.openSession();
try {
return session.createQuery("FROM Comment", Comment.class).getResultList();
} catch (Exception e) {
throw new RuntimeException("Can't get list of comments", e);
} finally {
session.close();
}
}

@Override
public void remove(Comment entity) {

Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Comment comment = session.get(Comment.class, entity.getId());
if (comment != null) {
session.remove(comment);
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete comment entity", e);
} finally {
session.close();
}
}
}
52 changes: 48 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -12,21 +14,63 @@ public MessageDaoImpl(SessionFactory sessionFactory) {

@Override
public Message create(Message entity) {
return null;
Session session = factory.openSession();
Transaction transaction = null;
try {
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);
} finally {
session.close();
}
}

@Override
public Message get(Long id) {
return null;
Session session = factory.openSession();
try {
Message message = session.get(Message.class, id);
return message;
} catch (Exception e) {
throw new RuntimeException("Can't get message by id " + id, e);
} finally {
session.close();
}
}

@Override
public List<Message> getAll() {
return null;
Session session = factory.openSession();
try {
return session.createQuery("FROM Message", Message.class).getResultList();
} catch (Exception e) {
throw new RuntimeException("Can't get list of messages", e);
} finally {
session.close();
}
}

@Override
public void remove(Message entity) {

Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.remove(session.get(Message.class, entity.getId()));
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete message entity", e);
} finally {
session.close();
}
}
}
27 changes: 25 additions & 2 deletions src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -11,11 +13,32 @@ public MessageDetailsDaoImpl(SessionFactory sessionFactory) {

@Override
public MessageDetails create(MessageDetails entity) {
return null;
Session session = factory.openSession();
Transaction transaction = null;
try {
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 details entity", e);
} finally {
session.close();
}
}

@Override
public MessageDetails get(Long id) {
return null;
Session session = factory.openSession();
try {
return session.get(MessageDetails.class, id);
} catch (Exception e) {
throw new RuntimeException("Can't get message details by id " + id, e);
} finally {
session.close();
}
}
}
36 changes: 33 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -12,16 +14,44 @@ public SmileDaoImpl(SessionFactory sessionFactory) {

@Override
public Smile create(Smile entity) {
return null;
Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(entity);
transaction.commit();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't insert smile entity", e);
} finally {
session.close();
}
}

@Override
public Smile get(Long id) {
return null;
Session session = factory.openSession();
try {
return session.get(Smile.class, id);
} catch (Exception e) {
throw new RuntimeException("Can't get smail by id " + id, e);
} finally {
session.close();
}
}

@Override
public List<Smile> getAll() {
return null;
Session session = factory.openSession();
try {
return session.createQuery("FROM Smile", Smile.class).getResultList();
} catch (Exception e) {
throw new RuntimeException("Can't get list of comments", e);
} finally {
session.close();
}
}
}
60 changes: 56 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package core.basesyntax.dao.impl;

import core.basesyntax.dao.UserDao;
import core.basesyntax.model.Comment;
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) {
Expand All @@ -12,21 +15,70 @@ public UserDaoImpl(SessionFactory sessionFactory) {

@Override
public User create(User entity) {
return null;
Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
if (entity.getComments() != null) {
for (Comment comment : entity.getComments()) {
session.save(comment);
}
}
session.save(entity);
transaction.commit();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't insert user entity", e);
} finally {
session.close();
}
}

@Override
public User get(Long id) {
return null;
Session session = factory.openSession();
try {
return session.get(User.class, id);
} catch (Exception e) {
throw new RuntimeException("Can't get smail by id " + id, e);
} finally {
session.close();
}
}

@Override
public List<User> getAll() {
return null;
Session session = factory.openSession();
try {
return session.createQuery("FROM User", User.class).getResultList();
} catch (Exception e) {
throw new RuntimeException("Can't get list of comments", e);
} finally {
session.close();
}
}

@Override
public void remove(User entity) {

Session session = factory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
User user = session.get(User.class, entity.getId());
if (user != null) {
session.remove(user);
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete comment entity", e);
} finally {
session.close();
}
}
}
9 changes: 8 additions & 1 deletion src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,16 +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 java.util.ArrayList;
import java.util.List;

@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
private List<Smile> smiles;
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
private List<Smile> smiles = new ArrayList<>();

public Long getId() {
return id;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/core/basesyntax/model/Message.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
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.OneToOne;

@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "message_details_id")
private MessageDetails messageDetails;

public Long getId() {
Expand Down
Loading
Loading