我有一个场景,一本书可以有一个类别,并且属于多个作者。一个类别可能有很多书籍。作者可能属于很多书籍。
所以
Book
至BookCategory
是many-to-one
Book
至Author
和Author
至Book
是many-to-many
(在book_author
表中)BookCategory
至Book
是one-to-many
我可以和作者以及类别一起保存(使用以下代码)新书。但是,问题是:
Author
表中是否已经存在该作者,那么同一位作者将再次插入到Author
表中Author
表中是否已经存在同一作者,然后从列表中省略该作者,那么book_author
表将如何知道这本书是否与已经存在的作者有关系?这是我声明实体和服务的方式:
BookDto
public class BookDto {
private String title;
private String year;
private Set<AuthorDto> author;
private String category;
}
AuthorDto
public class AuthorDto {
private String name;
private String address;
}
图书
@Entity(name = "book")
@Table(name = "book", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title", nullable = false)
private String title;
@Column(name = "year", nullable = false)
private String year;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "book_author",
joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id"))
@JsonManagedReference
private Set<Author> author;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", referencedColumnName = "id", nullable = false)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private BookCategory category;
}
作者
@Entity(name = "author")
@Table(name = "author", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "address", nullable = false)
private String address;
@ManyToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonBackReference
private Set<Book> book;
}
BookCategory
@Entity(name = "book_category")
@Table(name = "book_category")
public class BookCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "category", nullable = false)
private String category;
}
BookServiceImpl
public class BookServiceImpl implements BookService {
public BookDto save(BookDto bookDto) throws Exception {
try {
Book book = new Book();
BeanUtils.copyProperties(bookDto, book, "author", "category");
BookCategory bookCategory = bookCategoryDao.findByCategory(bookDto.getCategory());
Set<Author> dataAuthor = new HashSet<Author>();
Set<AuthorDto> dataAuthorDto = new HashSet<AuthorDto>();
bookDto.getAuthor().iterator().forEachRemaining(dataAuthorDto::add);
for (AuthorDto authorDto : dataAuthorDto) {
Author author = new Author();
/**
* Problem is:
* 1. If I did not check whether the author already exist in
* Author table, the same author will inserted again into Author table
* 2. If I check whether same author already exist in Author table, then omit
* the author from the list, how book_author table would know if
* the book have relationship with already existed author?
* */
BeanUtils.copyProperties(authorDto, author);
dataAuthor.add(author);
}
book.setAuthor(dataAuthor);
book.setCategory(bookCategory);
bookDao.save(book);
} catch (Exception e) {
throw new Exception(e);
}
return null;
}
}
任何帮助都会非常有帮助。谢谢!
答案 0 :(得分:0)
如果我没有检查Author表中是否已经存在该作者,则该作者将再次插入Author表中。
正确。得出的结论是,如果您不希望重复,则应该检查作者是否已经存在,无论您对“同一”作者的业务定义是什么。
如果我检查“作者”表中是否已存在同一作者,然后从列表中省略该作者,那么book_author表如何知道该书是否与已存在的作者有关系?
那么,那就不要忽略它。查找它。假设您对同一作者的定义是“具有相同名称的作者”,只需将以下方法添加到DataAuthorRepository
(或您所说的任何名称)中:
Optional<Author> findByNameIgnoreCase(String name)
然后,在BookServiceImpl
中,只需执行以下操作:
for (AuthorDto authorDto : dataAuthorDto) {
Author author = dataAuthor.findByNameIgnoreCase(authorDto.getName())
.orElseGet(() -> {
Author newAuthor = new Author();
BeanUtils.copyProperties(authorDto, newAuthor);
return newAuthor;
});
dataAuthor.add(author);
}