From 9ea0a7b39432a0400bf1f4559595b647d5526625 Mon Sep 17 00:00:00 2001 From: Volodymyr Mykychak Date: Wed, 9 Aug 2023 16:59:39 +0300 Subject: [PATCH 1/3] Commit tar hibernate-configuration-hw --- .github/workflows/ci.yml | 4 +- pom.xml | 10 ++++ src/main/java/mate/academy/Main.java | 17 ++++++- src/main/java/mate/academy/dao/MovieDao.java | 10 ++++ .../mate/academy/dao/impl/MovieDaoImpl.java | 45 +++++++++++++++++ .../exeption/DataProcessingException.java | 8 +++ src/main/java/mate/academy/model/Movie.java | 49 +++++++++++++++++++ .../mate/academy/service/MovieService.java | 9 ++++ .../service/impl/MovieServiceImpl.java | 23 +++++++++ .../java/mate/academy/util/HibernateUtil.java | 19 +++++++ src/main/resources/hibernate.cfg.xml | 13 +++++ .../java/mate/academy/FunctionalityTest.java | 2 - src/test/resources/hibernate.cfg.xml | 16 ++++-- src/test/resources/init_db.sql | 10 ++++ 14 files changed, 225 insertions(+), 10 deletions(-) create mode 100644 src/main/java/mate/academy/dao/MovieDao.java create mode 100644 src/main/java/mate/academy/dao/impl/MovieDaoImpl.java create mode 100644 src/main/java/mate/academy/exeption/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/impl/MovieServiceImpl.java create mode 100644 src/main/java/mate/academy/util/HibernateUtil.java create mode 100644 src/main/resources/hibernate.cfg.xml create mode 100644 src/test/resources/init_db.sql diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 68d34f27b..78b906585 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,10 +11,10 @@ jobs: with: ref: ${{github.event.pull_request.head.ref}} repository: ${{github.event.pull_request.head.repo.full_name}} - - name: Set up JDK 17 + - name: Set up JDK 11 uses: actions/setup-java@v2 with: - java-version: '17' + java-version: '11' distribution: 'adopt' cache: maven - name: Build with Maven diff --git a/pom.xml b/pom.xml index f0412faf9..674c627e5 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,16 @@ hsqldb 2.3.4 + + org.hibernate + hibernate-core + 5.4.27.Final + + + mysql + mysql-connector-java + 8.0.30 + diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 0058fbf96..44cbf4420 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -1,7 +1,22 @@ 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) { + public static final Injector injector = Injector.getInstance("mate.academy"); + public static void main(String[] args) { + MovieService movieService = (MovieService) injector.getInstance(MovieService.class); + Movie avatar = new Movie(); + avatar.setTitle("Foundation"); + avatar.setDescription("fantastic"); + movieService.add(avatar); + Movie titanic = new Movie(); + titanic.setTitle("Pulp fiction"); + titanic.setDescription("comedy"); + movieService.add(titanic); + System.out.println(movieService.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/impl/MovieDaoImpl.java b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java new file mode 100644 index 000000000..bd69dadd8 --- /dev/null +++ b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java @@ -0,0 +1,45 @@ +package mate.academy.dao.impl; + +import java.util.Optional; +import mate.academy.dao.MovieDao; +import mate.academy.exeption.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.save(movie); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Can't save movie to DB " + movie, e); + } finally { + if (session != null) { + session.close(); + } + } + return movie; + } + + @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("Can't get from database " + + "the movie with id: " + id, e); + } + } +} diff --git a/src/main/java/mate/academy/exeption/DataProcessingException.java b/src/main/java/mate/academy/exeption/DataProcessingException.java new file mode 100644 index 000000000..44908f006 --- /dev/null +++ b/src/main/java/mate/academy/exeption/DataProcessingException.java @@ -0,0 +1,8 @@ +package mate.academy.exeption; + +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..cdf7ab70b --- /dev/null +++ b/src/main/java/mate/academy/model/Movie.java @@ -0,0 +1,49 @@ +package mate.academy.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@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 + '\'' + '}'; + } +} 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..80e46c951 --- /dev/null +++ b/src/main/java/mate/academy/service/MovieService.java @@ -0,0 +1,9 @@ +package mate.academy.service; + +import mate.academy.model.Movie; + +public interface MovieService { + Movie add(Movie movie); + + Movie get(Long id); +} 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..e5da85639 --- /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).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..dae5853d2 --- /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 final 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..37d2da8af --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,13 @@ + + + org.hibernate.dialect.MySQL5Dialect + jdbc:mysql://localhost:3306/ticket_app?serverTimezone=UTC + com.mysql.cj.jdbc.Driver + user + password + true + create-drop + + + + diff --git a/src/test/java/mate/academy/FunctionalityTest.java b/src/test/java/mate/academy/FunctionalityTest.java index e26d210a0..22331044a 100644 --- a/src/test/java/mate/academy/FunctionalityTest.java +++ b/src/test/java/mate/academy/FunctionalityTest.java @@ -1,7 +1,6 @@ package mate.academy; import static org.mockito.Mockito.description; -import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mockStatic; @@ -130,7 +129,6 @@ public void add_closeSessionAndRollbackTransaction_ok() { mockSessionFactory(mockedSessionFactory); Object testMovie = getTestMovie(); Mockito.when(mockedSession.save(testMovie)).thenThrow(new RuntimeException()); - doThrow(new RuntimeException()).when(mockedSession).persist(testMovie); Class dataProcessingExceptionClass = getClass("DataProcessingException"); try { diff --git a/src/test/resources/hibernate.cfg.xml b/src/test/resources/hibernate.cfg.xml index b8ff2c8c1..5b2ee3f4a 100644 --- a/src/test/resources/hibernate.cfg.xml +++ b/src/test/resources/hibernate.cfg.xml @@ -1,10 +1,16 @@ - org.hsqldb.jdbcDriver - jdbc:hsqldb:mem - sa - - org.hibernate.dialect.HSQLDialect + org.hibernate.dialect.MySQL5Dialect + jdbc:mysql://localhost:3306/ticket_app?useLegacyDatetimeCode=false&serverTimezone=Europe/Kiev + + com.mysql.cj.jdbc.Driver + user + password true + update + + + + diff --git a/src/test/resources/init_db.sql b/src/test/resources/init_db.sql new file mode 100644 index 000000000..0b53c41ac --- /dev/null +++ b/src/test/resources/init_db.sql @@ -0,0 +1,10 @@ +CREATE DATABASE `ticket_app` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */; + +-- ticket_app.movies definition + +CREATE TABLE `movies` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `description` varchar(255) DEFAULT NULL, + `title` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; \ No newline at end of file From 18d93378f70e5303033af773d82e912d8f0d9510 Mon Sep 17 00:00:00 2001 From: Volodymyr Mykychak Date: Wed, 9 Aug 2023 17:37:45 +0300 Subject: [PATCH 2/3] Commit tar hibernate-configuration-hw --- src/main/resources/hibernate.cfg.xml | 2 +- src/{test => main}/resources/init_db.sql | 0 src/test/resources/hibernate.cfg.xml | 16 ---------------- 3 files changed, 1 insertion(+), 17 deletions(-) rename src/{test => main}/resources/init_db.sql (100%) delete mode 100644 src/test/resources/hibernate.cfg.xml diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index 37d2da8af..e6f627591 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -1,6 +1,6 @@ - org.hibernate.dialect.MySQL5Dialect + org.hibernate.dialect.MySQLDialect jdbc:mysql://localhost:3306/ticket_app?serverTimezone=UTC com.mysql.cj.jdbc.Driver user diff --git a/src/test/resources/init_db.sql b/src/main/resources/init_db.sql similarity index 100% rename from src/test/resources/init_db.sql rename to src/main/resources/init_db.sql diff --git a/src/test/resources/hibernate.cfg.xml b/src/test/resources/hibernate.cfg.xml deleted file mode 100644 index 5b2ee3f4a..000000000 --- a/src/test/resources/hibernate.cfg.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - org.hibernate.dialect.MySQL5Dialect - jdbc:mysql://localhost:3306/ticket_app?useLegacyDatetimeCode=false&serverTimezone=Europe/Kiev - - com.mysql.cj.jdbc.Driver - user - password - true - update - - - - - - From d89110989d67cf6c511216b417df5c6faa2e499c Mon Sep 17 00:00:00 2001 From: Volodymyr Mykychak Date: Wed, 9 Aug 2023 17:42:27 +0300 Subject: [PATCH 3/3] Commit tar hibernate-configuration-hw --- src/main/resources/hibernate.cfg.xml | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index e6f627591..b8ff2c8c1 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -1,13 +1,10 @@ - org.hibernate.dialect.MySQLDialect - jdbc:mysql://localhost:3306/ticket_app?serverTimezone=UTC - com.mysql.cj.jdbc.Driver - user - password + org.hsqldb.jdbcDriver + jdbc:hsqldb:mem + sa + + org.hibernate.dialect.HSQLDialect true - create-drop - -