Skip to content

Commit

Permalink
Done
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton committed Jul 16, 2024
1 parent 7777544 commit f229214
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 17 deletions.
54 changes: 50 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,15 @@

import core.basesyntax.dao.CommentDao;
import core.basesyntax.model.Comment;
import jakarta.persistence.Query;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import java.util.List;
import java.util.Optional;
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 +19,60 @@ 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 e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create entity to DB", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public Comment get(Long id) {
return null;
Optional<Comment> comment;
try (Session session = factory.openSession()) {
comment = Optional.ofNullable(session.get(Comment.class, id));
} catch (Exception e) {
throw new RuntimeException("Can't get user from DB", e);
}
return comment.get();
}

@Override
public List<Comment> getAll() {
return null;
Session session = factory.openSession();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Comment> criteriaQuery = criteriaBuilder.createQuery(Comment.class);
Root<Comment> employeeRoot = criteriaQuery.from(Comment.class);
criteriaQuery.select(employeeRoot);
Query query = session.createQuery(criteriaQuery);
return query.getResultList();
}

@Override
public void remove(Comment entity) {

Transaction transaction = null;
try (Session 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 entity from DB", e);
}
}
}
59 changes: 55 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 java.util.ArrayList;
import java.util.List;
import java.util.Optional;
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,68 @@ 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 e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create entity to DB", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public Message get(Long id) {
return null;
Optional<Message> message;
try (Session session = factory.openSession()) {
message = Optional.ofNullable(session.get(Message.class, id));
} catch (Exception e) {
throw new RuntimeException("Can't get user from DB", e);
}
return message.get();
}

@Override
public List<Message> getAll() {
return null;
List<Message> messages = new ArrayList<>();
Optional<Message> message;
int i = 1;
try (Session session = factory.openSession()) {
message = Optional.ofNullable(session.get(Message.class, i));
while (message.isPresent()) {
message = Optional.ofNullable(session.get(Message.class, i));
messages.add(message.get());
i++;
message = Optional.ofNullable(session.get(Message.class, i));
}
} catch (Exception e) {
throw new RuntimeException("Can't get user from DB", e);
}
return messages;
}

@Override
public void remove(Message entity) {

Transaction transaction = null;
try (Session 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 entity from DB", e);
}
}
}
29 changes: 27 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,34 @@ 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 e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create entity to DB", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public MessageDetails get(Long id) {
return null;
MessageDetails messageDetails;
try (Session session = factory.openSession()) {
messageDetails = session.get(MessageDetails.class, id);
} catch (Exception e) {
throw new RuntimeException("Can't get user from DB", e);
}
return messageDetails;
}
}
47 changes: 44 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 java.util.ArrayList;
import java.util.List;
import java.util.Optional;
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,53 @@ 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 e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create entity to DB", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public Smile get(Long id) {
return null;
Optional<Smile> smile;
try (Session session = factory.openSession()) {
smile = Optional.ofNullable(session.get(Smile.class, id));
} catch (Exception e) {
throw new RuntimeException("Can't get user from DB", e);
}
return smile.get();
}

@Override
public List<Smile> getAll() {
return null;
List<Smile> smiles = new ArrayList<>();
Optional<Smile> smile;
int i = 1;
try (Session session = factory.openSession()) {
smile = Optional.ofNullable(session.get(Smile.class, i));
while (smile.isPresent()) {
smile = Optional.ofNullable(session.get(Smile.class, i));
smiles.add(smile.get());
i++;
smile = Optional.ofNullable(session.get(Smile.class, i));
}
} catch (Exception e) {
throw new RuntimeException("Can't get user from DB", e);
}
return smiles;
}
}
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 @@ -2,8 +2,15 @@

import core.basesyntax.dao.UserDao;
import core.basesyntax.model.User;
import jakarta.persistence.Query;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import java.util.List;
import java.util.Optional;
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 +19,60 @@ 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 e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create entity to DB", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public User get(Long id) {
return null;
Optional<User> user;
try (Session session = factory.openSession()) {
user = Optional.ofNullable(session.get(User.class, id));
} catch (Exception e) {
throw new RuntimeException("Can't get user from DB", e);
}
return user.get();
}

@Override
public List<User> getAll() {
return null;
Session session = factory.openSession();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);
Root<User> employeeRoot = criteriaQuery.from(User.class);
criteriaQuery.select(employeeRoot);
Query query = session.createQuery(criteriaQuery);
return query.getResultList();
}

@Override
public void remove(User entity) {

Transaction transaction = null;
try (Session 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 entity from DB", e);
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
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.OneToMany;
import java.util.List;

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

@OneToMany(cascade = {CascadeType.MERGE, CascadeType.DETACH,
CascadeType.REFRESH}, fetch = FetchType.EAGER)

private List<Smile> smiles;

public Long getId() {
Expand Down
Loading

0 comments on commit f229214

Please sign in to comment.