generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 954
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added required hibernate dependencies
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
Showing
11 changed files
with
256 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,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)); | ||
} | ||
} |
This file contains 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,4 @@ | ||
package mate.academy.dao; | ||
|
||
public interface HibernateU { | ||
} |
This file contains 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,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; | ||
} | ||
} |
This file contains 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,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); | ||
} |
This file contains 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,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
12
src/main/java/mate/academy/exceptions/DataProcessingException.java
This file contains 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,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); | ||
|
||
} | ||
} |
This file contains 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,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 + '\'' | ||
+ '}'; | ||
} | ||
} |
This file contains 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,9 @@ | ||
package mate.academy.services; | ||
|
||
import mate.academy.model.Movie; | ||
|
||
public interface MovieService { | ||
Movie add(Movie movie); | ||
|
||
Movie get(Long id); | ||
} |
This file contains 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,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; | ||
} | ||
} |
This file contains 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,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> |