Skip to content

Commit

Permalink
jv-hibernate-cascade solution
Browse files Browse the repository at this point in the history
  • Loading branch information
vikkushnir committed Apr 2, 2024
1 parent 7777544 commit 99eb8ba
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 20 deletions.
53 changes: 49 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,10 @@
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;
import org.hibernate.query.Query;

public class CommentDaoImpl extends AbstractDao implements CommentDao {
public CommentDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,21 +15,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();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Could not insert comment entity", e);
} finally {
if (session != null) {
session.close();
}
}
}

@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 not get comment with id= " + id, e);
}
}

@Override
public List<Comment> getAll() {
return null;
try (Session session = factory.openSession()) {
Query<Comment> query = session.createQuery("FROM Comment", Comment.class);
return query.getResultList();
} catch (Exception e) {
throw new RuntimeException("Can not 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("Could not remove comment entity", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
53 changes: 49 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,10 @@
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;
import org.hibernate.query.Query;

public class MessageDaoImpl extends AbstractDao implements MessageDao {
public MessageDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,21 +15,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();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Could not insert message entity", e);
} finally {
if (session != null) {
session.close();
}
}
}

@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 not get message with id= " + id, e);
}
}

@Override
public List<Message> getAll() {
return null;
try (Session session = factory.openSession()) {
Query<Message> query = session.createQuery("FROM Message", Message.class);
return query.getResultList();
} catch (Exception e) {
throw new RuntimeException("Can not 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("Could not remove message entity: " + entity, e);
} finally {
if (session != null) {
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 = 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("Could not insert messagesDetail entity: " + entity, e);
} finally {
if (session != null) {
session.close();
}
}
}

@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 not get Message Details with id= " + id, e);
}
}
}
35 changes: 32 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,10 @@
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;
import org.hibernate.query.Query;

public class SmileDaoImpl extends AbstractDao implements SmileDao {
public SmileDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,16 +15,42 @@ 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("Could not insert smile entity: " + entity, e);
} finally {
if (session != null) {
session.close();
}
}
}

@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 not get smile with id= " + id, e);
}
}

@Override
public List<Smile> getAll() {
return null;
try (Session session = factory.openSession()) {
Query<Smile> query = session.createQuery("FROM Smile", Smile.class);
return query.getResultList();
} catch (Exception e) {
throw new RuntimeException("Can not get all smiles", e);
}
}
}
53 changes: 49 additions & 4 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,10 @@
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;
import org.hibernate.query.Query;

public class UserDaoImpl extends AbstractDao implements UserDao {
public UserDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,21 +15,63 @@ 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("Could not insert user entity" + entity, e);
} finally {
if (session != null) {
session.close();
}
}
}

@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 not get user with id= " + id, e);
}
}

@Override
public List<User> getAll() {
return null;
try (Session session = factory.openSession()) {
Query<User> query = session.createQuery("FROM User", User.class);
return query.getResultList();
} catch (Exception e) {
throw new RuntimeException("Can not 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("Could not remove user entity" + entity, e);
} finally {
if (session != null) {
session.close();
}
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -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.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
private List<Smile> smiles;

public Long getId() {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/model/Message.java
Original file line number Diff line number Diff line change
@@ -1,8 +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.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.PERSIST, CascadeType.REMOVE},
fetch = FetchType.LAZY)
private MessageDetails messageDetails;

public Long getId() {
Expand Down
Loading

0 comments on commit 99eb8ba

Please sign in to comment.