From 2c7e49495ba93bb6831a856bf1f33788f2ab9d03 Mon Sep 17 00:00:00 2001 From: Vitalii Pavlyk Date: Fri, 6 Sep 2024 17:07:12 +0300 Subject: [PATCH 01/10] added dependencies for hibernate and mysql --- pom.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pom.xml b/pom.xml index f2dbc0637..14156e1b4 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,16 @@ hsqldb 2.3.4 + + org.hibernate.orm + hibernate-core + 6.6.0.Final + + + com.mysql + mysql-connector-j + 8.3.0 + From d7d432492012eba7abdfa91f5a2a4ec552523223 Mon Sep 17 00:00:00 2001 From: Vitalii Pavlyk Date: Fri, 6 Sep 2024 17:08:33 +0300 Subject: [PATCH 02/10] added custom exception --- .../academy/exception/DataProcessingException.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/java/mate/academy/exception/DataProcessingException.java 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); + } +} From 3776e4ff8fa393bd905a8edaa5103a5b4116854c Mon Sep 17 00:00:00 2001 From: Vitalii Pavlyk Date: Fri, 6 Sep 2024 17:09:11 +0300 Subject: [PATCH 03/10] added hibernate.cfg.xml file. --- src/main/resources/hibernate.cfg.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main/resources/hibernate.cfg.xml diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 000000000..c0b6bdf45 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,13 @@ + + + org.hibernate.dialect.MySQLDialect + jdbc:mysql://localhost:3306/test?serverTimezone=UTC + com.mysql.cj.jdbc.Driver + root + F6Rf@N34,djA3i67 + true + create-drop + + + + \ No newline at end of file From e5889dcc412f4f043114b75d4e4aa7f731288f10 Mon Sep 17 00:00:00 2001 From: Vitalii Pavlyk Date: Fri, 6 Sep 2024 17:11:33 +0300 Subject: [PATCH 04/10] Created HibernateUtil class to initialize sessionFactory --- .../java/mate/academy/util/HibernateUtil.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/mate/academy/util/HibernateUtil.java 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..1e6aab373 --- /dev/null +++ b/src/main/java/mate/academy/util/HibernateUtil.java @@ -0,0 +1,24 @@ +package mate.academy.util; + +import mate.academy.exception.DataProcessingException; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +public class HibernateUtil { + private static final SessionFactory sessionFactory = initSessionFactory(); + + private HibernateUtil() { + } + + private static SessionFactory initSessionFactory() { + try { + return new Configuration().configure().buildSessionFactory(); + } catch (Exception e) { + throw new DataProcessingException("Can't create session factory ", e); + } + } + + public static SessionFactory getSessionFactory() { + return sessionFactory; + } +} From 4e0adbf8c6c0a8b6fa9aabb335012b564298f8ff Mon Sep 17 00:00:00 2001 From: Vitalii Pavlyk Date: Fri, 6 Sep 2024 17:12:22 +0300 Subject: [PATCH 05/10] Created an instance of MovieService using injector and test all methods from MovieService --- src/main/java/mate/academy/Main.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 0058fbf96..f42365384 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -1,7 +1,26 @@ 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) { + private static final Injector injector = Injector.getInstance("mate.academy"); + public static void main(String[] args) { + MovieService movieService = (MovieService) injector.getInstance(MovieService.class); + Movie firstMovie = new Movie("Modern Java", "Best practises of coding"); + Movie secondMovie = new Movie("Harry Potter and the Philosopher's Stone", + "It follows Harry Potter, a young wizard who discovers his magical heritage " + + "on his eleventh birthday when he receives a letter of acceptance to " + + "Hogwarts School of Witchcraft and Wizardry."); + Movie thirdMovie = new Movie("The Little Prince", + "The story follows a young prince who visits various planets, " + + "including Earth, and addresses themes of loneliness, friendship, love, " + + "and loss."); + movieService.add(firstMovie); + movieService.add(secondMovie); + movieService.add(thirdMovie); + movieService.get(2L); } } From fef395eea873736fb1a35606fdd68d3fe41916f7 Mon Sep 17 00:00:00 2001 From: Vitalii Pavlyk Date: Fri, 6 Sep 2024 17:15:27 +0300 Subject: [PATCH 06/10] Created Movie model and implemented parameters, constructor, annotations. --- src/main/java/mate/academy/model/Movie.java | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/mate/academy/model/Movie.java 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..800408a48 --- /dev/null +++ b/src/main/java/mate/academy/model/Movie.java @@ -0,0 +1,63 @@ +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 Movie(String title, String description) { + this.title = title; + this.description = description; + } + + public Movie(Long id, String title, String description) { + this.id = id; + this.title = title; + this.description = 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 + '\'' + '}'; + } +} From 70c66d01db33bfcb85e898e40b825beb1113472e Mon Sep 17 00:00:00 2001 From: Vitalii Pavlyk Date: Fri, 6 Sep 2024 17:16:22 +0300 Subject: [PATCH 07/10] Created MovieDao interface with methods. --- src/main/java/mate/academy/dao/MovieDao.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/mate/academy/dao/MovieDao.java 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); +} From 9987598e468e4591ff110c6cc4751df03fdd651d Mon Sep 17 00:00:00 2001 From: Vitalii Pavlyk Date: Fri, 6 Sep 2024 17:17:12 +0300 Subject: [PATCH 08/10] Created implementation of MovieDao. --- .../java/mate/academy/dao/MovieDaoImpl.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/mate/academy/dao/MovieDaoImpl.java 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..69919701f --- /dev/null +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -0,0 +1,45 @@ +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.save(movie); + transaction.commit(); + } catch (RuntimeException e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException(String.format( + "Cannot save movie:%s 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 (RuntimeException e) { + throw new DataProcessingException(String.format( + "Cannot find movie with id:%d in DB", id), e); + } + } +} From 06183db408551734e1077aab1d12bb8a3a8561be Mon Sep 17 00:00:00 2001 From: Vitalii Pavlyk Date: Fri, 6 Sep 2024 17:17:39 +0300 Subject: [PATCH 09/10] Created MovieService interface with methods. --- src/main/java/mate/academy/service/MovieService.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/mate/academy/service/MovieService.java 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); +} From fc8be43451c7d6da873786d0b21d539180ec2ced Mon Sep 17 00:00:00 2001 From: Vitalii Pavlyk Date: Fri, 6 Sep 2024 17:18:10 +0300 Subject: [PATCH 10/10] Created implementation of MovieService. --- .../academy/service/MovieServiceImpl.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/mate/academy/service/MovieServiceImpl.java 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..a2b3f03ea --- /dev/null +++ b/src/main/java/mate/academy/service/MovieServiceImpl.java @@ -0,0 +1,24 @@ +package mate.academy.service; + +import mate.academy.dao.MovieDao; +import mate.academy.exception.DataProcessingException; +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; + + @Override + public Movie add(Movie movie) { + return movieDao.add(movie); + } + + @Override + public Movie get(Long id) { + return movieDao.get(id).orElseThrow(() -> new DataProcessingException("Cannot find movie " + + "with id:" + id)); + } +}