Skip to content

hibernate error, can't find solution #944

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
<version>2.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.5.2.Final</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/mate/academy/hibernate/relations/Main.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
package mate.academy.hibernate.relations;

import java.util.List;
import mate.academy.hibernate.relations.dao.impl.ActorDaoImpl;
import mate.academy.hibernate.relations.dao.impl.CountryDaoImpl;
import mate.academy.hibernate.relations.dao.impl.MovieDaoImpl;
import mate.academy.hibernate.relations.model.Actor;
import mate.academy.hibernate.relations.model.Country;
import mate.academy.hibernate.relations.model.Movie;
import mate.academy.hibernate.relations.service.ActorService;
import mate.academy.hibernate.relations.service.CountryService;
import mate.academy.hibernate.relations.service.MovieService;
import mate.academy.hibernate.relations.service.impl.ActorServiceImpl;
import mate.academy.hibernate.relations.service.impl.CountryServiceImpl;
import mate.academy.hibernate.relations.service.impl.MovieServiceImpl;
import mate.academy.hibernate.relations.util.HibernateUtil;
import org.hibernate.SessionFactory;

public class Main {
public static void main(String[] args) {
// use this session factory when you will initialize service instances
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

Country usa = new Country("USA");
CountryService countryService = null; // TODO: initialize this instance
CountryService countryService = new CountryServiceImpl(
new CountryDaoImpl(sessionFactory));
countryService.add(usa);

Actor vinDiesel = new Actor("Vin Diesel");
vinDiesel.setCountry(usa);
ActorService actorService = null; // TODO: initialize this instance
ActorService actorService = new ActorServiceImpl(new ActorDaoImpl(sessionFactory));
actorService.add(vinDiesel);

Movie fastAndFurious = new Movie("Fast and Furious");
fastAndFurious.setActors(List.of(vinDiesel));
MovieService movieService = null; // TODO: initialize this instance
MovieService movieService = new MovieServiceImpl(new MovieDaoImpl(sessionFactory));
movieService.add(fastAndFurious);
System.out.println(movieService.get(fastAndFurious.getId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public abstract class AbstractDao {
protected AbstractDao(SessionFactory sessionFactory) {
this.factory = sessionFactory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.util.Optional;
import mate.academy.hibernate.relations.dao.ActorDao;
import mate.academy.hibernate.relations.exception.DataProcessingException;
import mate.academy.hibernate.relations.model.Actor;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class ActorDaoImpl extends AbstractDao implements ActorDao {
public ActorDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,11 +15,32 @@ public ActorDaoImpl(SessionFactory sessionFactory) {

@Override
public Actor add(Actor actor) {
return null;
Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(actor);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Cannot add the actor: " + actor.toString());
} finally {
if (session != null) {
session.close();
}
}
return actor;
}

@Override
public Optional<Actor> get(Long id) {
return null;
try (Session session = factory.openSession()) {
return Optional.ofNullable(session.get(Actor.class, id));
} catch (Exception e) {
throw new DataProcessingException("Cannot get actor(ID=" + id + ") from DB");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.util.Optional;
import mate.academy.hibernate.relations.dao.CountryDao;
import mate.academy.hibernate.relations.exception.DataProcessingException;
import mate.academy.hibernate.relations.model.Country;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class CountryDaoImpl extends AbstractDao implements CountryDao {
public CountryDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,11 +15,34 @@ public CountryDaoImpl(SessionFactory sessionFactory) {

@Override
public Country add(Country country) {
return null;
Session session = null;
Transaction transaction = null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not agree - the logical separation of the initialization blocks and the main logic

try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(country);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Cannot add the country: " + country.toString());
} finally {
if (session != null) {
session.close();
}
}

return country;
}

@Override
public Optional<Country> get(Long id) {
return null;
try (Session session = factory.openSession()) {
return Optional.ofNullable(session.get(Country.class, id));
} catch (Exception e) {
throw new DataProcessingException("Cannot get country(ID=" + id + ") from DB");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

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

public class MovieDaoImpl extends AbstractDao implements MovieDao {
public MovieDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,11 +15,34 @@ public MovieDaoImpl(SessionFactory sessionFactory) {

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not agree - the logical separation of the initialization blocks and the main logic

try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(movie);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("Cannot add the movie: " + movie.toString());
} finally {
if (session != null) {
session.close();
}
}

return movie;
}

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

public class DataProcessingException extends RuntimeException {
public DataProcessingException(String message) {
super(message);
}
}
13 changes: 13 additions & 0 deletions src/main/java/mate/academy/hibernate/relations/model/Actor.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
package mate.academy.hibernate.relations.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

@Entity
@Table(name = "actors")
public class Actor implements Cloneable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;

@ManyToOne
private Country country;

public Actor() {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/mate/academy/hibernate/relations/model/Country.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package mate.academy.hibernate.relations.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "countries")
public class Country implements Cloneable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/mate/academy/hibernate/relations/model/Movie.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package mate.academy.hibernate.relations.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "movies")
public class Movie implements Cloneable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@ManyToMany
@JoinTable(name = "movies_actors",
joinColumns = @JoinColumn(name = "movie_id"),
inverseJoinColumns = @JoinColumn(name = "actor_id"))
private List<Actor> actors;

public Movie() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package mate.academy.hibernate.relations.service.impl;

import jakarta.persistence.EntityNotFoundException;
import mate.academy.hibernate.relations.dao.ActorDao;
import mate.academy.hibernate.relations.model.Actor;
import mate.academy.hibernate.relations.service.ActorService;

public class ActorServiceImpl implements ActorService {
private final ActorDao actorDao;

public ActorServiceImpl(ActorDao actorDao) {
this.actorDao = actorDao;
}

@Override
public Actor add(Actor actor) {
return null;
return actorDao.add(actor);
}

@Override
public Actor get(Long id) {
return null;
return actorDao.get(id).orElseThrow(()
-> new EntityNotFoundException("Actor(ID=" + id + ") not found"));
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package mate.academy.hibernate.relations.service.impl;

import jakarta.persistence.EntityNotFoundException;
import mate.academy.hibernate.relations.dao.CountryDao;
import mate.academy.hibernate.relations.model.Country;
import mate.academy.hibernate.relations.service.CountryService;

public class CountryServiceImpl implements CountryService {
private final CountryDao countryDao;

public CountryServiceImpl(CountryDao countryDao) {
this.countryDao = countryDao;
}

@Override
public Country add(Country country) {
return null;
return countryDao.add(country);
}

@Override
public Country get(Long id) {
return null;
return countryDao.get(id).orElseThrow(()
-> new EntityNotFoundException("Country(ID=" + id + ") not found"));
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package mate.academy.hibernate.relations.service.impl;

import java.util.NoSuchElementException;
import mate.academy.hibernate.relations.dao.MovieDao;
import mate.academy.hibernate.relations.model.Movie;
import mate.academy.hibernate.relations.service.MovieService;

public class MovieServiceImpl implements MovieService {
private final MovieDao movieDao;

public MovieServiceImpl(MovieDao movieDao) {
this.movieDao = movieDao;
}

@Override
public Movie add(Movie movie) {
return null;
return movieDao.add(movie);
}

@Override
public Movie get(Long id) {
return null;
return movieDao.get(id).orElseThrow(()
-> new NoSuchElementException("Movie(ID=" + id + ") not found"));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package mate.academy.hibernate.relations.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 null;
return instance;
}
}
Loading
Loading