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.
implemented hibernate two dao methods
- Loading branch information
Showing
8 changed files
with
167 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
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 |
---|---|---|
@@ -1,7 +1,20 @@ | ||
package mate.academy; | ||
|
||
import mate.academy.dao.MovieDao; | ||
import mate.academy.lib.Injector; | ||
import mate.academy.model.Movie; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
private static final Injector injector = Injector.getInstance("mate.academy"); | ||
private static final MovieDao movieDao = (MovieDao) injector.getInstance(MovieDao.class); | ||
|
||
public static void main(String[] args) { | ||
String title = "Night crawler"; | ||
String description = "terrifying photographer"; | ||
Movie movie = new Movie(); | ||
movie.setTitle(title); | ||
movie.setDescription(description); | ||
System.out.println(movieDao.add(movie)); | ||
System.out.println(movieDao.get(movie.getId())); | ||
} | ||
} |
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,49 @@ | ||
package mate.academy.dao; | ||
|
||
import java.util.Optional; | ||
import mate.academy.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 (RuntimeException e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Couldn't add movie " + movie, e); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<Movie> get(Long id) { | ||
Session session = null; | ||
try { | ||
session = HibernateUtil.getSessionFactory().openSession(); | ||
return Optional.ofNullable(session.get(Movie.class, id)); | ||
} catch (RuntimeException e) { | ||
throw new DataProcessingException("Couldn't get movie by id: " + id, e); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
} |
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,47 @@ | ||
package mate.academy.model; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "movies") | ||
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; | ||
} | ||
|
||
@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,16 @@ | ||
package mate.academy.util; | ||
|
||
import org.hibernate.SessionFactory; | ||
import org.hibernate.cfg.Configuration; | ||
|
||
public class HibernateUtil { | ||
private static final SessionFactory instance = initSessionFactory(); | ||
|
||
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="dialect">org.hibernate.dialect.MySQL5Dialect</property> | ||
<property name="connection.url">jdbc:mysql://localhost:3306/cinema</property> | ||
<property name="connection.username">root</property> | ||
<property name="connection.password">Ltkmrf008866</property> | ||
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> | ||
<property name="show_sql">true</property> | ||
<property name="hbm2ddl.auto">create-drop</property> | ||
|
||
<mapping class="mate.academy.model.Movie"/> | ||
</session-factory> | ||
</hibernate-configuration> |