gpt4 book ai didi

java - 为同一个 Hibernate 二级缓存区域映射两个 CacheConcurrencyStrategy

转载 作者:行者123 更新时间:2023-11-29 04:42:37 25 4
gpt4 key购买 nike

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

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

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

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

Ps.: 也收到这个警告:HHH020007: read-only cache configured for mutable entity


->卡罗:

@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

关于java - 为同一个 Hibernate 二级缓存区域映射两个 CacheConcurrencyStrategy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38578631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com