изменён метод удаления

This commit is contained in:
Николай 2023-03-16 09:24:00 +04:00
parent cfeaf090f8
commit 7f64c30cb2
3 changed files with 11 additions and 4 deletions

View File

@ -18,9 +18,6 @@ public class CreatorService {
@PersistenceContext
private EntityManager em;
@Autowired
MangaService mangaService;
@Transactional
public Creator findCreator(Long id) {
final Creator creator = em.find(Creator.class, id);

View File

@ -6,6 +6,7 @@ import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@ -15,6 +16,8 @@ import java.util.List;
public class ReaderService {
@PersistenceContext
private EntityManager em;
@Autowired
private MangaService mangaService;
@Transactional
public Reader findReader(Long id) {
@ -50,7 +53,13 @@ public class ReaderService {
@Transactional
public void removeManga(Manga manga, Long readerId) {
em.createNativeQuery("delete from Mangas_Readers where MANGA_FK = " + manga.getId() + " AND READER_FK = "+ readerId).executeUpdate();
//em.createNativeQuery("delete from Mangas_Readers where MANGA_FK = " + manga.getId() + " AND READER_FK = "+ readerId).executeUpdate();
final Reader currentReader = findReader(readerId);
final Manga currentManga = em.find(Manga.class, manga.getId());
currentReader.getMangas().remove(currentManga);
currentManga.getReaders().remove(currentReader);
em.merge(currentReader);
em.merge(currentManga);
}
@Transactional

View File

@ -240,6 +240,7 @@ public class ReMangaTest {
Manga m11 = mangaService.findManga(m1.getId());
Assertions.assertEquals(1, r12.getMangas().size());
log.info(mangaService.findAllMangas().toString());
log.info(r12.getMangas().toString());
}
@Test