diff --git a/pom.xml b/pom.xml index f0412faf9..89012fc7e 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,16 @@ + + org.hibernate + hibernate-core + 6.2.6.Final + + + mysql + mysql-connector-java + 8.0.33 + org.apache.commons commons-lang3 @@ -90,6 +100,14 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + true + + org.apache.maven.plugins maven-compiler-plugin diff --git a/src/main/java/mate/academy/Main.java b/src/main/java/mate/academy/Main.java index 0058fbf96..1ad714ea3 100644 --- a/src/main/java/mate/academy/Main.java +++ b/src/main/java/mate/academy/Main.java @@ -1,7 +1,22 @@ 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"); + private static final MovieService movieService = (MovieService) + injector.getInstance(MovieService.class); + public static void main(String[] args) { + // Add test + Movie movie = new Movie(); + movie.setTitle("Terminator 2"); + movie.setDescription("Judgment Day"); + System.out.println(movieService.add(movie)); + // Get test + System.out.println(movieService.get(1L)); } } 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..7708ee016 --- /dev/null +++ b/src/main/java/mate/academy/dao/MovieDaoImpl.java @@ -0,0 +1,46 @@ +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; + +@Dao +public class MovieDaoImpl implements MovieDao { + @Override + public Movie add(Movie movie) { + Session session = null; + Transaction transaction = null; + try { + // sessionFactory.openSession() + session = HibernateUtil.getSessionFactory().openSession(); + transaction = session.beginTransaction(); + session.save(movie); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new DataProcessingException("Transaction error has occurred!"); + } finally { + if (session != null) { + session.close(); + } + } + return movie; + } + + @Override + public Optional get(Long id) { + SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); + try (Session session = sessionFactory.openSession()) { + return Optional.ofNullable(session.get(Movie.class, id)); + } catch (Exception e) { + throw new DataProcessingException("Couldn't get movie from DB!"); + } + } +} 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..28a6a14d7 --- /dev/null +++ b/src/main/java/mate/academy/model/Movie.java @@ -0,0 +1,53 @@ +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 = "movie") +public class Movie { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String title; + private String description; + + public Long getId() { + return id; + } + + public Movie setId(Long id) { + this.id = id; + return this; + } + + public String getTitle() { + return title; + } + + public Movie setTitle(String title) { + this.title = title; + return this; + } + + public String getDescription() { + return description; + } + + public Movie setDescription(String description) { + this.description = description; + return this; + } + + @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..0b52d781a --- /dev/null +++ b/src/main/java/mate/academy/service/MovieServiceImpl.java @@ -0,0 +1,22 @@ +package mate.academy.service; + +import mate.academy.dao.MovieDao; +import mate.academy.lib.Inject; +import mate.academy.lib.Service; +import mate.academy.model.Movie; + +@Service +public class MovieServiceImpl implements MovieService { + @Inject + private static MovieDao movieDao; + + @Override + public Movie add(Movie movie) { + return movieDao.add(movie); + } + + @Override + public Movie get(Long id) { + return movieDao.get(id).get(); + } +} 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..dae5853d2 --- /dev/null +++ b/src/main/java/mate/academy/util/HibernateUtil.java @@ -0,0 +1,19 @@ +package mate.academy.util; + +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +public class HibernateUtil { + private static final SessionFactory instance = initSessionFactory(); + + private HibernateUtil() { + } + + 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..97a595c94 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,13 @@ + + + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306/ticket_app?serverTimeZone=UTC + root + Password + org.hibernate.dialect.MySQLDialect + true + create-drop + + + + diff --git a/src/test/resources/hibernate.cfg.xml b/src/test/resources/hibernate.cfg.xml deleted file mode 100644 index b8ff2c8c1..000000000 --- a/src/test/resources/hibernate.cfg.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - org.hsqldb.jdbcDriver - jdbc:hsqldb:mem - sa - - org.hibernate.dialect.HSQLDialect - true - -