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

Hibernate configuration #1009

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.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.6.0.Final</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
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) {
private static final Injector injector = Injector.getInstance("mate.academy");

public static void main(String[] args) {
MovieService movieService = (MovieService) injector.getInstance(MovieService.class);
Movie firstMovie = new Movie("Modern Java", "Best practises of coding");
Movie secondMovie = new Movie("Harry Potter and the Philosopher's Stone",
"It follows Harry Potter, a young wizard who discovers his magical heritage "
+ "on his eleventh birthday when he receives a letter of acceptance to "
+ "Hogwarts School of Witchcraft and Wizardry.");
Movie thirdMovie = new Movie("The Little Prince",
"The story follows a young prince who visits various planets, "
+ "including Earth, and addresses themes of loneliness, friendship, love, "
+ "and loss.");
movieService.add(firstMovie);
movieService.add(secondMovie);
movieService.add(thirdMovie);
movieService.get(2L);
}
}
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/MovieDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException(String.format(
"Cannot save movie:%s 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 (RuntimeException e) {
throw new DataProcessingException(String.format(
"Cannot find movie with id:%d in DB", id), e);
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/mate/academy/exception/DataProcessingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mate.academy.exception;

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

public DataProcessingException(String message, Throwable cause) {
super(message, cause);
}
}
63 changes: 63 additions & 0 deletions src/main/java/mate/academy/model/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 = "movies")
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String description;

public Movie() {
}

public Movie(String title, String description) {
this.title = title;
this.description = description;
}

public Movie(Long id, String title, String description) {
this.id = id;
this.title = title;
this.description = 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.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) {
return movieDao.add(movie);
}

@Override
public Movie get(Long id) {
return movieDao.get(id).orElseThrow(() -> new DataProcessingException("Cannot find movie "
+ "with id:" + id));
}
}
24 changes: 24 additions & 0 deletions src/main/java/mate/academy/util/HibernateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mate.academy.util;

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

public class HibernateUtil {
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;
}
}
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.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>
<property name="connection.driver.class"> com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">F6Rf@N34,djA3i67</property>
<property name="show.sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>

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