Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hometask Hibernate-configuration #1032

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
Expand All @@ -43,10 +44,27 @@
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.1.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>4.28.3</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
<version>2.7.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.25.Final</version>
</dependency>
</dependencies>
<build>
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/mate/academy/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package mate.academy;

import mate.academy.dao.MovieDaoImpl;
import mate.academy.model.Movie;
import mate.academy.service.MovieService;
import mate.academy.service.MovieServiceImpl;

public class Main {
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
MovieService movieService = new MovieServiceImpl(new MovieDaoImpl());

Movie movie = new Movie("Transformation", "Comedy");
movieService.add(movie);

Movie retrievedMovie = movieService.get(movie.getId());
System.out.println("Retrieved movie: " + retrievedMovie.getTitle());
}
}
10 changes: 10 additions & 0 deletions src/main/java/mate/academy/dao/MovieDao.java
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) throws Exception;

Optional<Movie> get(Long id);
}
41 changes: 41 additions & 0 deletions src/main/java/mate/academy/dao/MovieDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mate.academy.dao;

import java.util.Optional;
import mate.academy.exception.DataProcessingException;
import mate.academy.model.Movie;
import mate.academy.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;

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();
} catch (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new DataProcessingException("No movie has been added", e);
} finally {
if (session != null) {
session.close();
}
}
return movie;
}

@Override
public Optional<Movie> get(Long id) {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
return Optional.ofNullable(session.get(Movie.class, id));
} catch (RuntimeException e) {
throw new DataProcessingException("Movie not found by id: " + id, e);
}
}
}
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, Throwable cause) {
super(message, cause);
}
}
8 changes: 0 additions & 8 deletions src/main/java/mate/academy/lib/Dao.java

This file was deleted.

8 changes: 0 additions & 8 deletions src/main/java/mate/academy/lib/Inject.java

This file was deleted.

174 changes: 0 additions & 174 deletions src/main/java/mate/academy/lib/Injector.java

This file was deleted.

8 changes: 0 additions & 8 deletions src/main/java/mate/academy/lib/Service.java

This file was deleted.

56 changes: 56 additions & 0 deletions src/main/java/mate/academy/model/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package mate.academy.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String description;

public Movie() {
}

public Movie(String title, String description) {
this.title = title;
this.description = 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 + '\''
+ '}';
}
}
9 changes: 9 additions & 0 deletions src/main/java/mate/academy/service/MovieService.java
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) throws Exception;

Movie get(Long id);
}
Loading
Loading