diff --git a/pom.xml b/pom.xml
index f2dbc0637..5e98ab835 100644
--- a/pom.xml
+++ b/pom.xml
@@ -48,7 +48,18 @@
hsqldb
2.3.4
+
+ org.hibernate
+ hibernate-core
+ 5.6.3.Final
+
+
+ javax.persistence
+ javax.persistence-api
+ 2.2
+
+
diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java
index 0058fbf96..789dcc672 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.lib.Injector;
+import mate.academy.model.Movie;
+import mate.academy.service.MovieService;
+
public class Main {
+ private static final Injector injector = Injector.getInstance("mate.academy");
+
public static void main(String[] args) {
+ MovieService movieService = (MovieService) injector.getInstance(MovieService.class);
+
+ Movie movie = new Movie();
+ movie.setTitle("Inception");
+ movie.setDescription("A mind-bending thriller");
+ movieService.add(movie);
+ System.out.println("Movie added: " + movieService.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/impl/MovieDaoImpl.java b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java
new file mode 100644
index 000000000..a35c152cd
--- /dev/null
+++ b/src/main/java/mate/academy/dao/impl/MovieDaoImpl.java
@@ -0,0 +1,42 @@
+package mate.academy.dao.impl;
+
+import java.util.Optional;
+import mate.academy.dao.MovieDao;
+import mate.academy.exception.DataProcessingException;
+import mate.academy.model.Movie;
+import mate.academy.util.HibernateUtil;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+
+public class MovieDaoImpl implements MovieDao {
+ @Override
+ public Movie add(Movie movie) {
+ Transaction transaction = null;
+ Session session = null;
+ try {
+ session = HibernateUtil.getSessionFactory().openSession();
+ transaction = session.beginTransaction();
+ session.save(movie);
+ transaction.commit();
+ return movie;
+ } catch (Exception e) {
+ if (transaction != null) {
+ transaction.rollback();
+ }
+ throw new DataProcessingException("Could not insert movie " + movie, e);
+ } finally {
+ if (session != null) {
+ session.close();
+ }
+ }
+ }
+
+ @Override
+ public Optional get(Long id) {
+ try (Session session = HibernateUtil.getSessionFactory().openSession()) {
+ return Optional.ofNullable(session.get(Movie.class, id));
+ } catch (Exception e) {
+ throw new DataProcessingException("Could not get movie 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..63b937dc4
--- /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, Throwable cause) {
+ super(message, cause);
+ }
+}
diff --git a/src/main/java/mate/academy/lib/Injector.java b/src/main/java/mate/academy/lib/Injector.java
index c5008b336..cb7a6b19e 100644
--- a/src/main/java/mate/academy/lib/Injector.java
+++ b/src/main/java/mate/academy/lib/Injector.java
@@ -110,6 +110,7 @@ private void setValueToField(Field field, Object instanceOfClass, Object classTo
throw new RuntimeException("Can't set value to field ", e);
}
}
+
/**
* Scans all classes accessible from the context class loader which
* belong to the given package and subpackages.
@@ -119,7 +120,6 @@ private void setValueToField(Field field, Object instanceOfClass, Object classTo
* @throws ClassNotFoundException if the class cannot be located
* @throws IOException if I/O errors occur
*/
-
private static List> getClasses(String packageName)
throws IOException, ClassNotFoundException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
@@ -139,6 +139,7 @@ private static List> getClasses(String packageName)
}
return classes;
}
+
/**
* Recursive method used to find all classes in a given directory and subdirs.
*
@@ -147,7 +148,6 @@ private static List> getClasses(String packageName)
* @return The classes
* @throws ClassNotFoundException if the class cannot be located
*/
-
private static List> findClasses(File directory, String packageName)
throws ClassNotFoundException {
List> classes = new ArrayList<>();
@@ -161,8 +161,7 @@ private static List> findClasses(File directory, String packageName)
if (file.getName().contains(".")) {
throw new RuntimeException("File name shouldn't consist point.");
}
- classes.addAll(findClasses(file, packageName + "."
- + file.getName()));
+ classes.addAll(findClasses(file, packageName + "." + file.getName()));
} else if (file.getName().endsWith(".class")) {
classes.add(Class.forName(packageName + '.'
+ file.getName().substring(0, file.getName().length() - 6)));
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..e26429301
--- /dev/null
+++ b/src/main/java/mate/academy/model/Movie.java
@@ -0,0 +1,52 @@
+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;
+
+ // Getters and setters
+ 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/impl/MovieServiceImpl.java b/src/main/java/mate/academy/service/impl/MovieServiceImpl.java
new file mode 100644
index 000000000..213593b54
--- /dev/null
+++ b/src/main/java/mate/academy/service/impl/MovieServiceImpl.java
@@ -0,0 +1,23 @@
+package mate.academy.service.impl;
+
+import mate.academy.dao.MovieDao;
+import mate.academy.lib.Inject;
+import mate.academy.lib.Service;
+import mate.academy.model.Movie;
+import mate.academy.service.MovieService;
+
+@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 RuntimeException("Movie 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..d939efad7
--- /dev/null
+++ b/src/main/java/mate/academy/util/HibernateUtil.java
@@ -0,0 +1,24 @@
+package mate.academy.util;
+
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+
+public class HibernateUtil {
+ private static final SessionFactory sessionFactory = buildSessionFactory();
+
+ private HibernateUtil() {
+ // Private constructor to prevent instantiation
+ }
+
+ private static SessionFactory buildSessionFactory() {
+ try {
+ return new Configuration().configure().buildSessionFactory();
+ } catch (Throwable ex) {
+ throw new ExceptionInInitializerError(ex);
+ }
+ }
+
+ 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..1e5717d56
--- /dev/null
+++ b/src/main/resources/hibernate.cfg.xml
@@ -0,0 +1,11 @@
+
+
+ org.hsqldb.jdbcDriver
+ jdbc:hsqldb:mem:test
+ sa
+
+ org.hibernate.dialect.HSQLDialect
+ true
+ update
+
+