From 4fcdea41616b479345262948c8f51a98759f4a41 Mon Sep 17 00:00:00 2001 From: ivan hromovyi Date: Sat, 5 Aug 2023 01:01:48 +0200 Subject: [PATCH] implemented hibernate two dao methods --- pom.xml | 11 +++++ .../mate/academy/DataProcessingException.java | 7 +++ src/main/java/mate/academy/Main.java | 15 +++++- src/main/java/mate/academy/dao/MovieDao.java | 10 ++++ .../java/mate/academy/dao/MovieDaoImpl.java | 49 +++++++++++++++++++ src/main/java/mate/academy/model/Movie.java | 47 ++++++++++++++++++ .../java/mate/academy/util/HibernateUtil.java | 16 ++++++ src/main/resources/hibernate.cfg.xml | 13 +++++ 8 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 src/main/java/mate/academy/DataProcessingException.java 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/model/Movie.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..0578f8d1c 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,18 @@ hsqldb 2.3.4 + + org.hibernate + hibernate-core + 5.4.27.Final + + + mysql + mysql-connector-java + 8.0.16 + + diff --git a/src/main/java/mate/academy/DataProcessingException.java b/src/main/java/mate/academy/DataProcessingException.java new file mode 100644 index 000000000..611c5682a --- /dev/null +++ b/src/main/java/mate/academy/DataProcessingException.java @@ -0,0 +1,7 @@ +package mate.academy; + +public class DataProcessingException extends RuntimeException { + public DataProcessingException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 0058fbf96..4fc3dbd44 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 { - public static void main(String[] args) { + private static final Injector injector = Injector.getInstance("mate.academy"); + private static final MovieDao movieDao = (MovieDao) injector.getInstance(MovieDao.class); + public static void main(String[] args) { + String title = "Night crawler"; + String description = "terrifying photographer"; + Movie movie = new Movie(); + movie.setTitle(title); + movie.setDescription(description); + System.out.println(movieDao.add(movie)); + System.out.println(movieDao.get(movie.getId())); } } 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..4232df981 --- /dev/null +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -0,0 +1,49 @@ +package mate.academy.dao; + +import java.util.Optional; +import mate.academy.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(); + return movie; + } catch (RuntimeException e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Couldn't add movie " + movie, e); + } finally { + if (session != null) { + session.close(); + } + } + } + + @Override + public Optional get(Long id) { + Session session = null; + try { + session = HibernateUtil.getSessionFactory().openSession(); + return Optional.ofNullable(session.get(Movie.class, id)); + } catch (RuntimeException e) { + throw new DataProcessingException("Couldn't get movie by id: " + id, e); + } finally { + if (session != null) { + session.close(); + } + } + } +} 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..7af57555a --- /dev/null +++ b/src/main/java/mate/academy/model/Movie.java @@ -0,0 +1,47 @@ +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/util/HibernateUtil.java b/src/main/java/mate/academy/util/HibernateUtil.java new file mode 100644 index 000000000..cc15b3e8a --- /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 instance = initSessionFactory(); + + 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..8e5972e91 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,13 @@ + + + org.hibernate.dialect.MySQL5Dialect + jdbc:mysql://localhost:3306/cinema + root + Ltkmrf008866 + com.mysql.cj.jdbc.Driver + true + create-drop + + + + \ No newline at end of file