0

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

Community
  • 1
  • 1
rakesh99
  • 1,234
  • 19
  • 34
  • Have a look at http://docs.oracle.com/javaee/6/api/javax/persistence/Version.html - Version handling should strictly be managed by your Backend / JPA implementation. "Manual" version handling can cause many unexpected problems in a multi-threaded environment, for instance... – MWiesner Aug 01 '15 at 11:02
  • the doc says this serves purpose of optimistic locking and this cannot be used to maintain revision history of a row.Isn't it so? using it on the version field gives stateobject exception on update requests for that id – rakesh99 Aug 01 '15 at 19:20

1 Answers1

1

There's a very short answer to your question: Once you set an id in a JPA entity, the id cannot be updated its value (be a native type, object or composite key).

If you want that behaviour, you need to delete the old entity and create a new one with the new version.

This might point to a flaw in you data model. The version shouldn't be part of the @Id, as you already have the UUID column.. and it looks like you don't want 2 rows with the same "UUID" and different version.

Augusto
  • 28,839
  • 5
  • 58
  • 88
  • thanks..i had thought to have only one generated Id and use hibernate enver for version maintenance..the data model in this post is faulty but my concern was why merge generates a new id – rakesh99 Aug 01 '15 at 16:26
  • Using an embeddedId (with ID and version) and generating it using some code in create method and in update just updating the version part of the embeddedId works resulting in different version rows of an Id.Here also the Id(embeddedId) is being modified.Isn't it? – rakesh99 Aug 01 '15 at 16:39