Skip to content

Commit

Permalink
Added required hibernate dependencies
Browse files Browse the repository at this point in the history
created a hibernate.cfg.xml file
created HibernateUtil class
created Movie model class
created MovieDao interface and MovieDaoImpl class
created MovieService interface and MovieServiceImpl class
created my custom unchecked DataProcessingException
  • Loading branch information
ipantazi committed Jul 16, 2024
1 parent ab6cb67 commit 0411442
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://www.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>mate-academy</groupId>
Expand All @@ -28,7 +28,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -43,10 +43,20 @@
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.4.Final</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
</dependencies>
<build>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
package mate.academy;

import java.util.Map;
import mate.academy.lib.Injector;
import mate.academy.model.Movie;
import mate.academy.services.MovieService;

public class Main {
private static final Injector injector = Injector.getInstance("mate.academy");

public static void main(String[] args) {
// MovieDao movieDao = (MovieDao) injector.getInstance(MovieDao.class);
MovieService movieService = (MovieService) injector.getInstance(MovieService.class);

Map.of("Dune: Part Two", "Paul Atreides unites with Chani and the Fremen\\.\\.\\.",
"Joker: Folie à Deux", "Arthur Fleck meets the love of his life\\.\\.\\.",
"A Quiet Place: Day One", "A woman finds herself trapped\\.\\.\\.")
.forEach((title, description) -> {
Movie movie = new Movie();
movie.setTitle(title);
movie.setDescription(description);
System.out.println("An added movie = " + movieService.add(movie));
});

System.out.println("The movie by id # 2 = " + movieService.get(2L));
}
}
4 changes: 4 additions & 0 deletions src/main/java/mate/academy/dao/HibernateU.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package mate.academy.dao;

public interface HibernateU {
}
24 changes: 24 additions & 0 deletions src/main/java/mate/academy/dao/HibernateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mate.academy.dao;

import mate.academy.exceptions.DataProcessingException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil implements HibernateU {
private static final SessionFactory sessionFactory = initSessionFactory();

private HibernateUtil() {
}

private static SessionFactory initSessionFactory() {
try {
return new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
throw new DataProcessingException("Can't create session factory ", e);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
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);
}
48 changes: 48 additions & 0 deletions src/main/java/mate/academy/dao/MovieDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package mate.academy.dao;

import java.util.Optional;
import mate.academy.exceptions.DataProcessingException;
import mate.academy.lib.Dao;
import mate.academy.model.Movie;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

@Dao
public class MovieDaoImpl implements MovieDao {
private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

@Override
public Movie add(Movie movie) {
Session session = null;
Transaction transaction = null;

try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
movie = (Movie) session.save(movie);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Can't save movie to database " + movie, e);
} finally {
if ((session != null)) {
session.close();
}
}
return movie;
}

@Override
public Optional<Movie> get(Long id) {
Movie movie;
try (Session session = sessionFactory.openSession()) {
movie = session.get(Movie.class, id);
} catch (Exception e) {
throw new DataProcessingException("Can't get the movie by id: " + id, e);
}
return Optional.ofNullable(movie);
}
}
12 changes: 12 additions & 0 deletions src/main/java/mate/academy/exceptions/DataProcessingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mate.academy.exceptions;

public class DataProcessingException extends RuntimeException {
public DataProcessingException(String message) {
super(message);
}

public DataProcessingException(String message, Throwable cause) {
super(message, cause);

}
}
53 changes: 53 additions & 0 deletions src/main/java/mate/academy/model/Movie.java
Original file line number Diff line number Diff line change
@@ -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 Movie() {
}

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/services/MovieService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mate.academy.services;

import mate.academy.model.Movie;

public interface MovieService {
Movie add(Movie movie);

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

import java.util.Optional;
import mate.academy.dao.MovieDao;
import mate.academy.exceptions.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) {
if (movie == null || movie.getDescription() == null || movie.getTitle() == null) {
throw new DataProcessingException("The argument (a movie) or his fields is null :"
+ movie);
}
if (movie.getId() != 0) {
throw new DataProcessingException("Incorrect operation, the movie already has an Id: "
+ movie);
}
Movie addedMoved = movieDao.add(movie);
if (addedMoved.getId() == 0) {
throw new DataProcessingException("The error when adding movie, the Id is missing. "
+ addedMoved);
}
return addedMoved;
}

@Override
public Movie get(Long id) {
if (id == 0) {
throw new DataProcessingException("The argument (an Id) is null.");
}
Optional<Movie> optionalMovie = movieDao.get(id);
if (optionalMovie.isEmpty()) {
throw new DataProcessingException("The error when getting movie by Id: " + id
+ ", a movie is missing.");
}
return null;
}
}
18 changes: 18 additions & 0 deletions src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="connection.url">jdbs.mysql://localhost:3306/my_db</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">illya</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<mapping class="mate.academy.model.Movie"/>
</session-factory>
</hibernate-configuration>

0 comments on commit 0411442

Please sign in to comment.