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

Hibernate Cascade solution #970

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
160 changes: 160 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package core.basesyntax;

import core.basesyntax.dao.CommentDao;
import core.basesyntax.dao.MessageDao;
import core.basesyntax.dao.SmileDao;
import core.basesyntax.dao.UserDao;
import core.basesyntax.dao.impl.CommentDaoImpl;
import core.basesyntax.dao.impl.MessageDaoImpl;
import core.basesyntax.dao.impl.SmileDaoImpl;
import core.basesyntax.dao.impl.UserDaoImpl;
import core.basesyntax.model.Comment;
import core.basesyntax.model.Message;
import core.basesyntax.model.MessageDetails;
import core.basesyntax.model.Smile;
import core.basesyntax.model.User;
import java.time.LocalDateTime;
import java.util.List;
import org.hibernate.SessionFactory;

public class Main {
public static void main(String[] args) {
// use this session factory when you will initialize service instances
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

SmileDao smileDao = new SmileDaoImpl(sessionFactory);

Smile smile1 = new Smile("smile1");
smileDao.create(smile1);
Smile smile2 = new Smile("smile2");
smileDao.create(smile2);
Smile smile3 = new Smile("smile3");
smileDao.create(smile3);

System.out.println("smile1:");
System.out.println(smileDao.get(1L));
List<Smile> smilesAll = smileDao.getAll();
System.out.println("smilesAll:");
System.out.println(smilesAll);
System.out.println();

CommentDao commentDao = new CommentDaoImpl(sessionFactory);

Comment comment1 = new Comment();
comment1.setContent("comment1 content");
comment1.setSmiles(List.of(smile1,smile2));
commentDao.create(comment1);
System.out.println("comment1:");
System.out.println(comment1);
System.out.println();

commentDao.remove(comment1);
System.out.println("\nAfter remove comment1:");
System.out.println(comment1);
System.out.println();

System.out.println("commentsAll:");
System.out.println(commentDao.getAll());
System.out.println();

System.out.println("smilesAll:");
System.out.println(smileDao.getAll());
System.out.println();

System.out.println("PreparingUser:");

Comment comment2 = new Comment();
comment2.setContent("comment2 content");
comment2.setSmiles(List.of(smile2, smile3));
System.out.println("comment2:");
System.out.println(comment2);
System.out.println();

System.out.println("commentsAll:");
System.out.println(commentDao.getAll());
System.out.println();

User user1 = new User();
user1.setUsername("user1");
user1.setComments(List.of(comment2));

System.out.println("user1:");
System.out.println(user1);
System.out.println();

UserDao userDao = new UserDaoImpl(sessionFactory);
userDao.create(user1);

System.out.println("\nAfter create in DB user1 :");
System.out.println(user1);
System.out.println();

System.out.println("usersAll:");
System.out.println(userDao.getAll());
System.out.println();

System.out.println("commentsAll:");
System.out.println(commentDao.getAll());
System.out.println();

System.out.println("smilesAll:");
System.out.println(smileDao.getAll());
System.out.println();

userDao.remove(user1);

System.out.println("\nAfter remove in DB user1 :");
System.out.println(user1);
System.out.println();

System.out.println("usersAll:");
System.out.println(userDao.getAll());
System.out.println();

System.out.println("commentsAll:");
System.out.println(commentDao.getAll());
System.out.println();

System.out.println("smilesAll:");
System.out.println(smileDao.getAll());
System.out.println();

System.out.println("\nPreparing message:");

MessageDetails messageDetails1 = new MessageDetails();
messageDetails1.setSender("user1");
messageDetails1.setSentTime(LocalDateTime.now());

System.out.println("messageDetails1:");
System.out.println(messageDetails1);
System.out.println();

Message message1 = new Message();
message1.setContent("message1 content");
message1.setMessageDetails(messageDetails1);

System.out.println("message1:");
System.out.println(message1);
System.out.println();

MessageDao messageDao = new MessageDaoImpl(sessionFactory);
messageDao.create(message1);

System.out.println("\nAfter create message1 in DB");
System.out.println("message1:");
System.out.println(messageDao.get(1L));
System.out.println();

System.out.println("messageAll:");
System.out.println(messageDao.getAll());
System.out.println();

System.out.println("\nAfter remove message1 in DB");

messageDao.remove(message1);

System.out.println("messageAll:");
System.out.println(messageDao.getAll());
System.out.println();
}
}
64 changes: 60 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import core.basesyntax.dao.CommentDao;
import core.basesyntax.model.Comment;
import jakarta.persistence.EntityNotFoundException;
import java.util.ArrayList;
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 +16,73 @@ 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't save to DB comment = " + entity.toString(), e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public Comment get(Long id) {
return null;
try (Session session = factory.openSession()) {
Comment comment = session.get(Comment.class, id);
if (comment != null) {
return comment;
} else {
throw new EntityNotFoundException("Not found in DB comment with id = " + id);
}
} catch (RuntimeException e) {
throw new RuntimeException("Can't get from DB comment with id = " + id, e);
}
}

@Override
public List<Comment> getAll() {
return null;
try (Session session = factory.openSession()) {
List<Comment> comments = session.createQuery("from Comment").list();
if (comments != null) {
return comments;
} else {
// throw new EntityNotFoundException("Not found in DB all comments");
return new ArrayList<>();
}
} catch (RuntimeException e) {
throw new RuntimeException("Can't get from DB 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't remove from DB comment = " + entity.toString(), e);
} finally {
if (session != null) {
session.close();
}
}
}
}
64 changes: 60 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import core.basesyntax.dao.MessageDao;
import core.basesyntax.model.Message;
import jakarta.persistence.EntityNotFoundException;
import java.util.ArrayList;
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 +16,73 @@ 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't save to DB message = " + entity.toString(), e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public Message get(Long id) {
return null;
try (Session session = factory.openSession()) {
Message entity = session.get(Message.class, id);
if (entity != null) {
return entity;
} else {
throw new EntityNotFoundException("Not found in DB message with id = " + id);
}
} catch (RuntimeException e) {
throw new RuntimeException("Can't get from DB message with id = " + id, e);
}
}

@Override
public List<Message> getAll() {
return null;
try (Session session = factory.openSession()) {
List<Message> entities = session.createQuery("from Message").list();
if (entities != null) {
return entities;
} else {
// throw new EntityNotFoundException("Not found in DB all smiles");
return new ArrayList<>();
}
} catch (RuntimeException e) {
throw new RuntimeException("Can't get from DB 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't remove from DB messege = " + entity.toString(), e);
} finally {
if (session != null) {
session.close();
}
}
}
}
35 changes: 33 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,10 @@

import core.basesyntax.dao.MessageDetailsDao;
import core.basesyntax.model.MessageDetails;
// import jakarta.persistence.EntityNotFoundException;
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 +14,39 @@ 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't save to DB messageDetails = " + entity.toString(), e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public MessageDetails get(Long id) {
return null;
try (Session session = factory.openSession()) {
MessageDetails entity = session.get(MessageDetails.class, id);
return entity;
// if (entity != null) {
// return entity;
// } else {
// throw new EntityNotFoundException(
// "Not found in DB messageDetails with id=" + id);
// }
} catch (RuntimeException e) {
throw new RuntimeException("Can't get from DB messageDetails with id = " + id, e);
}
}
}
Loading
Loading