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

Commit task hibernate-configuration-hw #668

Open
wants to merge 3 commits 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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
with:
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}
- name: Set up JDK 17
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '17'
java-version: '11'
distribution: 'adopt'
cache: maven
- name: Build with Maven
Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.27.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -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 {
public static void main(String[] args) {
public static final Injector injector = Injector.getInstance("mate.academy");

public static void main(String[] args) {
MovieService movieService = (MovieService) injector.getInstance(MovieService.class);
Movie avatar = new Movie();
avatar.setTitle("Foundation");
avatar.setDescription("fantastic");
movieService.add(avatar);
Movie titanic = new Movie();
titanic.setTitle("Pulp fiction");
titanic.setDescription("comedy");
movieService.add(titanic);
System.out.println(movieService.get(1L));
}
}
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);
}
45 changes: 45 additions & 0 deletions src/main/java/mate/academy/dao/impl/MovieDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mate.academy.dao.impl;

import java.util.Optional;
import mate.academy.dao.MovieDao;
import mate.academy.exeption.DataProcessingException;
import mate.academy.lib.Dao;
import mate.academy.model.Movie;
import mate.academy.util.HibernateUtil;
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 e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Can't save movie to DB " + movie, e);
} finally {
if (session != null) {
session.close();
}
}
return movie;
}

@Override
public Optional<Movie> get(Long id) {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
return Optional.ofNullable(session.get(Movie.class, id));
} catch (Exception e) {
throw new DataProcessingException("Can't get from database "
+ "the movie with id: " + id, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mate.academy.exeption;

public class DataProcessingException extends RuntimeException {

public DataProcessingException(String message, Throwable cause) {
super(message, cause);
}
}
49 changes: 49 additions & 0 deletions src/main/java/mate/academy/model/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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 + '\'' + '}';
}
}
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);
}
23 changes: 23 additions & 0 deletions src/main/java/mate/academy/service/impl/MovieServiceImpl.java
Original file line number Diff line number Diff line change
@@ -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).get();
}
}
19 changes: 19 additions & 0 deletions src/main/java/mate/academy/util/HibernateUtil.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 10 additions & 0 deletions src/main/resources/init_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE DATABASE `ticket_app` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;

-- ticket_app.movies definition

CREATE TABLE `movies` (
`id` bigint NOT NULL AUTO_INCREMENT,
`description` varchar(255) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
2 changes: 0 additions & 2 deletions src/test/java/mate/academy/FunctionalityTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mate.academy;

import static org.mockito.Mockito.description;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mockStatic;

Expand Down Expand Up @@ -130,7 +129,6 @@ public void add_closeSessionAndRollbackTransaction_ok() {
mockSessionFactory(mockedSessionFactory);
Object testMovie = getTestMovie();
Mockito.when(mockedSession.save(testMovie)).thenThrow(new RuntimeException());
doThrow(new RuntimeException()).when(mockedSession).persist(testMovie);
Class dataProcessingExceptionClass = getClass("DataProcessingException");

try {
Expand Down
Loading