1

商品ページがあります。リストされている各製品には独自の画像があります。ビューに表示するには、この行を使用します <img th:src="@{/data/{id}(id=${product.id})}"

製品を確認するためにエンドポイントに行くと、Hibernate は 1 つではなく 7 つのクエリを生成します。

Hibernate: 
    select
        product0_.id as id1_0_,
        product0_.content_id as content_2_0_,
        product0_.content_length as content_3_0_,
        product0_.mime_type as mime_typ4_0_,
        product0_.name as name5_0_ 
    from
        product product0_
Hibernate: 
    select
        product0_.id as id1_0_0_,
        product0_.content_id as content_2_0_0_,
        product0_.content_length as content_3_0_0_,
        product0_.mime_type as mime_typ4_0_0_,
        product0_.name as name5_0_0_ 
    from
        product product0_ 
    where
        product0_.id=?
Hibernate: 
    select
        product0_.id as id1_0_0_,
        product0_.content_id as content_2_0_0_,
        product0_.content_length as content_3_0_0_,
        product0_.mime_type as mime_typ4_0_0_,
        product0_.name as name5_0_0_ 
    from
        product product0_ 
    where
        product0_.id=?
Hibernate: 
    select
        product0_.id as id1_0_0_,
        product0_.content_id as content_2_0_0_,
        product0_.content_length as content_3_0_0_,
        product0_.mime_type as mime_typ4_0_0_,
        product0_.name as name5_0_0_ 
    from
        product product0_ 
    where
        product0_.id=?
Hibernate: 
    select
        product0_.id as id1_0_0_,
        product0_.content_id as content_2_0_0_,
        product0_.content_length as content_3_0_0_,
        product0_.mime_type as mime_typ4_0_0_,
        product0_.name as name5_0_0_ 
    from
        product product0_ 
    where
        product0_.id=?
Hibernate: 
    select
        product0_.id as id1_0_0_,
        product0_.content_id as content_2_0_0_,
        product0_.content_length as content_3_0_0_,
        product0_.mime_type as mime_typ4_0_0_,
        product0_.name as name5_0_0_ 
    from
        product product0_ 
    where
        product0_.id=?
Hibernate: 
    select
        product0_.id as id1_0_0_,
        product0_.content_id as content_2_0_0_,
        product0_.content_length as content_3_0_0_,
        product0_.mime_type as mime_typ4_0_0_,
        product0_.name as name5_0_0_ 
    from
        product product0_ 
    where
        product0_.id=?

おそらく、問題はページ上の画像の受け取り方に関係しています。ProductRepository は通常、1 つのクエリを実行して製品のリストを取得します。次に、ページに画像を表示するために、Spring は各製品に対して 3 つのクエリを実行します。"/data/1"また、画像付きのエンドポイントが 3 つのクエリを生成することにも気付きました。

Hibernate: 
    select
        product0_.id as id1_0_0_,
        product0_.content_id as content_2_0_0_,
        product0_.content_length as content_3_0_0_,
        product0_.mime_type as mime_typ4_0_0_,
        product0_.name as name5_0_0_ 
    from
        product product0_ 
    where
        product0_.id=?
Hibernate: 
    select
        product0_.id as id1_0_0_,
        product0_.content_id as content_2_0_0_,
        product0_.content_length as content_3_0_0_,
        product0_.mime_type as mime_typ4_0_0_,
        product0_.name as name5_0_0_ 
    from
        product product0_ 
    where
        product0_.id=?
Hibernate: 
    select
        product0_.id as id1_0_0_,
        product0_.content_id as content_2_0_0_,
        product0_.content_length as content_3_0_0_,
        product0_.mime_type as mime_typ4_0_0_,
        product0_.name as name5_0_0_ 
    from
        product product0_ 
    where
        product0_.id=?

このプロセスを最適化し、実行するクエリを減らすには? 問題を実証するプロジェクト

https://github.com/leonaugust/hibernate-problem

コード:

@SpringBootApplication
@Controller
@EnableJpaRepositories
public class HibernateProblemApplication {

  @Autowired
  private ProductRepository repository;

  public static void main(String[] args) {
    SpringApplication.run(HibernateProblemApplication.class, args);
  }

  @GetMapping("/")
  public String getAll(Model model) {
    model.addAttribute("products", repository.findAll());
    return "products";
  }

  @Bean
  public CommandLineRunner uploadImages(ProductRepository repository,
      ProductImageStore store) {
    return (args) -> {
      Product chicken = new Product("Chicken");
      store.setContent(chicken, this.getClass().getResourceAsStream("/img/chicken.jpg"));
      repository.save(chicken);

      Product goose = new Product("Goose");
      store.setContent(goose, this.getClass().getResourceAsStream("/img/goose.jpg"));
      repository.save(goose);
    };
  }

}


@StoreRestResource(path = "data")
@Repository
public interface ProductImageStore extends ContentStore<Product, String> {
}


@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

}


@Entity
public class Product {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  private String name;

  @ContentId
  private String contentId;

  @ContentLength
  private Long contentLength = 0L;

  @MimeType
  private String mimeType = "text/plain";

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

}
4

1 に答える 1