diff --git a/pom.xml b/pom.xml
index f0412faf9..7cae6f71a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -48,6 +48,18 @@
hsqldb
2.3.4
+
+ org.hibernate
+ hibernate-core
+ 5.6.15.Final
+
+
+ mysql
+ mysql-connector-java
+ 8.0.28
+
+
+
diff --git a/src/main/java/mate/academy/HibernateUtil.java b/src/main/java/mate/academy/HibernateUtil.java
new file mode 100644
index 000000000..e95f1ad73
--- /dev/null
+++ b/src/main/java/mate/academy/HibernateUtil.java
@@ -0,0 +1,20 @@
+package mate.academy;
+
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+
+public class HibernateUtil {
+ private static final SessionFactory instance = initSessionFactory();
+
+ private HibernateUtil() {
+ }
+
+ public static SessionFactory initSessionFactory() {
+ return new Configuration().configure().buildSessionFactory();
+ }
+
+ public static SessionFactory getSessionFactory() {
+ return instance;
+ }
+
+}
diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java
index 0058fbf96..6e52370e7 100644
--- a/src/main/java/mate/academy/Main.java
+++ b/src/main/java/mate/academy/Main.java
@@ -1,7 +1,16 @@
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) {
-
+ MovieService movieService = (MovieService) Injector.getInstance("mate.academy")
+ .getInstance(MovieService.class);
+ Movie greatGatsby = new Movie("Great Gatsby", "Ambitious and idealistic");
+ movieService.add(greatGatsby);
+ Movie fromDB = movieService.get(greatGatsby.getId());
+ System.out.println(greatGatsby.equals(fromDB));
}
}
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..62767a373
--- /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.HibernateUtil;
+import mate.academy.exception.DataProcessingException;
+import mate.academy.lib.Dao;
+import mate.academy.model.Movie;
+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 (Exception ex) {
+ if (transaction != null) {
+ transaction.rollback();
+ }
+ throw new DataProcessingException("Can't add movie to the DB " + movie);
+ } finally {
+ if (session != null) {
+ session.close();
+ }
+ }
+
+ return movie;
+ }
+
+ @Override
+ public Optional get(Long id) {
+ Session session = null;
+ Movie movie;
+ try {
+ session = HibernateUtil.getSessionFactory().openSession();
+ movie = session.get(Movie.class, id);
+ } catch (Exception ex) {
+ throw new DataProcessingException("Can't get movie from the DB with id" + id);
+ } finally {
+ if (session != null) {
+ session.close();
+ }
+ }
+ return Optional.ofNullable(movie);
+ }
+}
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..11850856f
--- /dev/null
+++ b/src/main/java/mate/academy/exception/DataProcessingException.java
@@ -0,0 +1,7 @@
+package mate.academy.exception;
+
+public class DataProcessingException extends RuntimeException {
+ public DataProcessingException(String message) {
+ super(message);
+ }
+}
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..01cec4a6f
--- /dev/null
+++ b/src/main/java/mate/academy/model/Movie.java
@@ -0,0 +1,77 @@
+package mate.academy.model;
+
+import java.util.Objects;
+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 Movie() {
+ }
+
+ public Movie(String title, String description) {
+ 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 + "'\'}";
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ Movie movie = (Movie) o;
+ return Objects.equals(id, movie.id)
+ && Objects.equals(title, movie.title)
+ && Objects.equals(description, movie.description);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(id, title, 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..8e536b23f
--- /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("Can't find movie with id " + id));
+ }
+}
diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml
new file mode 100644
index 000000000..c73fc60c5
--- /dev/null
+++ b/src/main/resources/hibernate.cfg.xml
@@ -0,0 +1,13 @@
+
+
+ com.mysql.cj.jdbc.Driver
+ jdbc:mysql://localhost:3306/cinema
+ root
+ password
+ org.hibernate.dialect.MySQL5Dialect
+ true
+ create-drop
+
+
+
+
\ No newline at end of file