具有@Lob字段的实体是否可以在Hibernate中缓存?

时间:2016-03-15 11:54:12

标签: java hibernate jpa lob

我使用Hibernate和MySQL,我需要将文章存储在数据库中。文章实体有一个字段:

@Lob
@Basic(fetch=EAGER)
@Column(name="CONTENT")
private byte[] content;

这个实体可以在Hibernate中缓存吗?

1 个答案:

答案 0 :(得分:0)

您可以使用@Cacheable注释缓存您的实体,您应该在<shared-cache-mode>(或等效的javax.persistence.sharedCache.mode)中指定persistence.xml创建EntityManagerFactory)。

下面是一个示例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>
相关问题