private void deleteBookFromCategories(String bookId) { for (Category category : categoryDao.getAll()) { if (category.getBooks() != null && category.getBooks().contains(bookId)) { category.getBooks().remove(bookId); categoryDao.add(category); } } }
@Override public Book save(Book book) { bookDao.add(book); deleteBookFromCategories(book.getId()); if (book.getCategory() != null && categoryDao.get(book.getCategory()) != null) { Category category = categoryDao.get(book.getCategory()); HashSet<String> booksSet = category.getBooks(); if (booksSet == null) booksSet = new HashSet<String>(); booksSet.add(book.getId()); category.setBooks(booksSet); categoryDao.add(category); } bookDao.add(book); return get(book.getId()); }