为同一个 Hibernate 二级缓存区域映射两个 CacheConcurrencyStrategy
Mapping two CacheConcurrencyStrategy for the same Hibernate second-level cache region
我为单个区域设置了不同的缓存策略,如 'read-write' 和 'read-only' 使用,当我尝试更新 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>
该地区必须有一个 CacheConcurrencyStrategy
。在您的情况下,Pessoa
class 必须在 Carro
之后注册,因此 myregion
设置为 READ_ONLY
。
默认情况下,每个实体都有不同的区域工厂,因此您可以为每个实体设置不同的CacheConcurrencyStrategy
。
我为单个区域设置了不同的缓存策略,如 'read-write' 和 'read-only' 使用,当我尝试更新 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>
该地区必须有一个 CacheConcurrencyStrategy
。在您的情况下,Pessoa
class 必须在 Carro
之后注册,因此 myregion
设置为 READ_ONLY
。
默认情况下,每个实体都有不同的区域工厂,因此您可以为每个实体设置不同的CacheConcurrencyStrategy
。