Skip to content

Commit

Permalink
hibernate-cascade-solution V1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikname2303 committed Apr 2, 2024
1 parent 7777544 commit 0f879c7
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 31 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
<version>6.4.4.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
Expand Down
50 changes: 46 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package core.basesyntax.dao.impl;

import core.basesyntax.dao.CommentDao;
import core.basesyntax.exception.DataProcessingException;
import core.basesyntax.model.Comment;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

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

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

@Override
public Comment get(Long id) {
return null;
try (Session session = factory.openSession()) {
return session.get(Comment.class, id);
} catch (Exception e) {
throw new DataProcessingException("Can't get comment by id: " + id, e);
}
}

@Override
public List<Comment> getAll() {
return null;
try (Session session = factory.openSession()) {
String hql = "FROM Comment";
Query<Comment> fromComment = session.createQuery(hql, Comment.class);
return fromComment.getResultList();
} catch (Exception e) {
throw new DataProcessingException("Can't get comments ", e);
}
}

@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 DataProcessingException("Can't remove comment", e);
}
}
}

53 changes: 47 additions & 6 deletions src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,73 @@
package core.basesyntax.dao.impl;

import core.basesyntax.dao.MessageDao;
import core.basesyntax.exception.DataProcessingException;
import core.basesyntax.model.Message;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

public class MessageDaoImpl extends AbstractDao implements MessageDao {
public MessageDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
}

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

@Override
public Message get(Long id) {
return null;
try (Session session = factory.openSession()) {
return session.get(Message.class, id);
} catch (Exception e) {
throw new DataProcessingException("Can't get message by id: " + id, e);
}
}

@Override
public List<Message> getAll() {
return null;
try (Session session = factory.openSession()) {
String hql = "FROM Message";
Query<Message> fromComment = session.createQuery(hql, Message.class);
return fromComment.getResultList();
} catch (Exception e) {
throw new DataProcessingException("Can't get messages ", e);
}
}

@Override
public void remove(Message entity) {

public void remove(Message message) {
Transaction transaction = null;
try (Session session = factory.openSession()) {
transaction = session.beginTransaction();
session.remove(message);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Can't remove message, everyone will see that=)", e);
}
}
}
30 changes: 27 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
package core.basesyntax.dao.impl;

import core.basesyntax.dao.MessageDetailsDao;
import core.basesyntax.exception.DataProcessingException;
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) {
super(sessionFactory);
}

@Override
public MessageDetails create(MessageDetails entity) {
return null;
public MessageDetails create(MessageDetails messageDetails) {
Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(messageDetails);
transaction.commit();
return messageDetails;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Can`t create new messageDetails", e);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public MessageDetails get(Long id) {
return null;
try (Session session = factory.openSession()) {
return session.get(MessageDetails.class, id);
} catch (Exception e) {
throw new DataProcessingException("Can`t get smile with this ID: " + id);
}
}
}
39 changes: 35 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,58 @@
package core.basesyntax.dao.impl;

import core.basesyntax.dao.SmileDao;
import core.basesyntax.exception.DataProcessingException;
import core.basesyntax.model.Smile;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

public class SmileDaoImpl extends AbstractDao implements SmileDao {
public SmileDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
}

@Override
public Smile create(Smile entity) {
return null;
public Smile create(Smile smile) {
Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(smile);
transaction.commit();
return smile;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Can`t create new smile");
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public Smile get(Long id) {
return null;
try (Session session = factory.openSession()) {
return session.get(Smile.class, id);
} catch (Exception e) {
throw new DataProcessingException("Can`t get smile with this ID: " + id);
}
}

@Override
public List<Smile> getAll() {
return null;
try (Session session = factory.openSession()) {
String hql = "FROM Smile";
Query<Smile> fromSmile = session.createQuery(hql, Smile.class);
return fromSmile.getResultList();
} catch (Exception e) {
throw new DataProcessingException("Can`t get smiles", e);
}
}
}
57 changes: 51 additions & 6 deletions src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,77 @@
package core.basesyntax.dao.impl;

import core.basesyntax.dao.UserDao;
import core.basesyntax.exception.DataProcessingException;
import core.basesyntax.model.User;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

public class UserDaoImpl extends AbstractDao implements UserDao {
public UserDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
}

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

@Override
public User get(Long id) {
return null;
try (Session session = factory.openSession()) {
return session.get(User.class, id);
} catch (Exception e) {
throw new DataProcessingException("Can't get message by id: " + id, e);
}
}

@Override
public List<User> getAll() {
return null;
try (Session session = factory.openSession()) {
String hql = "FROM Comment";
Query<User> fromUser = session.createQuery(hql, User.class);
if (fromUser != null) {
return fromUser.getResultList();
}
return new ArrayList<>();
} catch (Exception e) {
throw new DataProcessingException("Can't get comments ", e);
}
}

@Override
public void remove(User entity) {

public void remove(User user) {
Transaction transaction = null;
try (Session session = factory.openSession()) {
transaction = session.beginTransaction();
session.remove(user);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Can't remove user", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax.exception;

public class DataProcessingException extends RuntimeException {
public DataProcessingException(String message) {
super(message);
}

public DataProcessingException(String message, Throwable cause) {
super(message, cause);
}
}
Loading

0 comments on commit 0f879c7

Please sign in to comment.