【问题标题】:Are the entities with @Lob fields cacheable in Hibernate?具有@Lob 字段的实体是否可以在 Hibernate 中缓存?
【发布时间】:2016-03-15 11:54:11
【问题描述】:

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

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

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

【问题讨论】:

    标签: java hibernate jpa lob


    【解决方案1】:

    您可以使用@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>
    

    【讨论】:

    • 是的,因为您将缓存整个实体
    猜你喜欢
    • 1970-01-01
    • 2011-06-24
    • 2011-12-17
    • 2011-06-23
    • 2019-06-09
    • 1970-01-01
    • 2016-06-03
    • 2021-10-12
    • 2011-05-09
    相关资源
    最近更新 更多