我使用 Hibernate 和 MySQL,我需要将文章存储在数据库中。文章实体有一个字段:
@Lob
@Basic(fetch=EAGER)
@Column(name="CONTENT")
private byte[] content;
这个实体可以在 Hibernate 中缓存吗?
您可以使用注释缓存您的实体@Cacheable
,您应该<shared-cache-mode>
在persistence.xml
(或创建 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>