From e8b63fe5336e040e796f1a6626dcb687c8a16494 Mon Sep 17 00:00:00 2001 From: Yura Khilko Date: Sun, 23 Jul 2023 10:40:46 +0300 Subject: [PATCH] added solution. --- pom.xml | 19 +++++++- src/main/java/mate/academy/Main.java | 15 ++++++ src/main/java/mate/academy/dao/MovieDao.java | 10 ++++ .../java/mate/academy/dao/MovieDaoImpl.java | 48 +++++++++++++++++++ .../exception/DataProcessingException.java | 11 +++++ src/main/java/mate/academy/model/Movie.java | 47 ++++++++++++++++++ .../mate/academy/service/MovieService.java | 9 ++++ .../academy/service/MovieServiceImpl.java | 22 +++++++++ .../java/mate/academy/util/HibernateUtil.java | 19 ++++++++ src/main/resources/hibernate.cfg.xml | 13 +++++ 10 files changed, 212 insertions(+), 1 deletion(-) 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/pom.xml b/pom.xml index f0412faf9..09fe1252b 100644 --- a/pom.xml +++ b/pom.xml @@ -37,17 +37,34 @@ 3.8.0 test - + + + + org.mockito + mockito-core + 5.4.0 + test org.hsqldb hsqldb 2.3.4 + + org.hibernate + hibernate-core + 6.2.6.Final + + + mysql + mysql-connector-java + 8.0.22 + diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 0058fbf96..140fc59c8 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 { + private static final Injector injector = Injector.getInstance("mate.academy"); + private static final MovieService movieService = (MovieService) + injector.getInstance(MovieService.class); + public static void main(String[] args) { + for (int i = 0; i < 4; i++) { + Movie movie = new Movie(); + movie.setTitle("title" + i); + movie.setDescription("description" + i); + System.out.println(movieService.add(movie)); + } + System.out.println(movieService.get(3L)); } } 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..918be16a0 --- /dev/null +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -0,0 +1,48 @@ +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.save(movie); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Can't insert Movie record into table movies. Movie " + + movie, e); + } finally { + if (session != null) { + session.close(); + } + } + return movie; + } + + @Override + public Optional get(Long id) { + SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + try (Session session = sessionFactory.openSession()) { + return Optional.ofNullable(session.get(Movie.class, id)); + } catch (Exception e) { + throw new DataProcessingException("Can't read Movie record from table movies by id " + + id, e); + } + } +} 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..71a3d05e8 --- /dev/null +++ b/src/main/java/mate/academy/exception/DataProcessingException.java @@ -0,0 +1,11 @@ +package mate.academy.exception; + +public class DataProcessingException extends RuntimeException { + public DataProcessingException(String message) { + super(message); + } + + 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..d79820728 --- /dev/null +++ b/src/main/java/mate/academy/model/Movie.java @@ -0,0 +1,47 @@ +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 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/MovieServiceImpl.java b/src/main/java/mate/academy/service/MovieServiceImpl.java new file mode 100644 index 000000000..0b52d781a --- /dev/null +++ b/src/main/java/mate/academy/service/MovieServiceImpl.java @@ -0,0 +1,22 @@ +package mate.academy.service; + +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 static 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..46a89ca18 --- /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() { + } + + public static SessionFactory getSessionFactory() { + return instance; + } + + 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 new file mode 100644 index 000000000..e4ff9918f --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,13 @@ + + + com.mysql.cj.jdbc.Driver + jdbc:mysql://sql7.freesqldatabase.com:3306/sql7633485 + org.hibernate.dialect.MySQL8Dialect + sql7633485 + erHDpGC53N + true + create-drop + + + + \ No newline at end of file