我有一个产品页面。每种产品都有自己的形象。要在视图上显示它,我使用以下行 <img th:src="@{/data/{id}(id=${product.id})}"
当我去端点查看产品时,hibernate会生成7个查询,而不是1个。
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通常执行一个查询来获取产品列表。然后,为了在页面上显示图像,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;
}
}
1条答案
按热度按时间new9mtju1#
@谢谢你让我注意到这一点。结果发现,我们的rest控制器不是很聪明,执行的查询比它需要的要多。在1.2.1版本中修复。