From b38b61b360d33b663e91afd1f3c5868a17feb14d Mon Sep 17 00:00:00 2001 From: Nikita Khilobok Date: Tue, 1 Oct 2024 19:48:54 +0300 Subject: [PATCH] hw is done, please check --- mem.lck | Bin 0 -> 16 bytes mem.log | 0 mem.properties | 5 ++ mem.script | 45 +++++++++++++++ pom.xml | 11 ++++ src/main/java/mate/academy/Main.java | 12 ++++ src/main/java/mate/academy/dao/MovieDao.java | 12 ++++ .../java/mate/academy/dao/MovieDaoImpl.java | 52 ++++++++++++++++++ .../exception/DataProcessingException.java | 7 +++ src/main/java/mate/academy/lib/Dao.java | 3 + src/main/java/mate/academy/lib/Inject.java | 3 + src/main/java/mate/academy/lib/Injector.java | 16 +++--- src/main/java/mate/academy/lib/Service.java | 3 + src/main/java/mate/academy/model/Movie.java | 50 +++++++++++++++++ .../mate/academy/service/MovieService.java | 11 ++++ .../academy/service/MovieServiceImpl.java | 21 +++++++ .../java/mate/academy/util/HibernateUtil.java | 19 +++++++ src/main/resources/hibernate.cfg.xml | 18 ++++++ 18 files changed, 280 insertions(+), 8 deletions(-) create mode 100644 mem.lck create mode 100644 mem.log create mode 100644 mem.properties create mode 100644 mem.script create mode 100644 src/main/java/mate/academy/dao/MovieDao.java create mode 100644 src/main/java/mate/academy/dao/MovieDaoImpl.java create mode 100644 src/main/java/mate/academy/exception/DataProcessingException.java create mode 100644 src/main/java/mate/academy/model/Movie.java create mode 100644 src/main/java/mate/academy/service/MovieService.java create mode 100644 src/main/java/mate/academy/service/MovieServiceImpl.java create mode 100644 src/main/java/mate/academy/util/HibernateUtil.java create mode 100644 src/main/resources/hibernate.cfg.xml diff --git a/mem.lck b/mem.lck new file mode 100644 index 0000000000000000000000000000000000000000..cb42eb5b293d51cec6a5937fad681876da453937 GIT binary patch literal 16 XcmeYW4)pQyclKsrV4URf+lT`IB}xQx literal 0 HcmV?d00001 diff --git a/mem.log b/mem.log new file mode 100644 index 000000000..e69de29bb diff --git a/mem.properties b/mem.properties new file mode 100644 index 000000000..d4c2453bf --- /dev/null +++ b/mem.properties @@ -0,0 +1,5 @@ +#HSQL Database Engine 2.3.4 +#Tue Oct 01 19:47:09 IDT 2024 +modified=no +tx_timestamp=0 +version=2.3.4 diff --git a/mem.script b/mem.script new file mode 100644 index 000000000..ed6fd37bf --- /dev/null +++ b/mem.script @@ -0,0 +1,45 @@ +SET DATABASE UNIQUE NAME HSQLDB9248F21C7F +SET DATABASE GC 0 +SET DATABASE DEFAULT RESULT MEMORY ROWS 0 +SET DATABASE EVENT LOG LEVEL 0 +SET DATABASE TRANSACTION CONTROL LOCKS +SET DATABASE DEFAULT ISOLATION LEVEL READ COMMITTED +SET DATABASE TRANSACTION ROLLBACK ON CONFLICT TRUE +SET DATABASE TEXT TABLE DEFAULTS '' +SET DATABASE SQL NAMES FALSE +SET DATABASE SQL REFERENCES FALSE +SET DATABASE SQL SIZE TRUE +SET DATABASE SQL TYPES FALSE +SET DATABASE SQL TDC DELETE TRUE +SET DATABASE SQL TDC UPDATE TRUE +SET DATABASE SQL TRANSLATE TTI TYPES TRUE +SET DATABASE SQL TRANSLATE TTI TYPES TRUE +SET DATABASE SQL CONCAT NULLS TRUE +SET DATABASE SQL UNIQUE NULLS TRUE +SET DATABASE SQL CONVERT TRUNCATE TRUE +SET DATABASE SQL AVG SCALE 0 +SET DATABASE SQL DOUBLE NAN TRUE +SET FILES WRITE DELAY 500 MILLIS +SET FILES BACKUP INCREMENT TRUE +SET FILES CACHE SIZE 10000 +SET FILES CACHE ROWS 50000 +SET FILES SCALE 32 +SET FILES LOB SCALE 32 +SET FILES DEFRAG 0 +SET FILES NIO TRUE +SET FILES NIO SIZE 256 +SET FILES LOG TRUE +SET FILES LOG SIZE 50 +CREATE USER SA PASSWORD DIGEST 'd41d8cd98f00b204e9800998ecf8427e' +ALTER USER SA SET LOCAL TRUE +CREATE SCHEMA PUBLIC AUTHORIZATION DBA +ALTER SEQUENCE SYSTEM_LOBS.LOB_ID RESTART WITH 1 +SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC +GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.SQL_IDENTIFIER TO PUBLIC +GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.YES_OR_NO TO PUBLIC +GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.TIME_STAMP TO PUBLIC +GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CARDINAL_NUMBER TO PUBLIC +GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CHARACTER_DATA TO PUBLIC +GRANT DBA TO SA +SET SCHEMA SYSTEM_LOBS +INSERT INTO BLOCKS VALUES(0,2147483647,0) diff --git a/pom.xml b/pom.xml index f2dbc0637..5cb140235 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,17 @@ + + org.hibernate.orm + hibernate-core + 6.6.1.Final + + + com.mysql + mysql-connector-j + 9.0.0 + + org.apache.commons commons-lang3 diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 0058fbf96..82b420065 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -1,7 +1,19 @@ package mate.academy; +import mate.academy.lib.Injector; +import mate.academy.model.Movie; +import mate.academy.service.MovieService; + public class Main { public static void main(String[] args) { + MovieService movieService = (MovieService) Injector.getInstance(MovieService.class); + Movie movie = new Movie(); + movie.setTitle("Furious"); + movie.setDescription("Awesome movie"); + + Movie saveMovie = movieService.add(movie); + Movie getMovie = movieService.get(1L); + System.out.println(getMovie); } } 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..7b1817b49 --- /dev/null +++ b/src/main/java/mate/academy/dao/MovieDao.java @@ -0,0 +1,12 @@ +package mate.academy.dao; + +import java.util.Optional; +import mate.academy.lib.Dao; +import mate.academy.model.Movie; + +@Dao +public interface MovieDao { + Movie add(Movie movie); + + Optional get(Long id); +} diff --git a/src/main/java/mate/academy/dao/MovieDaoImpl.java b/src/main/java/mate/academy/dao/MovieDaoImpl.java new file mode 100644 index 000000000..268e52f5b --- /dev/null +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -0,0 +1,52 @@ +package mate.academy.dao; + +import java.util.Optional; +import mate.academy.exception.DataProcessingException; +import mate.academy.lib.Dao; +import mate.academy.model.Movie; +import mate.academy.util.HibernateUtil; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; + +@Dao +public class MovieDaoImpl implements MovieDao { + @Override + public Movie add(Movie movie) { + SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + Session session = null; + Transaction transaction = null; + try { + session = sessionFactory.openSession(); + transaction = session.beginTransaction(); + session.persist(movie); + transaction.commit(); + return movie; + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Failed to save movie: " + movie); + } finally { + if (session != null) { + session.close(); + } + } + } + + @Override + public Optional get(Long id) { + SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + Session session = null; + try { + session = sessionFactory.openSession(); + return Optional.ofNullable(session.get(Movie.class, id)); + } catch (Exception e) { + throw new DataProcessingException("Failed to get movie with id: " + id); + } finally { + if (session != null) { + session.close(); + } + } + } +} 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..ea7bbfda3 --- /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) { + super(message); + } +} diff --git a/src/main/java/mate/academy/lib/Dao.java b/src/main/java/mate/academy/lib/Dao.java index f558d09aa..6c89c7098 100644 --- a/src/main/java/mate/academy/lib/Dao.java +++ b/src/main/java/mate/academy/lib/Dao.java @@ -1,8 +1,11 @@ package mate.academy.lib; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Dao { } diff --git a/src/main/java/mate/academy/lib/Inject.java b/src/main/java/mate/academy/lib/Inject.java index dfd2a0486..b6684e85c 100644 --- a/src/main/java/mate/academy/lib/Inject.java +++ b/src/main/java/mate/academy/lib/Inject.java @@ -1,8 +1,11 @@ package mate.academy.lib; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Inject { } diff --git a/src/main/java/mate/academy/lib/Injector.java b/src/main/java/mate/academy/lib/Injector.java index c5008b336..827d32aeb 100644 --- a/src/main/java/mate/academy/lib/Injector.java +++ b/src/main/java/mate/academy/lib/Injector.java @@ -13,8 +13,8 @@ public class Injector { private static final Map injectors = new HashMap<>(); - private final Map, Object> instanceOfClasses = new HashMap<>(); - private final List> classes = new ArrayList<>(); + private static final Map, Object> instanceOfClasses = new HashMap<>(); + private static final List> classes = new ArrayList<>(); private Injector(String mainPackageName) { try { @@ -33,7 +33,7 @@ public static Injector getInstance(String mainPackageName) { return injector; } - public Object getInstance(Class certainInterface) { + public static Object getInstance(Class certainInterface) { Object newInstanceOfClass = null; Class clazz = findClassExtendingInterface(certainInterface); Object instanceOfCurrentClass = createInstance(clazz); @@ -57,7 +57,7 @@ public Object getInstance(Class certainInterface) { return newInstanceOfClass; } - private Class findClassExtendingInterface(Class certainInterface) { + private static Class findClassExtendingInterface(Class certainInterface) { for (Class clazz : classes) { Class[] interfaces = clazz.getInterfaces(); for (Class singleInterface : interfaces) { @@ -73,7 +73,7 @@ private Class findClassExtendingInterface(Class certainInterface) { + " interface and has valid annotation (Dao or Service)"); } - private Object getNewInstance(Class certainClass) { + private static Object getNewInstance(Class certainClass) { if (instanceOfClasses.containsKey(certainClass)) { return instanceOfClasses.get(certainClass); } @@ -82,7 +82,7 @@ private Object getNewInstance(Class certainClass) { return newInstance; } - private boolean isFieldInitialized(Field field, Object instance) { + private static boolean isFieldInitialized(Field field, Object instance) { field.setAccessible(true); try { return field.get(instance) != null; @@ -91,7 +91,7 @@ private boolean isFieldInitialized(Field field, Object instance) { } } - private Object createInstance(Class clazz) { + private static Object createInstance(Class clazz) { Object newInstance; try { Constructor classConstructor = clazz.getConstructor(); @@ -102,7 +102,7 @@ private Object createInstance(Class clazz) { return newInstance; } - private void setValueToField(Field field, Object instanceOfClass, Object classToInject) { + private static void setValueToField(Field field, Object instanceOfClass, Object classToInject) { try { field.setAccessible(true); field.set(instanceOfClass, classToInject); diff --git a/src/main/java/mate/academy/lib/Service.java b/src/main/java/mate/academy/lib/Service.java index 94010448d..7348180d2 100644 --- a/src/main/java/mate/academy/lib/Service.java +++ b/src/main/java/mate/academy/lib/Service.java @@ -1,8 +1,11 @@ package mate.academy.lib; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Service { } 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..351d5570d --- /dev/null +++ b/src/main/java/mate/academy/model/Movie.java @@ -0,0 +1,50 @@ +package mate.academy.model; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import mate.academy.lib.Inject; + +@Entity +@Inject +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 + '\'' + + '}'; + } +} 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..dd24e49b4 --- /dev/null +++ b/src/main/java/mate/academy/service/MovieService.java @@ -0,0 +1,11 @@ +package mate.academy.service; + +import mate.academy.lib.Service; +import mate.academy.model.Movie; + +@Service +public interface MovieService { + Movie add(Movie movie); + + Movie get(Long id); +} diff --git a/src/main/java/mate/academy/service/MovieServiceImpl.java b/src/main/java/mate/academy/service/MovieServiceImpl.java new file mode 100644 index 000000000..da9251f46 --- /dev/null +++ b/src/main/java/mate/academy/service/MovieServiceImpl.java @@ -0,0 +1,21 @@ +package mate.academy.service; + +import mate.academy.dao.MovieDao; +import mate.academy.lib.Injector; +import mate.academy.lib.Service; +import mate.academy.model.Movie; + +@Service +public class MovieServiceImpl implements MovieService { + private MovieDao movieDao = (MovieDao) Injector.getInstance(MovieDao.class); + + @Override + public Movie add(Movie movie) { + return movieDao.add(movie); + } + + @Override + public Movie get(Long id) { + return movieDao.get(id).orElse(null); + } +} 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..2989bb876 --- /dev/null +++ b/src/main/java/mate/academy/util/HibernateUtil.java @@ -0,0 +1,19 @@ +package mate.academy.util; + +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +public class HibernateUtil { + private static SessionFactory instance = initSessionFactory(); + + private HibernateUtil() { + } + + private static SessionFactory initSessionFactory() { + return new Configuration().configure().buildSessionFactory(); + } + + public static SessionFactory getSessionFactory() { + return instance; + } +} diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 000000000..3e1449c37 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,18 @@ + + + + + + org.hibernate.dialect.MySQL8Dialect + jdbc:mysql://localhost:3306/cinema?serverTimezone=UTC + com.mysql.cj.jdbc.Driver + root + Nk102030! + true + update + + + + \ No newline at end of file