Spring JPARepository查询多对多的交集表

时间:2017-11-17 16:40:26

标签: spring hibernate spring-data-jpa

我有3个实体类,如下所示(示例来自https://hellokoding.com/jpa-many-to-many-extra-columns-relationship-mapping-example-with-spring-boot-maven-and-mysql/

预订课程

@Entity
public class Book{
    private int id;
    private String name;
    private Set<BookPublisher> bookPublishers;

    public Book() {
    }

    public Book(String name) {
        this.name = name;
        bookPublishers = new HashSet<>();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
    public Set<BookPublisher>   getBookPublishers() {
        return bookPublishers;
    }

    public void setBookPublishers(Set<BookPublisher> bookPublishers) {
        this.bookPublishers = bookPublishers;
    }
}

发布商类

@Entity
public class Publisher {
    private int id;
    private String name;
    private Set<BookPublisher> bookPublishers;

    public Publisher(){

    }

    public Publisher(String name){
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(mappedBy = "publisher")
    public Set<BookPublisher> getBookPublishers() {
        return bookPublishers;
    }

    public void setBookPublishers(Set<BookPublisher> bookPublishers) {
        this.bookPublishers = bookPublishers;
    }
}

交叉表

@Entity
@Table(name = "book_publisher")
public class BookPublisher implements Serializable{
    private Book book;
    private Publisher publisher;
    private Date publishedDate;

    @Id
    @ManyToOne
    @JoinColumn(name = "book_id")
    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    @Id
    @ManyToOne
    @JoinColumn(name = "publisher_id")
    public Publisher getPublisher() {
        return publisher;
    }

    public void setPublisher(Publisher publisher) {
        this.publisher = publisher;
    }

    @Column(name = "published_date")
    public Date getPublishedDate() {
        return publishedDate;
    }

    public void setPublishedDate(Date publishedDate) {
        this.publishedDate = publishedDate;
    }
}

我想查询2件事,

  1. 获取属于特定发布商的图书清单,例如获取与发布商100相关的所有图书
  2. 获取与特定发布商无关的图书清单,例如获取与发布商100无关的所有图书
  3. 我希望使用简单的JPARepository查询来实现这一点,如可能的话,如findByXYZIn(...)等。

    如果使用JPA存储库查询查询多对多关系,请告诉我,如果是,我是否可以直接执行或是否需要对实体类进行任何更改

3 个答案:

答案 0 :(得分:1)

我不确定你是否可以通过方法名称来制作它。但你绝对可以使用JPA查询。这样的事情:"SELECT b FROM Book b JOIN b.bookPublishers bp JOIN bp.publisher p WHERE p.id = ?1"。和第二种情况不相等

答案 1 :(得分:1)

BookRepository

获取出版商的书籍

findBooksByBookPublishersPublisherId(Long publisherId)

获取未由发布者出版的图书

findBooksByBookPublishersPublisherIdNot(Long publisherId)

恕我直言Publication是更合适的名称,然后是BookPublisher,因为Publisher本身可能是BookPublisher(已发布的出版书籍)

答案 2 :(得分:1)

您可以使用命名查询来满足您的要求:

@Query("select b from Book b where b.publisher.idd = ?1")
Book findByPublisherId(int id);

@Query("select b from Book b where b.publisher.idd <> ?1")
Book findByDifferentPublisherId(int id);

请查看Using @Query Spring docs了解更多详情。