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 cascades #1014

Closed
wants to merge 20 commits into from
41 changes: 36 additions & 5 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,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 java.util.List;

public class CommentDaoImpl extends AbstractDao implements CommentDao {
public CommentDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,21 +14,50 @@ public CommentDaoImpl(SessionFactory sessionFactory) {

@Override
public Comment create(Comment entity) {
return null;
Transaction transaction = null;
try (Session session = factory.openSession()) {
transaction = session.beginTransaction();
session.save(entity);
transaction.commit();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't insert Comment entity", e);
}
}

@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 Comment entity by id", e);
}
}

@Override
public List<Comment> getAll() {
return null;
try (Session session = factory.openSession()) {
return session.createQuery("from Comment", Comment.class).list();
} catch (Exception e) {
throw new RuntimeException("Can't get all Comment entities", e);
}
}

@Override
public void remove(Comment entity) {

Transaction transaction = null;
try (Session session = factory.openSession()) {
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete Comment entity", e);
}
}
}
42 changes: 37 additions & 5 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,11 @@

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 java.util.List;

public class MessageDaoImpl extends AbstractDao implements MessageDao {
public MessageDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,21 +15,50 @@ public MessageDaoImpl(SessionFactory sessionFactory) {

@Override
public Message create(Message entity) {
return null;
Transaction transaction = null;
try (Session session = factory.openSession()) {
transaction = session.beginTransaction();
session.save(entity);
transaction.commit();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't insert Message entity", e);
}
}

@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 Message entity by id", e);
}
}

@Override
public List<Message> getAll() {
return null;
try (Session session = factory.openSession()) {
return session.createQuery("from Message", Message.class).list();
} catch (Exception e) {
throw new RuntimeException("Can't get all Message entities", e);
}
}

@Override
public void remove(Message entity) {

Transaction transaction = null;
try (Session session = factory.openSession()) {
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete Message entity", e);
}
}
}
41 changes: 36 additions & 5 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,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 java.util.List;

public class UserDaoImpl extends AbstractDao implements UserDao {
public UserDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,21 +14,50 @@ public UserDaoImpl(SessionFactory sessionFactory) {

@Override
public User create(User entity) {
return null;
Transaction transaction = null;
try (Session session = factory.openSession()) {
transaction = session.beginTransaction();
session.save(entity);
transaction.commit();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't insert User entity", e);
}
}

@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("Can't get User entity by id", e);
}
}

@Override
public List<User> getAll() {
return null;
try (Session session = factory.openSession()) {
return session.createQuery("from User", User.class).list();
} catch (Exception e) {
throw new RuntimeException("Can't get all User entities", e);
}
}

@Override
public void remove(User entity) {

Transaction transaction = null;
try (Session session = factory.openSession()) {
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete User entity", e);
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,15 +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.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import java.util.List;

@Entity
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
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/model/Message.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
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.OneToOne;

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

@OneToOne(cascade = jakarta.persistence.CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "message_details_id")
private MessageDetails messageDetails;

public Long getId() {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/model/User.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
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.OneToMany;
import java.util.List;

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

@OneToMany(cascade = jakarta.persistence.CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "user_id")
private List<Comment> comments;

public Long getId() {
Expand Down
Loading