From b5a10a99523ca217f4d6735d67550f0d1627eb7b Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:36:00 +0300 Subject: [PATCH 01/17] pom.xml --- pom.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pom.xml b/pom.xml index f2dbc0637..5e98ab835 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,18 @@ hsqldb 2.3.4 + + org.hibernate + hibernate-core + 5.6.3.Final + + + javax.persistence + javax.persistence-api + 2.2 + + From c03a94d55f8490105f9d49021562a58bb1c0c6e0 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:40:11 +0300 Subject: [PATCH 02/17] hibernate.cfg.xml --- src/main/resources/hibernate.cfg.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/resources/hibernate.cfg.xml diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 000000000..1e5717d56 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,11 @@ + + + org.hsqldb.jdbcDriver + jdbc:hsqldb:mem:test + sa + + org.hibernate.dialect.HSQLDialect + true + update + + From d37cafe2e7f1f7740ee7a3785e79d8f14a725649 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:41:35 +0300 Subject: [PATCH 03/17] MovieDao.java --- src/main/java/mate/academy/dao/MovieDao.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/mate/academy/dao/MovieDao.java diff --git a/src/main/java/mate/academy/dao/MovieDao.java b/src/main/java/mate/academy/dao/MovieDao.java new file mode 100644 index 000000000..60c289038 --- /dev/null +++ b/src/main/java/mate/academy/dao/MovieDao.java @@ -0,0 +1,9 @@ +package mate.academy.dao; + +import mate.academy.model.Movie; +import java.util.Optional; + +public interface MovieDao { + Movie add(Movie movie); + Optional get(Long id); +} From c4769c6a7b043c3bdf5eb273be9e6e03510f1568 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:42:00 +0300 Subject: [PATCH 04/17] MovieDaoImpl.java --- .../mate/academy/dao/impl/MovieDaoImpl.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/mate/academy/dao/impl/MovieDaoImpl.java diff --git a/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java new file mode 100644 index 000000000..395165701 --- /dev/null +++ b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java @@ -0,0 +1,36 @@ +package mate.academy.dao.impl; + +import mate.academy.dao.MovieDao; +import mate.academy.exception.DataProcessingException; +import mate.academy.model.Movie; +import mate.academy.util.HibernateUtil; +import org.hibernate.Session; +import org.hibernate.Transaction; +import java.util.Optional; + +public class MovieDaoImpl implements MovieDao { + @Override + public Movie add(Movie movie) { + Transaction transaction = null; + try (Session session = HibernateUtil.getSessionFactory().openSession()) { + transaction = session.beginTransaction(); + session.save(movie); + transaction.commit(); + return movie; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Could not insert movie " + movie, e); + } + } + + @Override + public Optional get(Long id) { + try (Session session = HibernateUtil.getSessionFactory().openSession()) { + return Optional.ofNullable(session.get(Movie.class, id)); + } catch (Exception e) { + throw new DataProcessingException("Could not get movie by id " + id, e); + } + } +} From 56008c96cd9513700c5958ae328f23f86b1acc2b Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:42:52 +0300 Subject: [PATCH 05/17] DataProcessingException.java --- .../mate/academy/exception/DataProcessingException.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/mate/academy/exception/DataProcessingException.java diff --git a/src/main/java/mate/academy/exception/DataProcessingException.java b/src/main/java/mate/academy/exception/DataProcessingException.java new file mode 100644 index 000000000..63b937dc4 --- /dev/null +++ b/src/main/java/mate/academy/exception/DataProcessingException.java @@ -0,0 +1,7 @@ +package mate.academy.exception; + +public class DataProcessingException extends RuntimeException { + public DataProcessingException(String message, Throwable cause) { + super(message, cause); + } +} From 099d6cd2f9e676866ff85c62ab5e1b24d0dee96a Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:44:13 +0300 Subject: [PATCH 06/17] Injector.java --- src/main/java/mate/academy/lib/Injector.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/mate/academy/lib/Injector.java b/src/main/java/mate/academy/lib/Injector.java index c5008b336..cb7a6b19e 100644 --- a/src/main/java/mate/academy/lib/Injector.java +++ b/src/main/java/mate/academy/lib/Injector.java @@ -110,6 +110,7 @@ private void setValueToField(Field field, Object instanceOfClass, Object classTo throw new RuntimeException("Can't set value to field ", e); } } + /** * Scans all classes accessible from the context class loader which * belong to the given package and subpackages. @@ -119,7 +120,6 @@ private void setValueToField(Field field, Object instanceOfClass, Object classTo * @throws ClassNotFoundException if the class cannot be located * @throws IOException if I/O errors occur */ - private static List> getClasses(String packageName) throws IOException, ClassNotFoundException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); @@ -139,6 +139,7 @@ private static List> getClasses(String packageName) } return classes; } + /** * Recursive method used to find all classes in a given directory and subdirs. * @@ -147,7 +148,6 @@ private static List> getClasses(String packageName) * @return The classes * @throws ClassNotFoundException if the class cannot be located */ - private static List> findClasses(File directory, String packageName) throws ClassNotFoundException { List> classes = new ArrayList<>(); @@ -161,8 +161,7 @@ private static List> findClasses(File directory, String packageName) if (file.getName().contains(".")) { throw new RuntimeException("File name shouldn't consist point."); } - classes.addAll(findClasses(file, packageName + "." - + file.getName())); + classes.addAll(findClasses(file, packageName + "." + file.getName())); } else if (file.getName().endsWith(".class")) { classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); From 0f7284b999b10421a46781822162d1a61cd08c33 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:45:59 +0300 Subject: [PATCH 07/17] Movie.java --- src/main/java/mate/academy/model/Movie.java | 47 +++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/mate/academy/model/Movie.java diff --git a/src/main/java/mate/academy/model/Movie.java b/src/main/java/mate/academy/model/Movie.java new file mode 100644 index 000000000..8e28e4dea --- /dev/null +++ b/src/main/java/mate/academy/model/Movie.java @@ -0,0 +1,47 @@ +package mate.academy.model; + +import javax.persistence.*; + +@Entity +@Table(name = "movies") +public class Movie { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String title; + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Override + public String toString() { + return "Movie{" + + "id=" + id + + ", title='" + title + '\'' + + ", description='" + description + '\'' + + '}'; + } +} From 1c7c81a007804f77f503da21b61fd72f3b3ef786 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:47:25 +0300 Subject: [PATCH 08/17] MovieService.java --- src/main/java/mate/academy/service/MovieService.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/main/java/mate/academy/service/MovieService.java diff --git a/src/main/java/mate/academy/service/MovieService.java b/src/main/java/mate/academy/service/MovieService.java new file mode 100644 index 000000000..8a912ee5c --- /dev/null +++ b/src/main/java/mate/academy/service/MovieService.java @@ -0,0 +1,8 @@ +package mate.academy.service; + +import mate.academy.model.Movie; + +public interface MovieService { + Movie add(Movie movie); + Movie get(Long id); +} From fb83c2f0ce19becfbb6a68e77fcf0e5fe132ab63 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:47:51 +0300 Subject: [PATCH 09/17] MovieServiceImpl.java --- .../service/impl/MovieServiceImpl.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/mate/academy/service/impl/MovieServiceImpl.java diff --git a/src/main/java/mate/academy/service/impl/MovieServiceImpl.java b/src/main/java/mate/academy/service/impl/MovieServiceImpl.java new file mode 100644 index 000000000..213593b54 --- /dev/null +++ b/src/main/java/mate/academy/service/impl/MovieServiceImpl.java @@ -0,0 +1,23 @@ +package mate.academy.service.impl; + +import mate.academy.dao.MovieDao; +import mate.academy.lib.Inject; +import mate.academy.lib.Service; +import mate.academy.model.Movie; +import mate.academy.service.MovieService; + +@Service +public class MovieServiceImpl implements MovieService { + @Inject + private MovieDao movieDao; + + @Override + public Movie add(Movie movie) { + return movieDao.add(movie); + } + + @Override + public Movie get(Long id) { + return movieDao.get(id).orElseThrow(() -> new RuntimeException("Movie not found")); + } +} From 3b472df4cd4fbd769123f680d3d31e7913f19aff Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:49:47 +0300 Subject: [PATCH 10/17] HibernateUtil.java --- .../java/mate/academy/util/HibernateUtil.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/mate/academy/util/HibernateUtil.java diff --git a/src/main/java/mate/academy/util/HibernateUtil.java b/src/main/java/mate/academy/util/HibernateUtil.java new file mode 100644 index 000000000..19e14d1a1 --- /dev/null +++ b/src/main/java/mate/academy/util/HibernateUtil.java @@ -0,0 +1,24 @@ +package mate.academy.util; + +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +public class HibernateUtil { + private static final SessionFactory sessionFactory = buildSessionFactory(); + + private static SessionFactory buildSessionFactory() { + try { + return new Configuration().configure().buildSessionFactory(); + } catch (Throwable ex) { + throw new ExceptionInInitializerError(ex); + } + } + + public static SessionFactory getSessionFactory() { + return sessionFactory; + } + + private HibernateUtil() { + // Private constructor to prevent instantiation + } +} From cfa058e4f740962b35e73bc240dd1395fe789f33 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sat, 27 Jul 2024 07:50:26 +0300 Subject: [PATCH 11/17] Main.java --- src/main/java/mate/academy/Main.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 0058fbf96..789dcc672 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -1,7 +1,20 @@ package mate.academy; +import mate.academy.lib.Injector; +import mate.academy.model.Movie; +import mate.academy.service.MovieService; + public class Main { + private static final Injector injector = Injector.getInstance("mate.academy"); + public static void main(String[] args) { + MovieService movieService = (MovieService) injector.getInstance(MovieService.class); + + Movie movie = new Movie(); + movie.setTitle("Inception"); + movie.setDescription("A mind-bending thriller"); + movieService.add(movie); + System.out.println("Movie added: " + movieService.get(movie.getId())); } } From 3280b894d207ec1c2eb55a4d5c176b909a0d8f2c Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sun, 28 Jul 2024 07:37:19 +0300 Subject: [PATCH 12/17] MovieDao.java --- src/main/java/mate/academy/dao/MovieDao.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/mate/academy/dao/MovieDao.java b/src/main/java/mate/academy/dao/MovieDao.java index 60c289038..e5f725e39 100644 --- a/src/main/java/mate/academy/dao/MovieDao.java +++ b/src/main/java/mate/academy/dao/MovieDao.java @@ -1,9 +1,10 @@ package mate.academy.dao; -import mate.academy.model.Movie; import java.util.Optional; +import mate.academy.model.Movie; public interface MovieDao { Movie add(Movie movie); + Optional get(Long id); } From 38d4f448d8652d35c76bb32d7148c261574c00a5 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sun, 28 Jul 2024 07:37:40 +0300 Subject: [PATCH 13/17] MovieDaoImpl.java --- src/main/java/mate/academy/dao/impl/MovieDaoImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java index 395165701..becfd6a37 100644 --- a/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java @@ -1,12 +1,12 @@ package mate.academy.dao.impl; +import java.util.Optional; import mate.academy.dao.MovieDao; import mate.academy.exception.DataProcessingException; import mate.academy.model.Movie; import mate.academy.util.HibernateUtil; import org.hibernate.Session; import org.hibernate.Transaction; -import java.util.Optional; public class MovieDaoImpl implements MovieDao { @Override From 6e0dcc5c88c9d7a7aeaa1d37c807042b088b6ae7 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sun, 28 Jul 2024 07:38:02 +0300 Subject: [PATCH 14/17] MovieService.java --- src/main/java/mate/academy/service/MovieService.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/mate/academy/service/MovieService.java b/src/main/java/mate/academy/service/MovieService.java index 8a912ee5c..80e46c951 100644 --- a/src/main/java/mate/academy/service/MovieService.java +++ b/src/main/java/mate/academy/service/MovieService.java @@ -4,5 +4,6 @@ public interface MovieService { Movie add(Movie movie); + Movie get(Long id); } From d38b38473d78982ec7bd5c991e56d9e8adde21cb Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sun, 28 Jul 2024 07:38:24 +0300 Subject: [PATCH 15/17] HibernateUtil.java --- src/main/java/mate/academy/util/HibernateUtil.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/mate/academy/util/HibernateUtil.java b/src/main/java/mate/academy/util/HibernateUtil.java index 19e14d1a1..d939efad7 100644 --- a/src/main/java/mate/academy/util/HibernateUtil.java +++ b/src/main/java/mate/academy/util/HibernateUtil.java @@ -6,6 +6,10 @@ public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); + private HibernateUtil() { + // Private constructor to prevent instantiation + } + private static SessionFactory buildSessionFactory() { try { return new Configuration().configure().buildSessionFactory(); @@ -17,8 +21,4 @@ private static SessionFactory buildSessionFactory() { public static SessionFactory getSessionFactory() { return sessionFactory; } - - private HibernateUtil() { - // Private constructor to prevent instantiation - } } From 1657a9f327c83a1d3660f5c77950190d8848b7ed Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sun, 28 Jul 2024 07:38:46 +0300 Subject: [PATCH 16/17] Movie.java --- src/main/java/mate/academy/model/Movie.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/mate/academy/model/Movie.java b/src/main/java/mate/academy/model/Movie.java index 8e28e4dea..e26429301 100644 --- a/src/main/java/mate/academy/model/Movie.java +++ b/src/main/java/mate/academy/model/Movie.java @@ -1,6 +1,10 @@ package mate.academy.model; -import javax.persistence.*; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; @Entity @Table(name = "movies") @@ -12,6 +16,7 @@ public class Movie { private String title; private String description; + // Getters and setters public Long getId() { return id; } @@ -38,10 +43,10 @@ public void setDescription(String description) { @Override public String toString() { - return "Movie{" + - "id=" + id + - ", title='" + title + '\'' + - ", description='" + description + '\'' + - '}'; + return "Movie{" + + "id=" + id + + ", title='" + title + '\'' + + ", description='" + description + '\'' + + '}'; } } From 88c93d3e96ccad7809bfad22138693f79e1bb932 Mon Sep 17 00:00:00 2001 From: VladSlob <141657806+VladSlob@users.noreply.github.com> Date: Sun, 28 Jul 2024 07:40:15 +0300 Subject: [PATCH 17/17] MovieDaoImpl.java --- src/main/java/mate/academy/dao/impl/MovieDaoImpl.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java index becfd6a37..a35c152cd 100644 --- a/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java +++ b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java @@ -12,7 +12,9 @@ public class MovieDaoImpl implements MovieDao { @Override public Movie add(Movie movie) { Transaction transaction = null; - try (Session session = HibernateUtil.getSessionFactory().openSession()) { + Session session = null; + try { + session = HibernateUtil.getSessionFactory().openSession(); transaction = session.beginTransaction(); session.save(movie); transaction.commit(); @@ -22,6 +24,10 @@ public Movie add(Movie movie) { transaction.rollback(); } throw new DataProcessingException("Could not insert movie " + movie, e); + } finally { + if (session != null) { + session.close(); + } } }