0

I am trying to achive the optimistic locking by using the @Version. But getting the exception 'object references an unsaved transient instance - save the transient instance before flushing'. I have a record in the parent table with version set to 0. Used javax.persistence.Version and not the org.springframework.data.annotation.Version, as spring annotation version is not working as expected. What am I missing here? The code is working as expected without the @Version annotation. These are my classes.

 @Entity
 public class ChildEntity {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    @OneToOne
    @JoinColumn
    private ParentEntity parent;

    @Version
    private Long version;
} 

@Entity
public class ParentEntity {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    @Version
    private Long version;

}

Service class:

public ChildEntity saveChild(ChildEntity childEntity) {
        return repo.save(childEntity);
    }

Json to create new childEntity

{
    "name" : "user1",
    "parent": {
        "id": 1
    }
}

Joel George
  • 62
  • 2
  • 12

0 Answers0