diff --git a/data.mv.db b/data.mv.db index 8b5e1f9..ccb25d8 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Collection.java b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Collection.java index 576dabf..e68a352 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Collection.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Collection.java @@ -42,9 +42,13 @@ public class Collection { if (films == null){ films = new ArrayList<>(); } - this.films.add(film); + if (!this.films.contains(film)) { + this.films.add(film); + film.addCollections(this); + } } + public void removeFilm(Film film){ if(films != null){ films.remove(film); diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Film.java b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Film.java index 20c2a1a..bee65cf 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Film.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Film.java @@ -1,5 +1,8 @@ package ru.ulstu.is.lab1.DataBase.model; +import org.hibernate.annotations.LazyCollection; +import org.hibernate.annotations.LazyCollectionOption; + import javax.persistence.*; import java.util.List; @@ -12,12 +15,15 @@ public class Film { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; - @ManyToMany(fetch = FetchType.EAGER) + @ManyToMany + @LazyCollection(LazyCollectionOption.FALSE) @JoinTable(name="films_genres", joinColumns = @JoinColumn(name="film_id"), inverseJoinColumns = @JoinColumn(name="genre_id") ) private List genres; + @ManyToMany(fetch = FetchType.EAGER, mappedBy = "films") + private List collections; public Film() { } @@ -42,7 +48,8 @@ public class Film { if (genres == null){ genres = new ArrayList<>(); } - this.genres.add(genre); + if (!this.genres.contains(genre)) + this.genres.add(genre); } public void removeGenre(Genre genre){ @@ -55,6 +62,16 @@ public class Film { return genres; } + public List getCollections() { + return collections; + } + + public void addCollections(Collection collection) { + if(this.collections==null) + collections=new ArrayList<>(); + collections.add(collection); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java index 71af0ed..382a835 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java @@ -11,8 +11,6 @@ public class Genre { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; - @ManyToMany(fetch = FetchType.EAGER, mappedBy = "genres") - private List films; public Genre() { } diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/service/FilmService.java b/src/main/java/ru/ulstu/is/lab1/DataBase/service/FilmService.java index a91c3cd..f0bdd14 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/service/FilmService.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/service/FilmService.java @@ -9,6 +9,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; +import java.math.BigInteger; +import java.util.ArrayList; import java.util.List; @Service @@ -89,4 +91,6 @@ public class FilmService { public void deleteAllFilms() { em.createQuery("delete from Film").executeUpdate(); } + + //сделать запрос отбора фильмов по жанру } diff --git a/src/test/java/ru/ulstu/is/lab1/JpaFilmTests.java b/src/test/java/ru/ulstu/is/lab1/JpaFilmTests.java index 028d7a0..eaf84e1 100644 --- a/src/test/java/ru/ulstu/is/lab1/JpaFilmTests.java +++ b/src/test/java/ru/ulstu/is/lab1/JpaFilmTests.java @@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.math.BigInteger; import java.util.List; @SpringBootTest