我一直遇到这个异常。在实体中实现版本控制以便我可以持久保存我的实体的最佳方法是什么?如果可能的话,我想避免不必要的设置器
java.lang.IllegalStateException: Cannot set property version because no setter, no wither and it's not part of the persistence constructor public com.ezycorp.eve.domain.Merchant(java.lang.String,java.lang.String,java.lang.Integer,java.lang.String,java.lang.String,java.lang.String)
这是我的实体
@Table("merchant")
public class Merchant {
@Id
private final MerchantId id;
private final String name;
private final MerchantKeyId merchantKeyId;
private final String username;
private final String password;
private final String webhookUrl;
@Version
private final int version=0;
@PersistenceCreator
public Merchant(String id, String name, Integer merchantKeyId, String username, String password, String webhookUrl) {
this.id = new MerchantId(id);
this.name = name;
this.merchantKeyId = new MerchantKeyId(merchantKeyId);
this.username = username;
this.password = password;
this.webhookUrl = webhookUrl;
}
}
...omitted getters
错误消息几乎告诉您您的选择:
no setter
:创建一个setter。实际上,只需将 version
包设为私有(无修饰符)而不是最终的就足够了
no wither
:创建“凋零”,
Merchant withVersion(int version){
Merchant newMerchant = ... // create a new instance with the new version
return newMerchant;
}
not part of the persistence constructor public com.ezycorp.eve.domain.Merchant(java.lang.String,java.lang.String,java.lang.Integer,java.lang.String,java.lang.String,java.lang.String)
:创建包含版本的构造函数。通过将其添加到参数列表或创建单独的构造函数。在后一种情况下,您需要用 @PersistenceCreator
进行注释