ЛР3 без доп задания

This commit is contained in:
ityurner02@mail.ru 2023-04-03 16:29:42 +04:00
parent de105ade49
commit 8baede6bca
6 changed files with 29 additions and 5 deletions

Binary file not shown.

View File

@ -42,9 +42,13 @@ public class Collection {
if (films == null){ if (films == null){
films = new ArrayList<>(); films = new ArrayList<>();
} }
this.films.add(film); if (!this.films.contains(film)) {
this.films.add(film);
film.addCollections(this);
}
} }
public void removeFilm(Film film){ public void removeFilm(Film film){
if(films != null){ if(films != null){
films.remove(film); films.remove(film);

View File

@ -1,5 +1,8 @@
package ru.ulstu.is.lab1.DataBase.model; package ru.ulstu.is.lab1.DataBase.model;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import javax.persistence.*; import javax.persistence.*;
import java.util.List; import java.util.List;
@ -12,12 +15,15 @@ public class Film {
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
private String name; private String name;
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany
@LazyCollection(LazyCollectionOption.FALSE)
@JoinTable(name="films_genres", @JoinTable(name="films_genres",
joinColumns = @JoinColumn(name="film_id"), joinColumns = @JoinColumn(name="film_id"),
inverseJoinColumns = @JoinColumn(name="genre_id") inverseJoinColumns = @JoinColumn(name="genre_id")
) )
private List<Genre> genres; private List<Genre> genres;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "films")
private List<Collection> collections;
public Film() { public Film() {
} }
@ -42,7 +48,8 @@ public class Film {
if (genres == null){ if (genres == null){
genres = new ArrayList<>(); genres = new ArrayList<>();
} }
this.genres.add(genre); if (!this.genres.contains(genre))
this.genres.add(genre);
} }
public void removeGenre(Genre genre){ public void removeGenre(Genre genre){
@ -55,6 +62,16 @@ public class Film {
return genres; return genres;
} }
public List<Collection> getCollections() {
return collections;
}
public void addCollections(Collection collection) {
if(this.collections==null)
collections=new ArrayList<>();
collections.add(collection);
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -11,8 +11,6 @@ public class Genre {
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
private String name; private String name;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "genres")
private List<Film> films;
public Genre() { public Genre() {
} }

View File

@ -9,6 +9,8 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Service @Service
@ -89,4 +91,6 @@ public class FilmService {
public void deleteAllFilms() { public void deleteAllFilms() {
em.createQuery("delete from Film").executeUpdate(); em.createQuery("delete from Film").executeUpdate();
} }
//сделать запрос отбора фильмов по жанру
} }

View File

@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.math.BigInteger;
import java.util.List; import java.util.List;
@SpringBootTest @SpringBootTest