I have been getting this exception. What would be the best way to implement versioning in an entity so that I can persist my entity? I would like to avoid unnecessary setters if possible
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)
This is my entity
@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
The error message pretty much tells you your options:
no setter
: create a setter. Actually just making version
package private (no modifier) and not final is sufficient
no wither
: create a "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)
: Create a constructor that includes the version. Either by adding it to the parameter list or by creating a separate constructor. In the later case you'll need to annotate that with @PersistenceCreator