【发布时间】:2016-03-15 11:54:11
【问题描述】:
我使用 Hibernate 和 MySQL,我需要将文章存储在数据库中。文章实体有一个字段:
@Lob
@Basic(fetch=EAGER)
@Column(name="CONTENT")
private byte[] content;
这个实体可以在 Hibernate 中缓存吗?
【问题讨论】:
我使用 Hibernate 和 MySQL,我需要将文章存储在数据库中。文章实体有一个字段:
@Lob
@Basic(fetch=EAGER)
@Column(name="CONTENT")
private byte[] content;
这个实体可以在 Hibernate 中缓存吗?
【问题讨论】:
您可以使用@Cacheable 注释来缓存您的实体,您应该在persistence.xml 中指定一个<shared-cache-mode>(或在创建EntityManagerFactory 时使用等效的javax.persistence.sharedCache.mode)。
下面是一个示例 persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="ProjectPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
...
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
...
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
</properties>
</persistence-unit>
</persistence>
【讨论】: