I have an Entity with id and version.On update I am trying to update it's version say
id version OtherProperty1 .... OtherPropertyN
XXX 0 abc1 abcN //on create
XXX 1 something2 something //on update
After having issues with auto-incerment id as in linked SO post(Case 5 fails with illegal argument exception).I tried with uuid2 strategy which doesn't fail with the same exception.It works but inserts a new row as decribed below
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Id
@Column(name = "version")
public int getVersion() {
return version;
}
I have a update method
@Override
public void updateEntity(SomeEntity pEntity) {
int version = pEntity.getVersion();
pEntity.setVersion(version + 1); //causes generation of new ID
entityManager.merge(pEntity);
}
pEntity.setVersion(version + 1);
lines causes generation of new ID which results in updated row with new ID and version 0.Commenting this results in successful updation of same row with given ID
It is simple to write a sql insert query for this but how to make JPA generate the same query in this case?
PS:Although using hibernate enver would be the practical approach of maintaining versions my question is about solving the issue using approach in this post