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

Implement DAO methods and configure cascades for entity classes #948

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
4 changes: 2 additions & 2 deletions 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 SESSION_FACTORY = initSessionFactory();

private HibernateUtil() {
}
Expand All @@ -18,6 +18,6 @@ private static SessionFactory initSessionFactory() {
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
return SESSION_FACTORY;
}
}
56 changes: 52 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,66 @@ 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 (Exception exception) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException(
"Failed to create comment: " + entity, exception);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@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("Failed to retrieve comment with id: " + id, e);
}
}

@Override
public List<Comment> getAll() {
return null;
try (Session session = factory.openSession()) {
String hql = "FROM Comment";
Query<Comment> query = session.createQuery(hql, Comment.class);
return query.getResultList();
} catch (Exception e) {
throw new RuntimeException("Failed to retrieve 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 exception) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException(
"Failed to remove comment: " + entity, exception);
} finally {
if (session != null) {
session.close();
}
}
}
}
54 changes: 50 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,64 @@ 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 (Exception exception) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to create message: " + entity, exception);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@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("Failed to retrieve message with id: " + id, e);
}
}

@Override
public List<Message> getAll() {
return null;
try (Session session = factory.openSession()) {
String hql = "FROM Message";
Query<Message> query = session.createQuery(hql, Message.class);
return query.getResultList();
} catch (Exception e) {
throw new RuntimeException("Failed to retrieve 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 exception) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to remove message: " + entity, exception);
} 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();
} catch (Exception exception) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to create message details: " + entity, exception);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@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("Failed to retrieve message details with id: " + id, e);
}
}
}
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,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,43 @@ 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();
} catch (Exception exception) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to create smile: " + entity, exception);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@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("Failed to retrieve smile with id: " + id, e);
}
}

@Override
public List<Smile> getAll() {
return null;
try (Session session = factory.openSession()) {
String hql = "FROM Smile";
Query<Smile> query = session.createQuery(hql, Smile.class);
return query.getResultList();
} catch (Exception e) {
throw new RuntimeException("Failed to retrieve all smiles", e);
}
}
}
54 changes: 50 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,64 @@ 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();
} catch (Exception exception) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to create user: " + entity, exception);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@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("Failed to retrieve user with id: " + id, e);
}
}

@Override
public List<User> getAll() {
return null;
try (Session session = factory.openSession()) {
String hql = "FROM User";
Query<User> query = session.createQuery(hql, User.class);
return query.getResultList();
} catch (Exception e) {
throw new RuntimeException("Failed to retrieve 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 exception) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to remove user: " + entity, exception);
} finally {
if (session != null) {
session.close();
}
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
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 java.util.List;

@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
@ManyToMany
private List<Smile> smiles;

public Long getId() {
Expand Down
Loading
Loading