From a63d4bc1d6634a68db3d46f8092afb7d5630628b Mon Sep 17 00:00:00 2001 From: Bohdan Date: Sun, 25 Aug 2024 22:11:32 +0300 Subject: [PATCH 1/3] Help me with tests --- 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 | 13 +++++ src/main/java/mate/academy/dao/MovieDao.java | 10 ++++ .../java/mate/academy/dao/MovieDaoImpl.java | 43 ++++++++++++++ .../exception/DataProcessingException.java | 7 +++ src/main/java/mate/academy/model/Movie.java | 54 ++++++++++++++++++ .../mate/academy/services/MovieService.java | 9 +++ .../academy/services/MovieServiceImpl.java | 26 +++++++++ .../java/mate/academy/util/HibernateUtil.java | 16 ++++++ src/main/resources/hibernate.cfg.xml | 14 +++++ 14 files changed, 253 insertions(+) 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/services/MovieService.java create mode 100644 src/main/java/mate/academy/services/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..06618df4373696bfd67fadd62099692b7fc01860 GIT binary patch literal 16 XcmeYW4)pQyclKsrV4T?XMy&||CV~YL 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..495b48fe8 --- /dev/null +++ b/mem.properties @@ -0,0 +1,5 @@ +#HSQL Database Engine 2.3.4 +#Sun Aug 25 22:00:47 EEST 2024 +modified=no +tx_timestamp=0 +version=2.3.4 diff --git a/mem.script b/mem.script new file mode 100644 index 000000000..0230aea9f --- /dev/null +++ b/mem.script @@ -0,0 +1,45 @@ +SET DATABASE UNIQUE NAME HSQLDB918AE8E83A +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..a9cb62d12 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,17 @@ hsqldb 2.3.4 + + + org.hibernate.orm + hibernate-core + 6.4.4.Final + + + mysql + mysql-connector-java + 8.0.33 + diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 0058fbf96..bb6a94ae5 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.dao.MovieDao; +import mate.academy.lib.Injector; +import mate.academy.model.Movie; + public class Main { + private static final Injector injector = Injector.getInstance("mate.academy.dao"); + public static void main(String[] args) { + MovieDao movieDao = (MovieDao) injector.getInstance(MovieDao.class); + + Movie movie = new Movie(); + movie.setTitle("FILM"); + movie.setDescription("NORM"); + movieDao.add(movie); + System.out.println(movieDao.get(1L)); } } 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..e5f725e39 --- /dev/null +++ b/src/main/java/mate/academy/dao/MovieDao.java @@ -0,0 +1,10 @@ +package mate.academy.dao; + +import java.util.Optional; +import mate.academy.model.Movie; + +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..aee6f779e --- /dev/null +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -0,0 +1,43 @@ +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.Transaction; + +@Dao +public class MovieDaoImpl implements MovieDao { + @Override + public Movie add(Movie movie) { + Session session = null; + Transaction transaction = null; + try { + session = HibernateUtil.getSessionFactory().openSession(); + transaction = session.beginTransaction(); + session.persist(movie); + transaction.commit(); + } catch (DataProcessingException e) { + if(transaction != null) { + transaction.rollback(); + } + } finally { + if (session != null) { + session.close(); + } + } + return movie; + } + + @Override + public Optional get(Long id) { + Session session = HibernateUtil.getSessionFactory().openSession(); + Movie movie = session.get(Movie.class, id); + session.close(); + + return Optional.ofNullable(movie); + } + +} 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); + } +} 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..5ccdc9071 --- /dev/null +++ b/src/main/java/mate/academy/model/Movie.java @@ -0,0 +1,54 @@ +package mate.academy.model; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +@Entity +@Table(name = "movies") +public class Movie { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String title; + private String description; + + public Movie() { + + } + + 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; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Override + public String toString() { + return "Movie{" + + "id=" + id + + ", title='" + title + '\'' + + ", description='" + description + '\'' + + '}'; + } +} diff --git a/src/main/java/mate/academy/services/MovieService.java b/src/main/java/mate/academy/services/MovieService.java new file mode 100644 index 000000000..74e8ed7e9 --- /dev/null +++ b/src/main/java/mate/academy/services/MovieService.java @@ -0,0 +1,9 @@ +package mate.academy.services; + +import mate.academy.model.Movie; + +public interface MovieService { + Movie add(Movie movie); + + Movie get(Long id); +} diff --git a/src/main/java/mate/academy/services/MovieServiceImpl.java b/src/main/java/mate/academy/services/MovieServiceImpl.java new file mode 100644 index 000000000..471736ca2 --- /dev/null +++ b/src/main/java/mate/academy/services/MovieServiceImpl.java @@ -0,0 +1,26 @@ +package mate.academy.services; + +import mate.academy.dao.MovieDao; +import mate.academy.lib.Inject; +import mate.academy.lib.Service; +import mate.academy.model.Movie; + +@Service +public class MovieServiceImpl implements MovieService { + @Inject + private MovieDao movieDao; + + public MovieServiceImpl(MovieDao movieDao) { + this.movieDao = movieDao; + } + + @Override + public Movie add(Movie movie) { + return movieDao.add(movie); + } + + @Override + public Movie get(Long id) { + return movieDao.get(id).get(); + } +} 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..df3a01328 --- /dev/null +++ b/src/main/java/mate/academy/util/HibernateUtil.java @@ -0,0 +1,16 @@ +package mate.academy.util; + +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +public class HibernateUtil { + private static final SessionFactory sessionFactory = initSessionFactory(); + + private static SessionFactory initSessionFactory() { + return new Configuration().configure().buildSessionFactory(); + } + + public static SessionFactory getSessionFactory() { + return sessionFactory; + } +} diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 000000000..966acb070 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,14 @@ + + + + org.hibernate.dialect.MySQLDialect + jdbc:mysql://localhost:3306/test?serverTimezone=UTC + com.mysql.cj.jdbc.Driver + root + bodyaA20041@ + true + create-drop + + + + \ No newline at end of file From 23c102468cf318bb31a8ea4e9cfbefe0e54f364a Mon Sep 17 00:00:00 2001 From: Bohdan Date: Sun, 25 Aug 2024 22:13:14 +0300 Subject: [PATCH 2/3] Help me with tests --- src/main/java/mate/academy/dao/MovieDaoImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/mate/academy/dao/MovieDaoImpl.java b/src/main/java/mate/academy/dao/MovieDaoImpl.java index aee6f779e..7099d8a29 100644 --- a/src/main/java/mate/academy/dao/MovieDaoImpl.java +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -20,7 +20,7 @@ public Movie add(Movie movie) { session.persist(movie); transaction.commit(); } catch (DataProcessingException e) { - if(transaction != null) { + if (transaction != null) { transaction.rollback(); } } finally { From 53017c54a64c5ef58fceb4107e373b3ae6924d45 Mon Sep 17 00:00:00 2001 From: Bohdan Date: Mon, 26 Aug 2024 13:55:05 +0300 Subject: [PATCH 3/3] Help me with tests --- mem.lck | Bin 16 -> 16 bytes src/main/java/mate/academy/dao/MovieDaoImpl.java | 3 ++- .../java/mate/academy/util/HibernateUtil.java | 4 ++++ src/main/resources/hibernate.cfg.xml | 2 +- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/mem.lck b/mem.lck index 06618df4373696bfd67fadd62099692b7fc01860..00f7c72bdba694d03efe0516697767f980feb920 100644 GIT binary patch literal 16 XcmeYW4)pQyclKsrV4T<&*c1r>B~S#e literal 16 XcmeYW4)pQyclKsrV4T?XMy&||CV~YL diff --git a/src/main/java/mate/academy/dao/MovieDaoImpl.java b/src/main/java/mate/academy/dao/MovieDaoImpl.java index 7099d8a29..98f4ae1a9 100644 --- a/src/main/java/mate/academy/dao/MovieDaoImpl.java +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -19,10 +19,11 @@ public Movie add(Movie movie) { transaction = session.beginTransaction(); session.persist(movie); transaction.commit(); - } catch (DataProcessingException e) { + } catch (Exception e) { if (transaction != null) { transaction.rollback(); } + throw new DataProcessingException("error while adding movie", e); } finally { if (session != null) { session.close(); diff --git a/src/main/java/mate/academy/util/HibernateUtil.java b/src/main/java/mate/academy/util/HibernateUtil.java index df3a01328..682ff26e3 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 = initSessionFactory(); + private HibernateUtil() { + + } + private static SessionFactory initSessionFactory() { return new Configuration().configure().buildSessionFactory(); } diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index 966acb070..b8d1376c5 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -1,7 +1,7 @@ - org.hibernate.dialect.MySQLDialect + org.hibernate.dialect.MySQL8Dialect jdbc:mysql://localhost:3306/test?serverTimezone=UTC com.mysql.cj.jdbc.Driver root