1

Тема: spring data jpa and join fetch

Всім привіт, маю такі класи
Book:

@Data
@NoArgsConstructor
@Entity
@Table(name = "book")
@JsonIdentityInfo(
        property = "isbn",
        generator = ObjectIdGenerators.PropertyGenerator.class
)
public class Book {
    @Id
    @JsonView(Views.Id.class)
    private String isbn;

    @JsonView(Views.IdName.class)
    private String name;

    @Column(name = "date_of_publication")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    @JsonView(Views.FullBook.class)
    private LocalDate dateOfPublication;

    @JsonView(Views.FullBook.class)
    private Integer quantity;

    @JsonView(Views.FullBook.class)
    private String details;
    
    @OneToMany(mappedBy = "book")
    private List<ActiveBook> activeBooks;
    
    @ManyToOne
    @JoinColumn(name = "publication_id", referencedColumnName = "id")
    @JsonView(Views.FullBook.class)
    private Publication publication;

    @ManyToMany(fetch = FetchType.EAGER,cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "book_has_authors",
            joinColumns = @JoinColumn(name = "b_isbn",referencedColumnName = "isbn"),
            inverseJoinColumns = @JoinColumn(name = "a_id",referencedColumnName = "id"))
    @JsonView(Views.FullBook.class)
    private List<Author> authors;
}

Author:

@Data
@NoArgsConstructor
@Entity
@Table(name = "author")
public class Author {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonView(Views.Id.class)
    private int id;

    @JsonView(Views.IdName.class)
    private String name;

    @ManyToMany
    @JoinTable(name = "book_has_authors",
            joinColumns = @JoinColumn(name = "a_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "b_isbn", referencedColumnName = "isbn"))
    @JsonView(Views.FullAuthor.class)
    private List<Book> books;

}

Publication:

@Data
@NoArgsConstructor
@Entity
@Table(name="publication")
public class Publication {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonView(Views.Id.class)
    private int id;

    @JsonView(Views.IdName.class)
    private String name;

    @OneToMany(mappedBy = "publication")
    @JsonView(Views.FullPublication.class)
    private List<Book> books;

}

BookRepository:

@Repository
public interface BookRepository extends JpaRepository<Book, String> {
    @EntityGraph(attributePaths = {"authors"})
    Page<Book> findAll(Pageable pageable);
}

Але всерівно виконюється запит для кожної publication, як це можна пофіксити? Знаю, що якщо напишу 2 left join fetch, то спрінг буде сваритися. Але ж publication це одна сутність і по поєму розумінні, так як fetch type = eager, то воно б мало зразу робити запит і для publication. буду радий почути розумні думки і поради, і якщо я помиляюся, то підправте, дякую.
Якщо когось цікавить hibernate запити які викликаються, то ось:

Hibernate: 
    select
        b1_0.isbn,
        a1_0.b_isbn,
        a1_1.id,
        a1_1.name,
        b1_0.date_of_publication,
        b1_0.details,
        b1_0.name,
        b1_0.publication_id,
        b1_0.quantity 
    from
        book b1_0 
    left join
        (book_has_authors a1_0 
    join
        author a1_1 
            on a1_1.id=a1_0.a_id) 
                on b1_0.isbn=a1_0.b_isbn
Hibernate: 
    select
        p1_0.id,
        p1_0.name 
    from
        publication p1_0 
    where
        p1_0.id=?
Hibernate: 
    select
        p1_0.id,
        p1_0.name 
    from
        publication p1_0 
    where
        p1_0.id=?
Hibernate: 
    select
        p1_0.id,
        p1_0.name 
    from
        publication p1_0 
    where
        p1_0.id=?
Hibernate: 
    select
        p1_0.id,
        p1_0.name 
    from
        publication p1_0 
    where
        p1_0.id=?