Skip to content

Commit

Permalink
solved task
Browse files Browse the repository at this point in the history
  • Loading branch information
Leanchers committed Jun 1, 2024
1 parent 7777544 commit fe7b009
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 18 deletions.
55 changes: 51 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,77 @@
import core.basesyntax.dao.CommentDao;
import core.basesyntax.model.Comment;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class CommentDaoImpl extends AbstractDao implements CommentDao {

public CommentDaoImpl(SessionFactory sessionFactory) {
super(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 (HibernateException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create the entity", e);
} 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(e);
}
}

@Override
public List<Comment> getAll() {
return null;
List<Comment> comments;
try (Session session = factory.openSession()) {
comments = session.createQuery("FROM Comment", Comment.class).list();
} catch (Exception e) {
throw new RuntimeException(e);
}
return comments;
}

@Override
public void remove(Comment entity) {

Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
} catch (HibernateException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete the entity", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
55 changes: 51 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,77 @@
import core.basesyntax.dao.MessageDao;
import core.basesyntax.model.Message;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class MessageDaoImpl extends AbstractDao implements MessageDao {

public MessageDaoImpl(SessionFactory sessionFactory) {
super(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 (HibernateException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create the entity", e);
} 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(e);
}
}

@Override
public List<Message> getAll() {
return null;
List<Message> message;
try (Session session = factory.openSession()) {
message = session.createQuery("FROM Message", Message.class).list();
} catch (Exception e) {
throw new RuntimeException(e);
}
return message;
}

@Override
public void remove(Message entity) {

Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
} catch (HibernateException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete the entity", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
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,20 +2,45 @@

import core.basesyntax.dao.MessageDetailsDao;
import core.basesyntax.model.MessageDetails;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class MessageDetailsDaoImpl extends AbstractDao implements MessageDetailsDao {

public MessageDetailsDaoImpl(SessionFactory sessionFactory) {
super(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 (HibernateException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create the entity", e);
} 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(e);
}
}
}
37 changes: 34 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,56 @@
import core.basesyntax.dao.SmileDao;
import core.basesyntax.model.Smile;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class SmileDaoImpl extends AbstractDao implements SmileDao {

public SmileDaoImpl(SessionFactory sessionFactory) {
super(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 (HibernateException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create the entity", e);
} 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(e);
}
}

@Override
public List<Smile> getAll() {
return null;
List<Smile> smile;
try (Session session = factory.openSession()) {
smile = session.createQuery("FROM Smile", Smile.class).list();
} catch (Exception e) {
throw new RuntimeException(e);
}
return smile;
}
}
55 changes: 51 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,77 @@
import core.basesyntax.dao.UserDao;
import core.basesyntax.model.User;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class UserDaoImpl extends AbstractDao implements UserDao {

public UserDaoImpl(SessionFactory sessionFactory) {
super(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 (HibernateException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create the entity", e);
} 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(e);
}
}

@Override
public List<User> getAll() {
return null;
List<User> users;
try (Session session = factory.openSession()) {
users = session.createQuery("FROM User", User.class).list();
} catch (Exception e) {
throw new RuntimeException(e);
}
return users;
}

@Override
public void remove(User entity) {

Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
} catch (HibernateException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete the entity", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package core.basesyntax.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
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
@JoinTable(
name = "comment_smile",
joinColumns = @JoinColumn(name = "comment_id"),
inverseJoinColumns = @JoinColumn(name = "smile_id")
)
private List<Smile> smiles;

public Long getId() {
Expand Down
Loading

0 comments on commit fe7b009

Please sign in to comment.