Skip to content

Commit

Permalink
implemented methods and added annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Khrystyna authored and Khrystyna committed Mar 12, 2024
1 parent 7777544 commit e9453a4
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 19 deletions.
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.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
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 (Exception ex) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create entity: " + entity);
} 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 ex) {
throw new RuntimeException("Can't get comment with id: " + id);
}
}

@Override
public List<Comment> getAll() {
return null;
Session session = null;
List<Comment> commentList = null;
try {
session = factory.openSession();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Comment> criteriaQuery = criteriaBuilder.createQuery(Comment.class);
criteriaQuery.from(Comment.class);
commentList = session.createQuery(criteriaQuery).getResultList();
} catch (Exception ex) {
throw new RuntimeException("Can't get all comments from DB");
} finally {
if (session != null) {
session.close();
}
}
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 ex) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't remove entity: " + entity);
} finally {
if (session != null) {
session.close();
}
}
}
}
65 changes: 61 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.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
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,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();
} catch (Exception ex) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create message: " + entity);
} 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 ex) {
throw new RuntimeException("Can't get message with id: " + id);
}
}

@Override
public List<Message> getAll() {
return null;
List<Message> messagesList = null;
Session session = null;
try {
session = factory.openSession();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Message> messageCriteriaQuery =
criteriaBuilder.createQuery(Message.class);
messageCriteriaQuery.from(Message.class);
messagesList = session.createQuery(messageCriteriaQuery).getResultList();
} catch (Exception ex) {
throw new RuntimeException("Can't get all messages from DB");
} finally {
if (session != null) {
session.close();
}
}
return messagesList;
}

@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 ex) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't remove entity: " + entity);
} 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 ex) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create message details entity: " + entity);
} 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 ex) {
throw new RuntimeException("Can't get smile with id: " + id);
}
}
}
46 changes: 43 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import core.basesyntax.dao.SmileDao;
import core.basesyntax.model.Smile;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
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 +16,52 @@ 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 ex) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create smile: " + entity);
} 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 ex) {
throw new RuntimeException("Can't get smile with id: " + id);
}
}

@Override
public List<Smile> getAll() {
return null;
List<Smile> smileList = null;
Session session = null;
try {
session = factory.openSession();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Smile> smileCriteriaQuery = criteriaBuilder.createQuery(Smile.class);
smileCriteriaQuery.from(Smile.class);
smileList = session.createQuery(smileCriteriaQuery).getResultList();
} catch (Exception ex) {
throw new RuntimeException("Can't get all smiles from DB");
} finally {
if (session != null) {
session.close();
}
}
return smileList;
}
}
64 changes: 60 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import core.basesyntax.dao.UserDao;
import core.basesyntax.model.User;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
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 +16,73 @@ 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 ex) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create user: " + entity);
} 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 ex) {
throw new RuntimeException("Can't get user with id: " + id);
}
}

@Override
public List<User> getAll() {
return null;
List<User> userList = null;
Session session = null;
try {
session = factory.openSession();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);
criteriaQuery.from(User.class);
userList = session.createQuery(criteriaQuery).getResultList();
} catch (Exception ex) {
throw new RuntimeException("Can't get all users from DB");
} finally {
if (session != null) {
session.close();
}
}
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 ex) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't remove entity: " + entity);
} finally {
if (session != null) {
session.close();
}
}
}
}
Loading

0 comments on commit e9453a4

Please sign in to comment.