简体   繁体   English

Spring JPA 是否对当前上下文/请求中已经检索到的缓存实体做任何聪明的事情?

[英]Does Spring JPA do anything smart with caching entities already retrieved in the current context/request?

If I have an entity Product and an entity Order that has a product as a field, and while handling a request that is coming in from a Controller, would the result of Product product = order.getProduct() be cached throughout the entire call, even though the result of that method call itself is no longer assigned a value?如果我有一个实体Product和一个将product作为字段的实体Order ,并且在处理来自 Controller 的请求时, Product product = order.getProduct()的结果是否会在整个调用过程中被缓存,即使该方法调用本身的结果不再被赋值? Assume product is not modified and could be safely discarded.假设product没有被修改并且可以被安全丢弃。

ie something like:即类似的东西:


Product product = order.getProduct();
doOtherStuff(order);


void doOtherStuff(Order order) {
    Product product = order.getProduct();
}

Does the latter call order.getProduct() always/never/usually/sometimes lead to another database call?后者调用order.getProduct()总是/从不/通常/有时会导致另一个数据库调用吗? What are the conditions for that?这样做的条件是什么? Assuming a Spring Boot application.假设 Spring 启动应用程序。

I realize I can see database queries for a line of code but I aim to understand the mechanism behind it.我意识到我可以看到一行代码的数据库查询,但我的目标是了解它背后的机制。

You could activate the option to show all the queries that are performed by adding these properties to your application.properties file.您可以通过将这些属性添加到application.properties文件来激活选项以显示所有执行的查询。

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

And then you can see whether it performs the query twice or not.然后你可以看到它是否执行了两次查询。

To answer shortly, from my experience, it does not cache anything .简短地说,根据我的经验,它不会缓存任何东西 So it performs the query/call to DB each time you do the call.因此,每次您进行调用时,它都会执行对 DB 的查询/调用。

You could check caching in Spring Boot:您可以在 Spring 引导中检查缓存:

https://www.baeldung.com/spring-cache-tutorial https://www.baeldung.com/spring-cache-tutorial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM