Skip to content

Commit

Permalink
MVN CLEAN PACKAGE
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGrey17 committed Apr 3, 2024
1 parent 0e0d85d commit 7cc57d8
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 25 deletions.
3 changes: 0 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import core.basesyntax.dao.CommentDao;
import core.basesyntax.model.Comment;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
Expand Down Expand Up @@ -51,7 +49,6 @@ public Comment get(Long id) {
}
}


public List<Comment> getAll() {
try (Session session = factory.openSession()) {
String hql = "FROM Comment";
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import core.basesyntax.dao.MessageDao;
import core.basesyntax.model.Message;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
Expand Down Expand Up @@ -51,7 +49,6 @@ public Message get(Long id) {
}
}


@Override
public List<Message> getAll() {
try (Session session = factory.openSession()) {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import core.basesyntax.dao.SmileDao;
import core.basesyntax.model.Smile;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
Expand Down Expand Up @@ -38,7 +36,6 @@ public Smile create(Smile entity) {
return entity;
}


@Override
public Smile get(Long id) {
try (Session session = factory.openSession()) {
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import core.basesyntax.dao.UserDao;
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;
Expand All @@ -23,13 +21,13 @@ public User create(User entity) {
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.save(entity);
session.persist(entity);
transaction.commit();
} catch (Exception e) {
} catch (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Cannot create User " + entity);
throw new RuntimeException("Error while creating entity");
} finally {
if (transaction != null) {
session.close();
Expand Down Expand Up @@ -65,6 +63,27 @@ public List<User> getAll() {

@Override
public void remove(User entity) {

Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
User user = session.get(User.class, entity.getId());
if (user != null) {
session.delete(user);
} else {
throw new RuntimeException("Entity not found with id: " + entity.getId());
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to delete entity " + entity, e);
} finally {
if (session != null && session.isOpen()) {
session.close();
}
}
}
}
10 changes: 6 additions & 4 deletions src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package core.basesyntax.model;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.OneToMany;

import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import java.util.List;

@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;
@OneToMany
@ManyToMany(cascade = CascadeType.MERGE)
private List<Smile> smiles;

public Long getId() {
return id;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/core/basesyntax/model/Message.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package core.basesyntax.model;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/core/basesyntax/model/MessageDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

import java.time.LocalDateTime;

@Entity
public class MessageDetails {
@Id
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/core/basesyntax/model/Smile.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Table(name = "smiles")
@Entity
public class Smile {
@Id
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/core/basesyntax/model/User.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package core.basesyntax.model;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.OneToMany;

import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import java.util.ArrayList;
import java.util.List;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
@OneToMany
@ManyToMany(
cascade = {
CascadeType.MERGE,
CascadeType.PERSIST},
fetch = FetchType.EAGER)
private List<Comment> comments;

public User() {
this.comments = new ArrayList<>();
}

public Long getId() {
return id;
}
Expand Down

0 comments on commit 7cc57d8

Please sign in to comment.