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

implemented task #987

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.0-SNAPSHOT</version>

<properties>
<jdk.version>17</jdk.version>
<jdk.version>22</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.checkstyle.plugin.configLocation>
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core.basesyntax;

import core.basesyntax.dao.UserDao;
import core.basesyntax.dao.impl.UserDaoImpl;
import core.basesyntax.model.Comment;
import core.basesyntax.model.Smile;
import core.basesyntax.model.User;
import org.hibernate.SessionFactory;

import java.util.List;

public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Smile smileFirst = new Smile();
smileFirst.setValue("by-by");
Smile smileSecond = new Smile();
smileSecond.setValue("fy-fy");

Comment commentFirst = new Comment();
commentFirst.setContent("tip top ty ru ru");
commentFirst.setSmiles(List.of(smileFirst, smileSecond));

User user = new User();
user.setUsername("Bob");
user.setComments(List.of(commentFirst));

UserDao userDao = new UserDaoImpl(sessionFactory);
userDao.create(user);
System.out.println(userDao.get(4L).toString());
}
}
55 changes: 50 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,31 +2,76 @@

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;

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 (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't create comment entity in DB "
+ 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("Can't get data from DB for comment with Id = " + id, e);
}
}

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

@Override
public void remove(Comment entity) {

Session session = null;
Transaction transaction = null;
try {
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 comment entity from DB", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
52 changes: 48 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,7 +3,9 @@
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;

public class MessageDaoImpl extends AbstractDao implements MessageDao {
public MessageDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,21 +14,63 @@ 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 message entity in DB "
+ entity.toString(), 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("Can't get data from DB for message with Id = " + id, e);
}
}

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

@Override
public void remove(Message entity) {

Session session = null;
Transaction transaction = null;
try {
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 messages entity from DB", e);
} finally {
if (session != null){
session.close();
}
}
}
}
27 changes: 25 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,32 @@ 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 messageDetails entity in DB", 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("Can't get data from DB for messageDetails with Id = " + id, e);
}
}
}
33 changes: 30 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,7 +3,9 @@
import core.basesyntax.dao.SmileDao;
import core.basesyntax.model.Smile;
import java.util.List;
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 +14,41 @@ 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 smile entity in DB", 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("Can't get data from DB for smile with Id = " + id, e);
}
}

@Override
public List<Smile> getAll() {
return null;
try (Session session = factory.openSession()){
return session.createQuery("from Smile", Smile.class).getResultList();
} catch (Exception e){
throw new RuntimeException("Can't get all smiles from DB", e);
}
}
}
52 changes: 48 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,7 +3,9 @@
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;

public class UserDaoImpl extends AbstractDao implements UserDao {
public UserDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,21 +14,63 @@ 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 user entity in DB "
+ entity.toString(), 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("Can't get data from DB for user with Id = " + id, e);
}
}

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

@Override
public void remove(User entity) {

Session session = null;
Transaction transaction = null;
try {
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 users entity from DB", e);
} finally {
if (session != null){
session.close();
}
}
}
}
Loading
Loading