HQL to a custom class with entity, selecting only id then querying n+1

Viewed 139

I'm using HQL Query and i'm trying to pass an entity object to the custom class(view), it's just selecting my entity Id and then my entire entity, making the query N+1, it's not so good.

Stack: Spring Boot v2.2.5.RELEASE

It's a bug of Hibernate ?
Does anyone know any way to resolve this ?
I'm trying to not map each column of entity in my custom class.
Follow the examples below:

@Data
@Entity
@EqualsAndHashCode(callSuper = true)
public class Book extends MyModel {

    @Column(name = "title")
    private String title;

    @Column(name = "some_column_fetch")
    private String someColumnFetch;

    @ManyToOne(cascade = CascadeType.ALL)
    private Author author;

}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
public class Magazine extends MyModel {

    @Column(name = "title")
    private String title;

    @Column(name = "some_column_fetch")
    private String someColumnFetch;

    @ManyToOne(cascade = CascadeType.ALL)
    private Author author;

}

@Data
public class BookView {

    private final Book book;

    private final Magazine magazine;

}
public interface BookRepository extends JpaRepository<Book, UUID> {

    @Query("SELECT new com.example.demo.views.BookView(book, magazine) " +
            " FROM Book book" +
            " LEFT JOIN Magazine magazine ON book.someColumnFetch = magazine.someColumnFetch")
    List<BookView> findViews();

}
0 Answers
Related