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 task #967

Open
wants to merge 2 commits 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
57 changes: 53 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,30 +3,79 @@
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 {
private Session session = null;
private Transaction transaction = null;

public CommentDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
}

@Override
public Comment create(Comment entity) {
return null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(entity);
transaction.commit();
session.close();
return entity;
} catch (Exception e) {
transaction.rollback();
throw new RuntimeException("Error creating entity to the database", e);
} finally {
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't get an Entity from the DB", e);
}
}

@Override
public List<Comment> getAll() {
return null;
List<Comment> comments = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
comments = session.createQuery("FROM Comment", Comment.class).getResultList();
transaction.commit();
session.close();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Error retrieving all entities from the database", e);
} finally {
if (session != null) {
session.close();
}
}
return comments;
}

@Override
public void remove(Comment entity) {

try {
session = factory.openSession();
transaction = session.beginTransaction();
session.remove(entity);
transaction.commit();
session.close();
} catch (Exception e) {
transaction.rollback();
throw new RuntimeException("Error removing entity to the database", e);
} finally {
session.close();
}
}
}
57 changes: 53 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,30 +3,79 @@
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 {
private Session session = null;
private Transaction transaction = null;

public MessageDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
}

@Override
public Message create(Message entity) {
return null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(entity);
transaction.commit();
session.close();
return entity;
} catch (Exception e) {
transaction.rollback();
throw new RuntimeException("Error creating entity to the database", e);
} finally {
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't get an Entity from the DB", e);
}
}

@Override
public List<Message> getAll() {
return null;
List<Message> messages = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
messages = session.createQuery("FROM Message", Message.class).getResultList();
transaction.commit();
session.close();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Error retrieving all entities from the database", e);
} finally {
if (session != null) {
session.close();
}
}
return messages;
}

@Override
public void remove(Message entity) {

try {
session = factory.openSession();
transaction = session.beginTransaction();
session.remove(entity);
transaction.commit();
session.close();
} catch (Exception e) {
transaction.rollback();
throw new RuntimeException("Error removing entity to the database", e);
} finally {
session.close();
}
}
}
25 changes: 23 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,20 +2,41 @@

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 {
private Session session = null;
private Transaction transaction = null;

public MessageDetailsDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
}

@Override
public MessageDetails create(MessageDetails entity) {
return null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(entity);
transaction.commit();
session.close();
return entity;
} catch (Exception e) {
transaction.rollback();
throw new RuntimeException("Error creating entity to the database", e);
} finally {
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't get an Entity from the DB", e);
}
}
}
44 changes: 41 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,25 +3,63 @@
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 {
private Session session = null;
private Transaction transaction = null;

public SmileDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
}

@Override
public Smile create(Smile entity) {
return null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(entity);
transaction.commit();
session.close();
return entity;
} catch (Exception e) {
transaction.rollback();
throw new RuntimeException("Error creating entity to the database", e);
} finally {
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't get an Entity from the DB", e);
}
}

@Override
public List<Smile> getAll() {
return null;
List<Smile> smiles;
try {
session = factory.openSession();
transaction = session.beginTransaction();
smiles = session.createQuery("FROM Smile", Smile.class).getResultList();
transaction.commit();
session.close();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Error retrieving all entities from the database", e);
} finally {
if (session != null) {
session.close();
}
}
return smiles;
}
}
63 changes: 59 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,30 +3,85 @@
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 {
private Session session = null;
private Transaction transaction = null;

public UserDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
}

@Override
public User create(User entity) {
return null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(entity);
transaction.commit();
session.close();
return entity;
} catch (Exception e) {
transaction.rollback();
throw new RuntimeException("Error creating entity to the database", e);
} finally {
session.close();
}
}

@Override
public User get(Long id) {
return null;
try (Session session = factory.openSession()) {
Query<User> getEntityQuery = session.createQuery("from User u "
+ "left join fetch u.comments " + "where u.id = :id");
getEntityQuery.setParameter("id", id);
return getEntityQuery.getSingleResult();
} catch (Exception e) {
throw new RuntimeException("can't get an Entity from the DB", e);
}
}

@Override
public List<User> getAll() {
return null;
List<User> users;
try {
session = factory.openSession();
transaction = session.beginTransaction();
users = session.createQuery("FROM User u "
+ "left join fetch u.comments ",
User.class).getResultList();
transaction.commit();
session.close();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Error retrieving all entities from the database", e);
} finally {
if (session != null) {
session.close();
}
}
return users;
}

@Override
public void remove(User entity) {

try {
session = factory.openSession();
transaction = session.beginTransaction();
session.remove(entity);
transaction.commit();
session.close();
} catch (Exception e) {
transaction.rollback();
throw new RuntimeException("Error removing entity to the database", e);
} finally {
session.close();
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package core.basesyntax.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import java.util.List;

@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;

@OneToMany()
private List<Smile> smiles;

public Comment() {
}

public Long getId() {
return id;
}
Expand Down
Loading
Loading