[英]Mapping two CacheConcurrencyStrategy for the same Hibernate second-level cache region
我为单个区域设置了不同的缓存策略,如“读写”和“只读”用法,当我尝试更新Carro Entity时,抛出以下异常:
错误org.hibernate.internal.SessionImpl - HHH000346:托管刷新期间出错[无法写入只读对象]
线程“main”中的异常java.lang.UnsupportedOperationException:无法写入只读对象
如果我将不同地区的实体分开工作。 那么,在同一地区不能有两种不同类型的策略?
Ps。:接收此警告:HHH020007:为可变实体配置的只读缓存
- > Carro:
@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>
该区域必须具有单个CacheConcurrencyStrategy
。 在您的情况下, Pessoa
类必须在Carro
之后注册,因此myregion
设置为READ_ONLY
。
默认情况下,每个实体都有不同的区域工厂,因此您可以基于每个实体设置不同的CacheConcurrencyStrategy
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.