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 #991

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.3.Final</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
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");

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

Movie movie = new Movie();
movie.setTitle("Inception");
movie.setDescription("A mind-bending thriller");

movieService.add(movie);
System.out.println("Movie added: " + movieService.get(movie.getId()));
}
}
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);
}
42 changes: 42 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,42 @@
package mate.academy.dao.impl;

import java.util.Optional;
import mate.academy.dao.MovieDao;
import mate.academy.exception.DataProcessingException;
import mate.academy.model.Movie;
import mate.academy.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;

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

@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("Could not get movie by 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);
}
}
7 changes: 3 additions & 4 deletions src/main/java/mate/academy/lib/Injector.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private void setValueToField(Field field, Object instanceOfClass, Object classTo
throw new RuntimeException("Can't set value to field ", e);
}
}

/**
* Scans all classes accessible from the context class loader which
* belong to the given package and subpackages.
Expand All @@ -119,7 +120,6 @@ private void setValueToField(Field field, Object instanceOfClass, Object classTo
* @throws ClassNotFoundException if the class cannot be located
* @throws IOException if I/O errors occur
*/

private static List<Class<?>> getClasses(String packageName)
throws IOException, ClassNotFoundException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Expand All @@ -139,6 +139,7 @@ private static List<Class<?>> getClasses(String packageName)
}
return classes;
}

/**
* Recursive method used to find all classes in a given directory and subdirs.
*
Expand All @@ -147,7 +148,6 @@ private static List<Class<?>> getClasses(String packageName)
* @return The classes
* @throws ClassNotFoundException if the class cannot be located
*/

private static List<Class<?>> findClasses(File directory, String packageName)
throws ClassNotFoundException {
List<Class<?>> classes = new ArrayList<>();
Expand All @@ -161,8 +161,7 @@ private static List<Class<?>> findClasses(File directory, String packageName)
if (file.getName().contains(".")) {
throw new RuntimeException("File name shouldn't consist point.");
}
classes.addAll(findClasses(file, packageName + "."
+ file.getName()));
classes.addAll(findClasses(file, packageName + "." + file.getName()));
} else if (file.getName().endsWith(".class")) {
classes.add(Class.forName(packageName + '.'
+ file.getName().substring(0, file.getName().length() - 6)));
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/mate/academy/model/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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;

// Getters and setters
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).orElseThrow(() -> new RuntimeException("Movie not found"));
}
}
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 org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

private HibernateUtil() {
// Private constructor to prevent instantiation
}

private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
11 changes: 11 additions & 0 deletions src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:mem:test</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Loading