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.
- Loading branch information
Showing
15 changed files
with
227 additions
and
11 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,18 @@ | ||
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) { | ||
MovieService movieService = (MovieService) Injector.getInstance(MovieService.class); | ||
Movie movie = new Movie(); | ||
movie.setTitle("Harry Potter"); | ||
movie.setDescription("Nice Movie"); | ||
|
||
Movie saveMovie = movieService.add(movie); | ||
Movie getMovie = movieService.get(1L); | ||
System.out.println(getMovie); | ||
} | ||
} |
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.dao; | ||
|
||
import java.util.Optional; | ||
import mate.academy.lib.Dao; | ||
import mate.academy.model.Movie; | ||
|
||
@Dao | ||
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,52 @@ | ||
package mate.academy.dao.impl; | ||
|
||
import java.util.Optional; | ||
import mate.academy.dao.MovieDao; | ||
import mate.academy.exeption.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(); | ||
return movie; | ||
} catch (Exception e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Can't add movie: " + movie); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<Movie> get(Long id) { | ||
Session session = null; | ||
try { | ||
session = HibernateUtil.getSessionFactory().openSession(); | ||
session.get(Movie.class, id); | ||
return Optional.ofNullable(session.get(Movie.class, id)); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't get movie by id : " + id); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/exeption/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,7 @@ | ||
package mate.academy.exeption; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message) { | ||
super(message); | ||
} | ||
} |
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,8 +1,11 @@ | ||
package mate.academy.lib; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Dao { | ||
} |
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,8 +1,11 @@ | ||
package mate.academy.lib; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Inject { | ||
} |
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,8 +1,11 @@ | ||
package mate.academy.lib; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Service { | ||
} |
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,42 @@ | ||
package mate.academy.model; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import mate.academy.lib.Inject; | ||
|
||
@Entity | ||
@Inject | ||
public class Movie { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private long id; | ||
private String title; | ||
private String 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; | ||
} | ||
|
||
} |
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,11 @@ | ||
package mate.academy.service; | ||
|
||
import mate.academy.lib.Service; | ||
import mate.academy.model.Movie; | ||
|
||
@Service | ||
public interface MovieService { | ||
Movie add(Movie movie); | ||
|
||
Movie get(Long id); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/mate/academy/service/imlp/MovieServiceImpl.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,22 @@ | ||
package mate.academy.service.imlp; | ||
|
||
import mate.academy.dao.MovieDao; | ||
import mate.academy.lib.Injector; | ||
import mate.academy.lib.Service; | ||
import mate.academy.model.Movie; | ||
import mate.academy.service.MovieService; | ||
|
||
@Service | ||
public class MovieServiceImpl implements MovieService { | ||
private MovieDao movieDao = (MovieDao) Injector.getInstance(MovieDao.class); | ||
|
||
@Override | ||
public Movie add(Movie movie) { | ||
return movieDao.add(movie); | ||
} | ||
|
||
@Override | ||
public Movie get(Long id) { | ||
return movieDao.get(id).orElse(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,19 @@ | ||
package mate.academy.util; | ||
|
||
import org.hibernate.SessionFactory; | ||
import org.hibernate.cfg.Configuration; | ||
|
||
public class HibernateUtil { | ||
private static SessionFactory instance = getSessionFactory(); | ||
|
||
private HibernateUtil() { | ||
} | ||
|
||
public static SessionFactory initSessionFactory() { | ||
return new Configuration().configure().buildSessionFactory(); | ||
} | ||
|
||
public static SessionFactory getSessionFactory() { | ||
return instance; | ||
} | ||
} |
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">jdbc:mysql://localhost:3306</property> | ||
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> | ||
<property name="connection.username">root</property> | ||
<property name="connection.password">qwerty123</property> | ||
<property name="show_sql">true</property> | ||
<property name="hbm2ddl.auto">update</property> | ||
|
||
<mapping class="mate.academy.model.Movie"/> | ||
</session-factory> | ||
</hibernate-configuration> |
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,10 +1,13 @@ | ||
<hibernate-configuration> | ||
<session-factory> | ||
<property name="connection.url">jdbc:mysql://localhost:3306</property> | ||
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property> | ||
<property name="connection.url">jdbc:hsqldb:mem</property> | ||
<property name="connection.username">sa</property> | ||
<property name="connection.password"/> | ||
<property name="connection.username">root</property> | ||
<property name="connection.password">qwerty123</property> | ||
<property name="dialect">org.hibernate.dialect.HSQLDialect</property> | ||
<property name="show_sql">true</property> | ||
<property name="hbm2ddl.auto">create-drop</property> | ||
|
||
<mapping class="mate.academy.model.Movie"/> | ||
</session-factory> | ||
</hibernate-configuration> |