generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
BelitskyiAlexandr
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
BelitskyiAlexandr:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e81b031
hibernate error, can't find solution
BelitskyiAlexandr 60e186d
failed attempt to move repetitive logic to an abstract class. Reorgan…
BelitskyiAlexandr c970c06
Elena-Bruyako's comments implemented
BelitskyiAlexandr ff86c3c
Elena-Bruyako's comments implemented
BelitskyiAlexandr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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) { | ||||
|
@@ -12,11 +15,34 @@ public MovieDaoImpl(SessionFactory sessionFactory) { | |||
|
||||
@Override | ||||
public Movie add(Movie movie) { | ||||
return null; | ||||
Session session = null; | ||||
Transaction transaction = null; | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||||
} | ||||
} | ||||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/hibernate/relations/exception/DataProcessingException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/java/mate/academy/hibernate/relations/model/Actor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/hibernate/relations/model/Country.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/mate/academy/hibernate/relations/model/Movie.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 11 additions & 2 deletions
13
src/main/java/mate/academy/hibernate/relations/service/impl/ActorServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
13 changes: 11 additions & 2 deletions
13
src/main/java/mate/academy/hibernate/relations/service/impl/CountryServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
13 changes: 11 additions & 2 deletions
13
src/main/java/mate/academy/hibernate/relations/service/impl/MovieServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
BelitskyiAlexandr marked this conversation as resolved.
Show resolved
Hide resolved
BelitskyiAlexandr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
13 changes: 12 additions & 1 deletion
13
src/main/java/mate/academy/hibernate/relations/util/HibernateUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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