Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The task was written according to the requirements #1034

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 35 additions & 38 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.0-SNAPSHOT</version>

<properties>
<jdk.version>17</jdk.version>
<jdk.version>11</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<skipTests>true</skipTests>
Expand Down Expand Up @@ -48,25 +48,32 @@
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.15.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Compiler Plugin for Java Version -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<warName>${project.artifactId}</warName>
<webResources>
<resource>
<filtering>true</filtering>
<includes>
<include>src/test/resources/hibernate.cfg.xml</include>
</includes>
</resource>
</webResources>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<!-- Optional: Checkstyle for Code Quality -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
Expand All @@ -86,32 +93,22 @@
<linkXRef>false</linkXRef>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>${project.artifactId}</warName>
<webResources>
<resource>
<filtering>true</filtering>
<includes>
<include>src/test/resources/hibernate.cfg.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<skip>${skipTests}</skip>
</configuration>
</execution>
</executions>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
13 changes: 12 additions & 1 deletion src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package mate.academy;

import java.util.Optional;
import mate.academy.dao.MovieDao;
import mate.academy.dao.MovieDaoImpl;
import mate.academy.model.Movie;

public class Main {
public static void main(String[] args) {

MovieDao movieDao = new MovieDaoImpl();
Movie movie = new Movie();
movie.setTitle("Terminator 5");
movie.setDescription("awesome movie");
movieDao.add(movie);
Optional<Movie> movieFromDB = movieDao.get(1L);
System.out.println(movieFromDB);
}
}
10 changes: 10 additions & 0 deletions src/main/java/mate/academy/dao/MovieDao.java
Original file line number Diff line number Diff line change
@@ -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<Movie> get(Long id);
}
52 changes: 52 additions & 0 deletions src/main/java/mate/academy/dao/MovieDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package mate.academy.dao;

import java.util.Optional;
import mate.academy.exception.DataProcessingException;
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 {
private final SessionFactory sessionFactory;

public MovieDaoImpl() {
this.sessionFactory = HibernateUtil.getSessionFactory();
}

public MovieDaoImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Override
public Movie add(Movie movie) {
Session session = null;
Transaction transaction = null;
try {
session = 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 add movie: " + movie, e);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public Optional<Movie> get(Long id) {
try (Session session = sessionFactory.openSession()) {
return Optional.ofNullable(session.get(Movie.class, id));
} catch (Exception e) {
throw new DataProcessingException("Could not get movie with id " + id, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mate.academy.exception;

public class DataProcessingException extends RuntimeException {
public DataProcessingException(String message, Throwable cause) {
super(message, cause);
}
}
48 changes: 48 additions & 0 deletions src/main/java/mate/academy/model/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package mate.academy.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
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 + '\''
+ '}';
}
}
9 changes: 9 additions & 0 deletions src/main/java/mate/academy/service/MovieService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mate.academy.service;

import mate.academy.model.Movie;

public interface MovieService {
Movie add(Movie movie);

Movie get(Long id);
}
24 changes: 24 additions & 0 deletions src/main/java/mate/academy/service/MovieServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mate.academy.service;

import mate.academy.dao.MovieDao;
import mate.academy.exception.DataProcessingException;
import mate.academy.model.Movie;

public class MovieServiceImpl implements MovieService {
private final MovieDao movieDao;

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", null));
}
}
20 changes: 20 additions & 0 deletions src/main/java/mate/academy/util/HibernateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mate.academy.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static SessionFactory instance = initSessionFactory();

private HibernateUtil() {
}

private static SessionFactory initSessionFactory() {
return new Configuration().configure().buildSessionFactory();
}

public static SessionFactory getSessionFactory() {
return instance;
}

}
13 changes: 13 additions & 0 deletions src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/ticket_app?serverTimeZone=UTC</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">KfqjifUfdhj22,</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>

<mapping class ="org.example.model.Movie"/>
</session-factory>
</hibernate-configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline
Loading