From f8fa2f7019da5d28822116a94c3f41f92ea4959d Mon Sep 17 00:00:00 2001 From: Illya Pantazi Date: Wed, 17 Jul 2024 05:08:47 +0200 Subject: [PATCH] Added required hibernate dependencies created a hibernate.cfg.xml file created HibernateUtil class created Movie model class created MovieDao interface and MovieDaoImpl class created MovieService interface and MovieServiceImpl class created my custom unchecked DataProcessingException --- pom.xml | 8 +++++--- src/main/java/mate/academy/Main.java | 4 +--- src/main/java/mate/academy/dao/MovieDaoImpl.java | 3 ++- src/main/java/mate/academy/model/Movie.java | 8 +++----- .../mate/academy/services/MovieServiceImpl.java | 16 ++++++---------- src/main/resources/hibernate.cfg.xml | 4 ++-- 6 files changed, 19 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index c5a2e0226..074b0d287 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,8 @@ https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml + 22 + 22 @@ -34,19 +36,19 @@ org.mockito mockito-inline - 3.8.0 + 5.2.0 test org.mockito mockito-core - 4.4.0 + 5.12.0 test org.hibernate.orm hibernate-core - 6.4.4.Final + 6.5.2.Final org.hsqldb diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 4dea2629c..d78f40766 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -6,10 +6,8 @@ import mate.academy.services.MovieService; public class Main { - private static final Injector injector = Injector.getInstance("mate.academy"); - public static void main(String[] args) { - // MovieDao movieDao = (MovieDao) injector.getInstance(MovieDao.class); + Injector injector = Injector.getInstance("mate.academy"); MovieService movieService = (MovieService) injector.getInstance(MovieService.class); Map.of("Dune: Part Two", "Paul Atreides unites with Chani and the Fremen\\.\\.\\.", diff --git a/src/main/java/mate/academy/dao/MovieDaoImpl.java b/src/main/java/mate/academy/dao/MovieDaoImpl.java index 7abd341dd..ffac195cb 100644 --- a/src/main/java/mate/academy/dao/MovieDaoImpl.java +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -20,8 +20,9 @@ public Movie add(Movie movie) { try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); - movie = (Movie) session.save(movie); + session.persist(movie); transaction.commit(); + movie.setId((Long) session.getIdentifier(movie)); } catch (Exception e) { if (transaction != null) { transaction.rollback(); diff --git a/src/main/java/mate/academy/model/Movie.java b/src/main/java/mate/academy/model/Movie.java index 1b84e39f8..f63c8c594 100644 --- a/src/main/java/mate/academy/model/Movie.java +++ b/src/main/java/mate/academy/model/Movie.java @@ -4,25 +4,23 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; -import jakarta.persistence.Table; @Entity -@Table(name = "movie") public class Movie { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; + private Long id; private String title; private String description; public Movie() { } - public long getId() { + public Long getId() { return id; } - public void setId(long id) { + public void setId(Long id) { this.id = id; } diff --git a/src/main/java/mate/academy/services/MovieServiceImpl.java b/src/main/java/mate/academy/services/MovieServiceImpl.java index 584ae15db..1f520a32e 100644 --- a/src/main/java/mate/academy/services/MovieServiceImpl.java +++ b/src/main/java/mate/academy/services/MovieServiceImpl.java @@ -1,6 +1,5 @@ package mate.academy.services; -import java.util.Optional; import mate.academy.dao.MovieDao; import mate.academy.exceptions.DataProcessingException; import mate.academy.lib.Inject; @@ -18,12 +17,12 @@ public Movie add(Movie movie) { throw new DataProcessingException("The argument (a movie) or his fields is null :" + movie); } - if (movie.getId() != 0) { + if (movie.getId() != null) { throw new DataProcessingException("Incorrect operation, the movie already has an Id: " + movie); } Movie addedMoved = movieDao.add(movie); - if (addedMoved.getId() == 0) { + if (addedMoved.getId() == null) { throw new DataProcessingException("The error when adding movie, the Id is missing. " + addedMoved); } @@ -32,14 +31,11 @@ public Movie add(Movie movie) { @Override public Movie get(Long id) { - if (id == 0) { + if (id == null) { throw new DataProcessingException("The argument (an Id) is null."); } - Optional optionalMovie = movieDao.get(id); - if (optionalMovie.isEmpty()) { - throw new DataProcessingException("The error when getting movie by Id: " + id - + ", a movie is missing."); - } - return null; + return movieDao.get(id).orElseThrow(() + -> new DataProcessingException("The error when getting movie by Id: " + id + + ", a movie is missing.")); } } diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index 94252976b..0c75825d7 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -6,12 +6,12 @@ org.hibernate.dialect.MySQL8Dialect - jdbs.mysql://localhost:3306/my_db + jdbs.mysql://localhost:3306/my_db?serverTimeZone=UTC com.mysql.cj.jdbc.Driver root illya true - update + create-drop