diff --git a/hibernate-project/pom.xml b/hibernate-project/pom.xml
index 1e57da8..1464a0c 100644
--- a/hibernate-project/pom.xml
+++ b/hibernate-project/pom.xml
@@ -69,6 +69,11 @@
jakarta.validation-api
3.1.0
+
+ org.slf4j
+ slf4j-simple
+ 2.0.17
+
com.mysql
mysql-connector-j
@@ -79,16 +84,6 @@
reflections
0.10.2
-
- org.slf4j
- slf4j-simple
- 2.0.17
-
-
- org.mockito
- mockito-core
- 5.17.0
-
diff --git a/hibernate-project/src/main/java/com/humanbooster/controller/ArticleController.java b/hibernate-project/src/main/java/com/humanbooster/controller/ArticleController.java
new file mode 100644
index 0000000..9cf0e42
--- /dev/null
+++ b/hibernate-project/src/main/java/com/humanbooster/controller/ArticleController.java
@@ -0,0 +1,14 @@
+package com.humanbooster.controller;
+
+import com.humanbooster.dao.GenericDao;
+import com.humanbooster.model.Article;
+import jakarta.ws.rs.Path;
+import org.hibernate.SessionFactory;
+
+@Path("/articles")
+public class ArticleController extends GenericControllerImpl {
+
+ public ArticleController(SessionFactory sessionFactory, GenericDao dao) {
+ super(sessionFactory,dao);
+ }
+}
diff --git a/hibernate-project/src/main/java/com/humanbooster/controller/GenericController.java b/hibernate-project/src/main/java/com/humanbooster/controller/GenericController.java
new file mode 100644
index 0000000..a7ceea2
--- /dev/null
+++ b/hibernate-project/src/main/java/com/humanbooster/controller/GenericController.java
@@ -0,0 +1,11 @@
+package com.humanbooster.controller;
+
+import java.util.List;
+
+public interface GenericController {
+ void create(T entity);
+ T read(ID id);
+ void update(ID id);
+ void delete(ID id);
+ List getAll();
+}
diff --git a/hibernate-project/src/main/java/com/humanbooster/controller/GenericControllerImpl.java b/hibernate-project/src/main/java/com/humanbooster/controller/GenericControllerImpl.java
new file mode 100644
index 0000000..c223c77
--- /dev/null
+++ b/hibernate-project/src/main/java/com/humanbooster/controller/GenericControllerImpl.java
@@ -0,0 +1,54 @@
+package com.humanbooster.controller;
+
+import com.humanbooster.dao.GenericDao;
+import jakarta.ws.rs.*;
+import jakarta.ws.rs.core.MediaType;
+import org.hibernate.SessionFactory;
+
+import java.util.List;
+
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+public abstract class GenericControllerImpl implements GenericController, GenericDao {
+
+ protected final SessionFactory sessionFactory;
+ private final GenericDao dao;
+
+ public GenericControllerImpl(SessionFactory sessionFactory, GenericDao dao) {
+ this.sessionFactory = sessionFactory;
+ this.dao = dao;
+ }
+
+ @POST
+ @Override
+ public void create(T entity) {
+ dao.create(entity);
+ }
+
+ @GET
+ @Path("/{id}")
+ @Override
+ public T read(@PathParam("id") ID id) {
+ return dao.read(id);
+ }
+
+ @PUT
+ @Path("/{id}")
+ @Override
+ public void update(@PathParam("id") ID id) {
+ dao.update(id);
+ }
+
+ @DELETE
+ @Path("/{id}")
+ @Override
+ public void delete(@PathParam("id") ID id) {
+ dao.delete(id);
+ }
+
+ @GET
+ @Override
+ public List getAll() {
+ return dao.getAll();
+ }
+}
diff --git a/hibernate-project/src/main/java/com/humanbooster/controller/UserController.java b/hibernate-project/src/main/java/com/humanbooster/controller/UserController.java
index ce661ae..4d59ad0 100644
--- a/hibernate-project/src/main/java/com/humanbooster/controller/UserController.java
+++ b/hibernate-project/src/main/java/com/humanbooster/controller/UserController.java
@@ -1,50 +1,25 @@
package com.humanbooster.controller;
import com.humanbooster.config.HibernateConfig;
+import com.humanbooster.dao.GenericDao;
import com.humanbooster.dao.UserDao;
import com.humanbooster.model.User;
+import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.hibernate.SessionFactory;
-import java.util.List;
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
-public class UserController {
+public class UserController extends GenericControllerImpl {
- SessionFactory sessionFactory = HibernateConfig.getSessionFactory();
-
- private final UserDao dao = new UserDao(sessionFactory);
-
- @GET
- public List getAll() {
- return dao.findAll();
+ public UserController() {
+ this(HibernateConfig.getSessionFactory(), new UserDao(HibernateConfig.getSessionFactory()));
}
- @GET
- @Path("/{id}")
- public User getById(@PathParam("id") Long id) {
- return dao.read(id);
- }
-
- @POST
- public void create(User person) {
- dao.create(person);
- }
-
- @PUT
- @Path("/{id}")
- public void update(@PathParam("id") Long id, User user) {
- user.setId(id);
- dao.update(user);
- }
-
- @DELETE
- @Path("/{id}")
- public void delete(@PathParam("id") Long id) {
- System.out.println();
- dao.delete(id);
+ public UserController(SessionFactory sessionFactory, UserDao userDao) {
+ super(sessionFactory, userDao);
}
}
diff --git a/hibernate-project/src/main/java/com/humanbooster/dao/GenericDao.java b/hibernate-project/src/main/java/com/humanbooster/dao/GenericDao.java
index fe59aa4..12c317a 100644
--- a/hibernate-project/src/main/java/com/humanbooster/dao/GenericDao.java
+++ b/hibernate-project/src/main/java/com/humanbooster/dao/GenericDao.java
@@ -5,7 +5,7 @@ import java.util.List;
public interface GenericDao {
void create(T entity);
T read(ID id);
- void update(T entity);
+ void update(ID id);
void delete(ID id);
- List findAll();
+ List getAll();
}
diff --git a/hibernate-project/src/main/java/com/humanbooster/dao/GenericDaoImpl.java b/hibernate-project/src/main/java/com/humanbooster/dao/GenericDaoImpl.java
index bd502f0..fefaf32 100644
--- a/hibernate-project/src/main/java/com/humanbooster/dao/GenericDaoImpl.java
+++ b/hibernate-project/src/main/java/com/humanbooster/dao/GenericDaoImpl.java
@@ -35,9 +35,10 @@ public abstract class GenericDaoImpl implements GenericDao {
}
@Override
- public void update(T entity) {
+ public void update(ID id) {
try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
+ T entity = session.get(entityClass, id);
session.merge(entity);
session.getTransaction().commit();
}
@@ -54,7 +55,7 @@ public abstract class GenericDaoImpl implements GenericDao {
}
@Override
- public List findAll() {
+ public List getAll() {
try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
List entities = session.createQuery("from " + entityClass.getName(), entityClass).list();
diff --git a/hibernate-project/src/main/java/com/humanbooster/model/Article.java b/hibernate-project/src/main/java/com/humanbooster/model/Article.java
index 1760911..5562ef1 100644
--- a/hibernate-project/src/main/java/com/humanbooster/model/Article.java
+++ b/hibernate-project/src/main/java/com/humanbooster/model/Article.java
@@ -1,5 +1,6 @@
package com.humanbooster.model;
+import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import java.time.LocalDate;
@@ -9,6 +10,7 @@ public class Article extends Publication {
@ManyToOne
@JoinColumn(name="author_id", nullable = false)
+ @JsonBackReference
private User author;
private int views = 0;
diff --git a/hibernate-project/src/main/java/com/humanbooster/model/User.java b/hibernate-project/src/main/java/com/humanbooster/model/User.java
index 49a6373..15c1562 100644
--- a/hibernate-project/src/main/java/com/humanbooster/model/User.java
+++ b/hibernate-project/src/main/java/com/humanbooster/model/User.java
@@ -1,5 +1,6 @@
package com.humanbooster.model;
+import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
@@ -19,6 +20,7 @@ public class User {
private String email;
@OneToMany(mappedBy="author", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
+ @JsonManagedReference
private List articles;
public User() {}
diff --git a/hibernate-project/src/main/java/com/humanbooster/service/AdService.java b/hibernate-project/src/main/java/com/humanbooster/service/AdService.java
index 622cad1..14c0525 100644
--- a/hibernate-project/src/main/java/com/humanbooster/service/AdService.java
+++ b/hibernate-project/src/main/java/com/humanbooster/service/AdService.java
@@ -15,8 +15,8 @@ public record AdService (AdDao adDao) {
return adDao.read(id);
}
- public void updateAd(Ad ad) {
- adDao.update(ad);
+ public void updateAd(Long id) {
+ adDao.update(id);
}
public void deleteAd(Long id) {
@@ -24,6 +24,6 @@ public record AdService (AdDao adDao) {
}
public List getAllAds() {
- return adDao.findAll();
+ return adDao.getAll();
}
}
diff --git a/hibernate-project/src/main/java/com/humanbooster/service/ArticleService.java b/hibernate-project/src/main/java/com/humanbooster/service/ArticleService.java
index 2e3b76e..784313d 100644
--- a/hibernate-project/src/main/java/com/humanbooster/service/ArticleService.java
+++ b/hibernate-project/src/main/java/com/humanbooster/service/ArticleService.java
@@ -15,8 +15,8 @@ public record ArticleService(ArticleDao articleDao) {
articleDao.read(id);
}
- public void updateArticle(Article article) {
- articleDao.update(article);
+ public void updateArticle(Long id) {
+ articleDao.update(id);
}
public void deleteArticle(Long id) {
@@ -24,7 +24,7 @@ public record ArticleService(ArticleDao articleDao) {
}
public List getAllArticles() {
- return articleDao.findAll();
+ return articleDao.getAll();
}
public Article findArticleByAuthor(String author) {
diff --git a/hibernate-project/src/main/java/com/humanbooster/service/UserService.java b/hibernate-project/src/main/java/com/humanbooster/service/UserService.java
index 7a0b1f6..64c939c 100644
--- a/hibernate-project/src/main/java/com/humanbooster/service/UserService.java
+++ b/hibernate-project/src/main/java/com/humanbooster/service/UserService.java
@@ -15,8 +15,8 @@ public record UserService (UserDao userDao) {
userDao.read(id);
}
- public void updateUser(User user) {
- userDao.update(user);
+ public void updateUser(Long id) {
+ userDao.update(id);
}
public void deleteUser(Long id) {
@@ -24,7 +24,7 @@ public record UserService (UserDao userDao) {
}
public List getAllUsers() {
- return userDao.findAll();
+ return userDao.getAll();
}
public User findUserByEmail(String email) {
diff --git a/hibernate-project/test.http b/hibernate-project/test.http
new file mode 100644
index 0000000..b3066bb
--- /dev/null
+++ b/hibernate-project/test.http
@@ -0,0 +1,14 @@
+### GET request to example server
+GET http://localhost/users
+
+###
+POST http://localhost/users/
+Content-Type: application/json
+
+{
+ "name": "John Doe",
+ "email": "john.doe@example.com"
+}
+
+###
+DELETE http://localhost/articles/13