【问题标题】:Mapping two CacheConcurrencyStrategy for the same Hibernate second-level cache region为同一个Hibernate二级缓存区域映射两个CacheConcurrencyStrategy
【发布时间】:2016-11-29 10:30:26
【问题描述】:

我为单个区域设置了不同的缓存策略,例如“读写”和“只读”使用,当我尝试更新 Carro 实体时,抛出以下异常:

错误 org.hibernate.internal.SessionImpl - HHH000346:托管刷新期间出错 [无法写入只读对象]

线程“main”java.lang.UnsupportedOperationException 中的异常:无法写入只读对象

如果我将不同区域的实体分开工作。那么,不能在同一个区域有两种不同类型的策略吗?

Ps.:也收到此警告:HHH020007:为可变实体配置了只读缓存


-> 卡罗:

@Entity
@Table(name = "carro")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "myregion")
public class Carro implements Serializable
{ 
     private static final long serialVersionUID = 8467432396096896736L;

     @Id
     @Column(name = "id")
     private Integer id;

     @Column(name = "carro")
     private String carro;

     @OneToMany(mappedBy = "carro", fetch = FetchType.LAZY)
     private List<Pessoa> pessoas = new ArrayList<Pessoa>();
}

-> 佩索阿:

@Entity
@Table(name = "pessoa")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "myregion")
public class Pessoa implements Serializable
{
    private static final long serialVersionUID = 8467432396096896736L;

    @Id
    @Column(name = "id")
    private Integer id;

    @Column(name = "nome")
    private String Nome;

    @Column(name = "sexo")
    private String sexo;

    @Column(name = "idade")
    private Integer idade;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "carro_id")
    private Carro carro;
}

->ehcache.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
         monitoring="autodetect" dynamicConfig="true">

    <cache name="myregion" maxEntriesLocalHeap="1000" eternal="false" timeToLiveSeconds="1000">
        <persistence strategy="none"/>
    </cache>

    <cache name="org.hibernate.cache.internal.StandardQueryCache" maxEntriesLocalHeap="1000" eternal="false" timeToLiveSeconds="120">
        <persistence strategy="none"/>
    </cache>

    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache" maxEntriesLocalHeap="1000" eternal="true">
        <persistence strategy="none"/>
    </cache>
</ehcache>

【问题讨论】:

    标签: java hibernate jpa orm second-level-cache


    【解决方案1】:

    该区域必须有一个 CacheConcurrencyStrategy。在您的情况下,Pessoa 类必须在Carro 之后注册,因此myregion 设置为READ_ONLY

    默认情况下,每个实体都有不同的区域工厂,因此您可以为每个实体设置不同的CacheConcurrencyStrategy

    【讨论】:

      猜你喜欢
      • 2020-03-26
      • 1970-01-01
      • 2023-03-24
      • 2015-05-14
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      相关资源
      最近更新 更多