Skip to content

Commit

Permalink
Added required hibernate dependencies
Browse files Browse the repository at this point in the history
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
ipantazi committed Jul 17, 2024
1 parent 0411442 commit f8fa2f7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 24 deletions.
8 changes: 5 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<maven.checkstyle.plugin.configLocation>
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
</properties>

<dependencies>
Expand All @@ -34,19 +36,19 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.8.0</version>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.4.0</version>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.4.Final</version>
<version>6.5.2.Final</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
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);
Injector injector = Injector.getInstance("mate.academy");
MovieService movieService = (MovieService) injector.getInstance(MovieService.class);

Map.of("Dune: Part Two", "Paul Atreides unites with Chani and the Fremen\\.\\.\\.",
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mate/academy/dao/MovieDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public Movie add(Movie movie) {
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
movie = (Movie) session.save(movie);
session.persist(movie);
transaction.commit();
movie.setId((Long) session.getIdentifier(movie));
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/mate/academy/model/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@
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 Long id;
private String title;
private String description;

public Movie() {
}

public long getId() {
public Long getId() {
return id;
}

public void setId(long id) {
public void setId(Long id) {
this.id = id;
}

Expand Down
16 changes: 6 additions & 10 deletions src/main/java/mate/academy/services/MovieServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mate.academy.services;

import java.util.Optional;
import mate.academy.dao.MovieDao;
import mate.academy.exceptions.DataProcessingException;
import mate.academy.lib.Inject;
Expand All @@ -18,12 +17,12 @@ public Movie add(Movie movie) {
throw new DataProcessingException("The argument (a movie) or his fields is null :"
+ movie);
}
if (movie.getId() != 0) {
if (movie.getId() != null) {
throw new DataProcessingException("Incorrect operation, the movie already has an Id: "
+ movie);
}
Movie addedMoved = movieDao.add(movie);
if (addedMoved.getId() == 0) {
if (addedMoved.getId() == null) {
throw new DataProcessingException("The error when adding movie, the Id is missing. "
+ addedMoved);
}
Expand All @@ -32,14 +31,11 @@ public Movie add(Movie movie) {

@Override
public Movie get(Long id) {
if (id == 0) {
if (id == null) {
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;
return movieDao.get(id).orElseThrow(()
-> new DataProcessingException("The error when getting movie by Id: " + id
+ ", a movie is missing."));
}
}
4 changes: 2 additions & 2 deletions src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<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.url">jdbs.mysql://localhost:3306/my_db?serverTimeZone=UTC</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>
<property name="hbm2ddl.auto">create-drop</property>

<mapping class="mate.academy.model.Movie"/>
</session-factory>
Expand Down

0 comments on commit f8fa2f7

Please sign in to comment.