java - 避免使用多对多关系 jpa 添加相同的数据

标签 java spring-boot jpa

我有一个场景,一本书可以有一个类别并属于多个作者。一个类别可以有很多书籍。作者可能属于很多书籍。

所以,

  1. BookBookCategory多对一
  2. BookAuthorAuthorBook多对多(在 book_author 表中)
  3. BookCategoryBook一对多

我可以与作者和类别一起保存(使用下面的代码)新书。但是,问题是:

  1. 如果我没有检查作者是否已存在于 Author 表中,则同一作者将再次插入到 Author 表中
  2. 如果我检查 Author 表中是否已存在相同的作者,然后从列表中省略该作者,book_author 表如何知道该书是否与已存在的关系作者?

以下是我声明实体和服务的方式:

BookDto

public class BookDto {
    private String title;
    private String year;
    private Set<AuthorDto> author;
    private String category;
}

作者Dto

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;
}

图书类别

@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;
    }
}

任何帮助都会非常有帮助。谢谢!

最佳答案

If I did not check whether the author already exist in Author table, the same author will inserted again into Author table

正确。由此得出的结论是,如果您不想重复,则应该检查作者是否已经存在,无论您对“同一”作者的业务定义是什么。

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 exist 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);
} 

关于java - 避免使用多对多关系 jpa 添加相同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56850749/

相关文章:

java - Java中的默认构造函数

spring-boot - 在 Thymeleaf 片段中将数组作为参数传递

java - 如何修复 "Detached entity passed to persist"Spring Boot+Hibernate

hibernate - JPA Hibernate左连接获取生成多个查询

sql-server - 如何在spring jpa实体中提及sqlserver数据库名称?

java - 为什么使用 apache-email 库时附件文件名损坏?

java - 在 JTextPane 中显示选定的图像

java - SpringBoot : Can I @Autowire Bean in runnable JAR from JAR provided using java -cp?

java - 如何使用 JPA 使库对象数据库持久化?

java - 滚动背景,JAVA