Spring Data JPA - 如何缓存包含在许多其他实体中的实体
Spring Data JPA - How to cache entity, which is included in many of other entities
我使用 Spring Data JPA 从存储库中获取实体。我有一个名为类别的特定实体,它可以包含在报价、项目和用户中。每次我从 JpaRepository 加载其中一些实体时,Spring 都会发出其他请求来获取类别。因此,合作伙伴实体如下所示:
@Entity
class Project(...) {
constructor() : this(...)
@Id
var id: String = IDGenerator.longId()
@ManyToMany
var categories: MutableList<Category> = mutableListOf()
@ManyToOne
var mainCategory: Category? = null
//other fiels
}
类别如下所示:
@Entity
class Category(var name: String,
var icon: String) {
constructor() : this("", "")
@Id
var id: String = IDGenerator.longId()
var marker: String = "default-category.png"
@ElementCollection
var defaultImg: MutableList<String> = mutableListOf("default.jpg")
}
我如何缓存类别并使它们不从数据库加载,按 ID?
P.S。项目中大约有40-50个类别。
您想使用休眠"second level cache"
1 将二级缓存库之一添加到您的 pom.xml 文件中。我更喜欢 ehcache,但你可以使用任何其他的。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<!--Your hibernate version-->
<version>5.2.2.Final</version>
</dependency>
2启用二级缓存
persistence.xml
<properties>
...
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
...
</properties>
或者您可以在 application.properties
spring.data.jpa.hibernate.cache.use_second_level_cache=true
spring.data.jpa.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
3 将@Cacheable 注释添加到您的实体。
@Entity
@Cacheable
class Category(...){
.........
}
这就是开始。 Category会从DB中读取一次,存储在二级缓存中。下次 Hibernate 将从那里获取它,没有任何新的 "select from".
我使用 Spring Data JPA 从存储库中获取实体。我有一个名为类别的特定实体,它可以包含在报价、项目和用户中。每次我从 JpaRepository 加载其中一些实体时,Spring 都会发出其他请求来获取类别。因此,合作伙伴实体如下所示:
@Entity
class Project(...) {
constructor() : this(...)
@Id
var id: String = IDGenerator.longId()
@ManyToMany
var categories: MutableList<Category> = mutableListOf()
@ManyToOne
var mainCategory: Category? = null
//other fiels
}
类别如下所示:
@Entity
class Category(var name: String,
var icon: String) {
constructor() : this("", "")
@Id
var id: String = IDGenerator.longId()
var marker: String = "default-category.png"
@ElementCollection
var defaultImg: MutableList<String> = mutableListOf("default.jpg")
}
我如何缓存类别并使它们不从数据库加载,按 ID?
P.S。项目中大约有40-50个类别。
您想使用休眠"second level cache"
1 将二级缓存库之一添加到您的 pom.xml 文件中。我更喜欢 ehcache,但你可以使用任何其他的。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<!--Your hibernate version-->
<version>5.2.2.Final</version>
</dependency>
2启用二级缓存 persistence.xml
<properties>
...
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
...
</properties>
或者您可以在 application.properties
spring.data.jpa.hibernate.cache.use_second_level_cache=true
spring.data.jpa.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
3 将@Cacheable 注释添加到您的实体。
@Entity
@Cacheable
class Category(...){
.........
}
这就是开始。 Category会从DB中读取一次,存储在二级缓存中。下次 Hibernate 将从那里获取它,没有任何新的 "select from".