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.
hw-hibernate-configuration-solution-andrey-kiyas
- Loading branch information
1 parent
744419c
commit e42fbea
Showing
11 changed files
with
205 additions
and
10 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,22 @@ | ||
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"); | ||
private static final MovieService movieService = (MovieService) | ||
injector.getInstance(MovieService.class); | ||
|
||
public static void main(String[] args) { | ||
// Add test | ||
Movie movie = new Movie(); | ||
movie.setTitle("Terminator 2"); | ||
movie.setDescription("Judgment Day"); | ||
System.out.println(movieService.add(movie)); | ||
|
||
// Get test | ||
System.out.println(movieService.get(1L)); | ||
} | ||
} |
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,46 @@ | ||
package mate.academy.dao; | ||
|
||
import java.util.Optional; | ||
import mate.academy.exception.DataProcessingException; | ||
import mate.academy.lib.Dao; | ||
import mate.academy.model.Movie; | ||
import mate.academy.util.HibernateUtil; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.Transaction; | ||
|
||
@Dao | ||
public class MovieDaoImpl implements MovieDao { | ||
@Override | ||
public Movie add(Movie movie) { | ||
Session session = null; | ||
Transaction transaction = null; | ||
try { | ||
// sessionFactory.openSession() | ||
session = HibernateUtil.getSessionFactory().openSession(); | ||
transaction = session.beginTransaction(); | ||
session.save(movie); | ||
transaction.commit(); | ||
} catch (Exception e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Transaction error has occurred!"); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
return movie; | ||
} | ||
|
||
@Override | ||
public Optional<Movie> get(Long id) { | ||
SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); | ||
try (Session session = sessionFactory.openSession()) { | ||
return Optional.ofNullable(session.get(Movie.class, id)); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Couldn't get movie from DB!"); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/exception/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.exception; | ||
|
||
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 |
---|---|---|
@@ -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 Long getId() { | ||
return id; | ||
} | ||
|
||
public Movie setId(Long id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public Movie setTitle(String title) { | ||
this.title = title; | ||
return this; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public Movie setDescription(String description) { | ||
this.description = description; | ||
return this; | ||
} | ||
|
||
@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.service; | ||
|
||
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,22 @@ | ||
package mate.academy.service; | ||
|
||
import mate.academy.dao.MovieDao; | ||
import mate.academy.lib.Inject; | ||
import mate.academy.lib.Service; | ||
import mate.academy.model.Movie; | ||
|
||
@Service | ||
public class MovieServiceImpl implements MovieService { | ||
@Inject | ||
private static MovieDao movieDao; | ||
|
||
@Override | ||
public Movie add(Movie movie) { | ||
return movieDao.add(movie); | ||
} | ||
|
||
@Override | ||
public Movie get(Long id) { | ||
return movieDao.get(id).get(); | ||
} | ||
} |
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 final SessionFactory instance = initSessionFactory(); | ||
|
||
private HibernateUtil() { | ||
} | ||
|
||
private 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,13 @@ | ||
<hibernate-configuration> | ||
<session-factory> | ||
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> | ||
<property name="connection.url">jdbc:mysql://localhost:3306/ticket_app?serverTimeZone=UTC</property> | ||
<property name="connection.username">root</property> | ||
<property name="connection.password">Password</property> | ||
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> | ||
<property name="show_sql">true</property> | ||
<property name="hbm2ddl.auto">create-drop</property> | ||
|
||
<mapping class="mate.academy.model.Movie"/> | ||
</session-factory> | ||
</hibernate-configuration> |
This file was deleted.
Oops, something went wrong.