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

Task: hibernate-cascade. Executor: Oleksii Bashkirov. #995

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
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
Expand Down Expand Up @@ -79,4 +84,3 @@
</pluginManagement>
</build>
</project>

9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/dao/CommentDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package core.basesyntax.dao;

import core.basesyntax.model.Comment;
import java.util.List;

public interface CommentDao extends GenericDao<Comment> {

Comment create(Comment comment);

Comment get(Long id);

List<Comment> getAll();

void remove(Comment comment);
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/dao/MessageDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package core.basesyntax.dao;

import core.basesyntax.model.Message;
import java.util.List;

public interface MessageDao extends GenericDao<Message> {

Message create(Message entity);

Message get(Long id);

List<Message> getAll();

void remove(Message entity);
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package core.basesyntax.dao;

import core.basesyntax.model.User;
import java.util.List;

public interface UserDao extends GenericDao<User> {

User create(User user);

User get(Long id);

List<User> getAll();

void remove(User user);
}
45 changes: 45 additions & 0 deletions src/main/java/core/basesyntax/dao/impl/AbstractDao.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
package core.basesyntax.dao.impl;

import java.util.function.Consumer;
import java.util.function.Function;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public abstract class AbstractDao {
protected final SessionFactory factory;

protected AbstractDao(SessionFactory sessionFactory) {
this.factory = sessionFactory;
}

protected <T> T executeInsideTransaction(Function<Session, T> function) {
Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
T result = function.apply(session);
transaction.commit();
return result;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Transaction failed: ", e);
} finally {
if (session != null) {
session.close();
}
}
}

protected void executeInsideTransaction(Consumer<Session> consumer) {
Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
consumer.accept(session);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Transaction failed: ", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
24 changes: 20 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package core.basesyntax.dao.impl;

import static core.basesyntax.util.HqlQueries.GET_ALL_COMMENTS;

import core.basesyntax.dao.CommentDao;
import core.basesyntax.model.Comment;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

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

@Override
public Comment create(Comment entity) {
return null;
return executeInsideTransaction(session -> {
session.persist(entity);
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("Can't get comment by id: " + id, e);
}
}

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

@Override
public void remove(Comment entity) {

executeInsideTransaction(session -> {
session.remove(entity);
});
}
}
24 changes: 20 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package core.basesyntax.dao.impl;

import static core.basesyntax.util.HqlQueries.GET_ALL_MESSAGES;

import core.basesyntax.dao.MessageDao;
import core.basesyntax.model.Message;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

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

@Override
public Message create(Message entity) {
return null;
return executeInsideTransaction(session -> {
session.persist(entity);
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("Can't get message by id: " + id, e);
}
}

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

@Override
public void remove(Message entity) {

executeInsideTransaction(session -> {
session.remove(entity);
});
}
}
12 changes: 10 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,6 +2,7 @@

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

public class MessageDetailsDaoImpl extends AbstractDao implements MessageDetailsDao {
Expand All @@ -11,11 +12,18 @@ public MessageDetailsDaoImpl(SessionFactory sessionFactory) {

@Override
public MessageDetails create(MessageDetails entity) {
return null;
return executeInsideTransaction(session -> {
session.persist(entity);
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("Can't get message details by id: " + id, e);
}
}
}
20 changes: 17 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package core.basesyntax.dao.impl;

import static core.basesyntax.util.HqlQueries.GET_ALL_SMILES;

import core.basesyntax.dao.SmileDao;
import core.basesyntax.model.Smile;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class SmileDaoImpl extends AbstractDao implements SmileDao {
Expand All @@ -12,16 +15,27 @@ public SmileDaoImpl(SessionFactory sessionFactory) {

@Override
public Smile create(Smile entity) {
return null;
return executeInsideTransaction(session -> {
session.persist(entity);
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("Can't get smile by id: " + id, e);
}
}

@Override
public List<Smile> getAll() {
return null;
try (Session session = factory.openSession()) {
return session.createQuery(GET_ALL_SMILES, Smile.class).list();
} catch (Exception e) {
throw new RuntimeException("Can't get all smiles", e);
}
}
}
33 changes: 29 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package core.basesyntax.dao.impl;

import static core.basesyntax.util.HqlQueries.GET_ALL_USERS;

import core.basesyntax.dao.UserDao;
import core.basesyntax.model.User;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

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

@Override
public User create(User entity) {
return null;
return executeInsideTransaction(session -> {
session.persist(entity);
return entity;
});
}

@Override
public User get(Long id) {
return null;
try (Session session = factory.openSession()) {
User user = session.get(User.class, id);
if (user != null) {
Hibernate.initialize(user.getComments());
}
return user;
} catch (Exception e) {
throw new RuntimeException("Can't get user by id: " + id, e);
}
}

@Override
public List<User> getAll() {
return null;
try (Session session = factory.openSession()) {
List<User> users = session.createQuery(GET_ALL_USERS, User.class).list();
for (User user : users) {
Hibernate.initialize(user.getComments());
}
return users;
} catch (Exception e) {
throw new RuntimeException("Can't get all users", e);
}
}

@Override
public void remove(User entity) {

executeInsideTransaction(session -> {
session.remove(entity);
});
}
}
11 changes: 9 additions & 2 deletions src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package core.basesyntax.model;

import static jakarta.persistence.GenerationType.IDENTITY;

import jakarta.persistence.Entity;
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)
@GeneratedValue(strategy = IDENTITY)
private Long id;

private String content;

@OneToMany()
private List<Smile> smiles;

public Long getId() {
Expand Down
Loading
Loading