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

This commit is contained in:
ityurner02@mail.ru 2023-04-10 22:02:08 +04:00
parent 8baede6bca
commit 3e57102a6d
4 changed files with 46 additions and 22 deletions

Binary file not shown.

View File

@ -93,4 +93,11 @@ public class FilmService {
} }
//сделать запрос отбора фильмов по жанру //сделать запрос отбора фильмов по жанру
@Transactional
public List<Film> findFilmOnGenre(Genre genre){
List filmList = em.createQuery("SELECT DISTINCT f FROM Film f where :filmsGenres MEMBER OF f.genres")
.setParameter("filmsGenres", genre)
.getResultList();
return filmList;
}
} }

View File

@ -29,11 +29,11 @@ public class JpaCollectionTests {
filmService.deleteAllFilms(); filmService.deleteAllFilms();
final Film film1 = filmService.addFilm("Джокер"); final Film film1 = filmService.addFilm("Джокер");
final Film film2 = filmService.addFilm("Учитель на замену"); final Film film2 = filmService.addFilm("Учитель на замену");
final Collection collection1 = collectionService.addCollection("Грустное"); Collection collection1 = collectionService.addCollection("Грустное");
collection1.addFilm(film1); collection1 = collectionService.addFilm(collection1.getId(), film1.getId());
collection1.addFilm(film2); collection1 = collectionService.addFilm(collection1.getId(), film2.getId());
final Collection collection2 = collectionService.addCollection("Об антигерое"); Collection collection2 = collectionService.addCollection("Об антигерое");
collection2.addFilm(film1); collection2 = collectionService.addFilm(collection2.getId(), film1.getId());
log.info("testCreateCollection " + collection1.toString()); log.info("testCreateCollection " + collection1.toString());
log.info("testCreateCollection " + collection2.toString()); log.info("testCreateCollection " + collection2.toString());
Assertions.assertNotNull(collection1.getId()); Assertions.assertNotNull(collection1.getId());
@ -44,8 +44,8 @@ public class JpaCollectionTests {
collectionService.deleteAllCollections(); collectionService.deleteAllCollections();
filmService.deleteAllFilms(); filmService.deleteAllFilms();
final Film film = filmService.addFilm("Астрал"); final Film film = filmService.addFilm("Астрал");
final Collection collection = collectionService.addCollection("Пугающее"); Collection collection = collectionService.addCollection("Пугающее");
collection.addFilm(film); collection = collectionService.addFilm(collection.getId(), film.getId());
log.info("testCollectionRead[0]: " + collection.toString()); log.info("testCollectionRead[0]: " + collection.toString());
final Collection findCollection = collectionService.findCollection(collection.getId()); final Collection findCollection = collectionService.findCollection(collection.getId());
log.info("testCollectionRead[1]: " + findCollection.toString()); log.info("testCollectionRead[1]: " + findCollection.toString());
@ -58,10 +58,10 @@ public class JpaCollectionTests {
final Film film1 = filmService.addFilm("Начало"); final Film film1 = filmService.addFilm("Начало");
final Film film2 = filmService.addFilm("Пункт назначения"); final Film film2 = filmService.addFilm("Пункт назначения");
final Film film3 = filmService.addFilm("День сурка"); final Film film3 = filmService.addFilm("День сурка");
final Collection collection = collectionService.addCollection("Избранное"); Collection collection = collectionService.addCollection("Избранное");
collection.addFilm(film1); collection = collectionService.addFilm(collection.getId(), film1.getId());
collection.addFilm(film2); collection = collectionService.addFilm(collection.getId(), film2.getId());
collection.addFilm(film3); collection = collectionService.addFilm(collection.getId(), film3.getId());
log.info("testAddFilms[0]: " + collection.toString()); log.info("testAddFilms[0]: " + collection.toString());
Assertions.assertEquals(collection.getFilms().size(), 3); Assertions.assertEquals(collection.getFilms().size(), 3);
} }

View File

@ -28,11 +28,11 @@ public class JpaFilmTests {
genreService.deleteAllGenres(); genreService.deleteAllGenres();
final Genre genre1 = genreService.addGenre("Фантастика"); final Genre genre1 = genreService.addGenre("Фантастика");
final Genre genre2 = genreService.addGenre("Комедия"); final Genre genre2 = genreService.addGenre("Комедия");
final Film film1 = filmService.addFilm("Сапожник"); Film film1 = filmService.addFilm("Сапожник");
film1.addGenre(genre1); film1 = filmService.addGenre(film1.getId(), genre1.getId());
film1.addGenre(genre2); film1 = filmService.addGenre(film1.getId(), genre2.getId());
final Film film2 = filmService.addFilm("Маска"); Film film2 = filmService.addFilm("Маска");
film2.addGenre(genre1); film2 = filmService.addGenre(film2.getId(), genre1.getId());
log.info("testCreateFilm " + film1.toString()); log.info("testCreateFilm " + film1.toString());
log.info("testCreateFilm " + film2.toString()); log.info("testCreateFilm " + film2.toString());
Assertions.assertNotNull(film1.getId()); Assertions.assertNotNull(film1.getId());
@ -43,8 +43,8 @@ public class JpaFilmTests {
filmService.deleteAllFilms(); filmService.deleteAllFilms();
genreService.deleteAllGenres(); genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Ужасы"); final Genre genre = genreService.addGenre("Ужасы");
final Film film = filmService.addFilm("Оно"); Film film = filmService.addFilm("Оно");
film.addGenre(genre); film = filmService.addGenre(film.getId(), genre.getId());
log.info("testFilmRead[0]: " + film.toString()); log.info("testFilmRead[0]: " + film.toString());
final Film findFilm = filmService.findFilm(film.getId()); final Film findFilm = filmService.findFilm(film.getId());
log.info("testFilmRead[1]: " + findFilm.toString()); log.info("testFilmRead[1]: " + findFilm.toString());
@ -57,10 +57,10 @@ public class JpaFilmTests {
final Genre genre1 = genreService.addGenre("Драма"); final Genre genre1 = genreService.addGenre("Драма");
final Genre genre2 = genreService.addGenre("Триллер"); final Genre genre2 = genreService.addGenre("Триллер");
final Genre genre3 = genreService.addGenre("Боевик"); final Genre genre3 = genreService.addGenre("Боевик");
final Film film = filmService.addFilm("Брат"); Film film = filmService.addFilm("Брат");
film.addGenre(genre1); film = filmService.addGenre(film.getId(), genre1.getId());
film.addGenre(genre2); film = filmService.addGenre(film.getId(), genre2.getId());
film.addGenre(genre3); film = filmService.addGenre(film.getId(), genre3.getId());
log.info("testAddGenres[0]: " + film.toString()); log.info("testAddGenres[0]: " + film.toString());
Assertions.assertEquals(film.getGenres().size(), 3); Assertions.assertEquals(film.getGenres().size(), 3);
} }
@ -76,4 +76,21 @@ public class JpaFilmTests {
log.info("testFilmReadAllEmpty: " + films.toString()); log.info("testFilmReadAllEmpty: " + films.toString());
Assertions.assertEquals(films.size(), 0); Assertions.assertEquals(films.size(), 0);
} }
@Test
void testSelect() {
filmService.deleteAllFilms();
genreService.deleteAllGenres();
final Genre genre1 = genreService.addGenre("Драма");
final Genre genre2 = genreService.addGenre("Триллер");
final Genre genre3 = genreService.addGenre("Боевик");
Film film = filmService.addFilm("Брат");
Film film2 = filmService.addFilm("Маска");
film = filmService.addGenre(film.getId(), genre1.getId());
film = filmService.addGenre(film.getId(), genre2.getId());
film = filmService.addGenre(film.getId(), genre3.getId());
film2 = filmService.addGenre(film2.getId(), genre1.getId());
List<Film> list = filmService.findFilmOnGenre(genre1);
log.info("findFilmOnGenre: " + list.toString());
Assertions.assertEquals(list.size(), 2);
}
} }