diff --git a/pom.xml b/pom.xml index f2dbc0637..ebb849c17 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,16 @@ + + org.hibernate + hibernate-core + 5.6.15.Final + + + mysql + mysql-connector-java + 8.0.33 + org.apache.commons commons-lang3 diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 0058fbf96..54ab1e27e 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -1,7 +1,24 @@ package mate.academy; +import mate.academy.dao.MovieDaoImpl; +import mate.academy.lib.Injector; +import mate.academy.model.Movie; +import mate.academy.service.MovieService; +import mate.academy.service.MovieServiceImpl; + public class Main { + private static final Injector injector = Injector.getInstance("mate.academy"); + public static void main(String[] args) { + MovieService movieService = new MovieServiceImpl(new MovieDaoImpl()); + + Movie movie = new Movie(); + movie.setTitle("Inception"); + movie.setDescription("A mind-bending thriller by Christopher Nolan."); + movieService.add(movie); + + Movie retrievedMovie = movieService.get(movie.getId()); + System.out.println("Retrieved movie: " + retrievedMovie.getTitle()); } } 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..e4d7e5f85 --- /dev/null +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -0,0 +1,52 @@ +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; + +public class MovieDaoImpl implements MovieDao { + + @Dao + @Override + public Movie add(Movie movie) { + Session session = null; + Transaction transaction = null; + try { + SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + session = sessionFactory.openSession(); + transaction = session.beginTransaction(); + session.persist(movie); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Can't insert Movie entity", e); + } finally { + if (session != null) { + session.close(); + } + } + return movie; + } + + @Override + public Optional get(Long id) { + Session session = null; + try { + session = HibernateUtil.getSessionFactory().openSession(); + return Optional.ofNullable(session.get(Movie.class, id)); + } catch (Exception e) { + throw new DataProcessingException("Can't get Movie entity by id " + id, e); + } finally { + if (session != null) { + session.close(); + } + } + } +} 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..188cf670e --- /dev/null +++ b/src/main/java/mate/academy/exception/DataProcessingException.java @@ -0,0 +1,12 @@ +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..b1bc9471c --- /dev/null +++ b/src/main/java/mate/academy/model/Movie.java @@ -0,0 +1,55 @@ +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/MovieServiceImpl.java b/src/main/java/mate/academy/service/MovieServiceImpl.java new file mode 100644 index 000000000..fd9404ebe --- /dev/null +++ b/src/main/java/mate/academy/service/MovieServiceImpl.java @@ -0,0 +1,28 @@ +package mate.academy.service; + +import mate.academy.dao.MovieDao; +import mate.academy.exception.DataProcessingException; +import mate.academy.lib.Inject; +import mate.academy.model.Movie; + +public class MovieServiceImpl implements MovieService { + private final MovieDao movieDao; + + @Inject + public MovieServiceImpl(MovieDao movieDao) { + this.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("Movie with id " + + id + + " not found")); + } +} 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..819c0c3c7 --- /dev/null +++ b/src/main/java/mate/academy/util/HibernateUtil.java @@ -0,0 +1,20 @@ +package mate.academy.util; + +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +public class HibernateUtil { + private static SessionFactory sessionFactory; + + private HibernateUtil() { + this.sessionFactory = new Configuration().configure().buildSessionFactory(); + } + + public HibernateUtil(SessionFactory sessionFactory) { + HibernateUtil.sessionFactory = sessionFactory; + } + + public static SessionFactory getSessionFactory() { + return sessionFactory; + } +} diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 000000000..98cd7bc3a --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,14 @@ + + + org.hibernate.dialect.MySQL5Dialect + jdbc:mysql://localhost:3306/ticket_app?serverTimeZone=UTC + com.mysql.cj.jdbc.Driver + root + Sasa12122005Fetisov + true + create-drop + + + + + \ No newline at end of file