Skip to content

Commit

Permalink
Hibernate cascade task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazarian-1 committed Jul 28, 2024
1 parent 7777544 commit f2497e7
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/java/core/basesyntax/HibernateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static SessionFactory sessionFactory = initSessionFactory();
private static final SessionFactory sessionFactory = initSessionFactory();

private HibernateUtil() {
}
Expand Down
61 changes: 58 additions & 3 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,74 @@ 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();
return entity;

} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't add Comment object in database", e);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public Comment get(Long id) {
return null;
Comment comment = null;

try (Session session = factory.openSession()) {
comment = session.get(Comment.class, id);
} catch (Exception e) {
throw new RuntimeException("Can't retrieve Comment object by id:" + id, e);
}

return comment;
}

@Override
public List<Comment> getAll() {
return null;
List<Comment> commentList = null;

try (Session session = factory.openSession()) {
commentList = session.createQuery("FROM Comment", Comment.class).list();
} catch (Exception e) {
throw new RuntimeException("Can't retrieve all Comment objects:", e);
}

return commentList;
}

@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 object from database", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
61 changes: 58 additions & 3 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,74 @@ 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();
return entity;

} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't add Message object in database", e);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public Message get(Long id) {
return null;
Message message = null;

try (Session session = factory.openSession()) {
message = session.get(Message.class, id);
} catch (Exception e) {
throw new RuntimeException("Can't retrieve Message object by id:" + id, e);
}

return message;
}

@Override
public List<Message> getAll() {
return null;
List<Message> messageList = null;

try (Session session = factory.openSession()) {
messageList = session.createQuery("FROM Message", Message.class).list();
} catch (Exception e) {
throw new RuntimeException("Can't retrieve all Comment objects:", e);
}

return messageList;
}

@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 object from database", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
32 changes: 30 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,37 @@ 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();
return entity;

} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't add MessageDetails object in database", e);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public MessageDetails get(Long id) {
return null;
MessageDetails messageDetails = null;

try (Session session = factory.openSession()) {
messageDetails = session.get(MessageDetails.class, id);
} catch (Exception e) {
throw new RuntimeException("Can't retrieve MessageDetails object by id:" + id, e);
}
return messageDetails;
}
}
43 changes: 40 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,51 @@ 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();
return entity;

} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't add Smile object in database", e);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public Smile get(Long id) {
return null;
Smile smile = null;

try (Session session = factory.openSession()) {
smile = session.get(Smile.class, id);
} catch (Exception e) {
throw new RuntimeException("Can't retrieve Smile object by id:" + id, e);
}

return smile;
}

@Override
public List<Smile> getAll() {
return null;
List<Smile> smileList = null;

try (Session session = factory.openSession()) {
smileList = session.createQuery("FROM Smile", Smile.class).list();
} catch (Exception e) {
throw new RuntimeException("Can't retrieve all Smile objects:", e);
}

return smileList;
}
}
61 changes: 58 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -12,21 +14,74 @@ 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();
return entity;

} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't add User object in database", e);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public User get(Long id) {
return null;
User user = null;

try (Session session = factory.openSession()) {
user = session.get(User.class, id);
} catch (Exception e) {
throw new RuntimeException("Can't retrieve User object by id:" + id, e);
}

return user;
}

@Override
public List<User> getAll() {
return null;
List<User> userList = null;

try (Session session = factory.openSession()) {
userList = session.createQuery("FROM User", User.class).list();
} catch (Exception e) {
throw new RuntimeException("Can't retrieve all User objects:", e);
}

return userList;
}

@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 object from database", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
Loading

0 comments on commit f2497e7

Please sign in to comment.