Product and ProductTag form a one-to-many relationship, as shown below.
@Entity
public class Product {
@Id
Long id;
@OneToMan(mappedBy = "product")
List<ProductTag> productTags;
}
@Entity
public class ProductTag {
@Id
Long id;
String content;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_id")
Product product;
}
Now I have an API that searches products, then returns them with their tags. Every time I call product.getProductTags()
, Hibernate will fire an SQL query. Since the MySQL server is far away from the app server, I would like to cache product.getProductTags()
call. How do I achieve that?