2016-07-25 69 views
1

我爲單個區域設置了不同的緩存策略,如「讀寫」和「只讀」使用,當我嘗試更新Carro實體時,出現以下異常被拋出:爲同一個Hibernate二級緩存區域映射兩個CacheConcurrencyStrategy

錯誤org.hibernate.internal.SessionImpl - HHH000346:錯誤在管理沖水[無法寫入只讀對象]

異常線程 「main」 java.lang.UnsupportedOperationException :不能寫入只讀對象

如果我將不同地區的實體分開工作。那麼,在同一地區不能有兩種不同類型的戰略?

詩:獲此警告過: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> 

回答

3

該地區必須有一個CacheConcurrencyStrategy。在您的情況下,Pessoa類必須已在Carro之後註冊,因此myregion設置爲READ_ONLY

默認情況下,每個實體都有不同的地區工廠,因此您可以根據實體設置不同的CacheConcurrencyStrategy