简体   繁体   中英

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? Assume product is not modified and could be safely discarded.

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? What are the conditions for that? Assuming a Spring Boot application.

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.

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.

You could check caching in Spring Boot:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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