1

I am facing issue while saving a parent entity with inheritance if it already exists. I have an entities as below,

@Entity
@PrimaryKeyJoinColumn(name = "CHILD_ID")
public class ChildEntity extends ParentEntity {
  @Column(name = "NAME")
  private String name;

  @Column(name = "TYPE")
  @Enumerated(EnumType.STRING)
  private RoleEnum roleEnum;

  //getter setters
}


@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class ParentEntity implements Persistable<String>, Serializable {

  @Id
  @Column(name = "PARENT_ID", length = 36, columnDefinition = "uniqueidentifier")
  private String parentId;

  @CreatedBy
  @Column(name = "created_by", length = 50, updatable = false)
  private String createdBy;

  @CreatedDate
  @Column(name = "created_date", updatable = false)
  private Instant createdDate = Instant.now();

  @LastModifiedBy
  @Column(name = "last_modified_by", length = 50)
  private String lastModifiedBy;

  @LastModifiedDate
  @Column(name = "last_modified_date")
  private Instant lastModifiedDate = Instant.now();

  @Column(name = "salutation", length = 20)
  private String salutation;
}

When I am saving an Child entity, I want to update the parent entity if it already exists, but it is always inserting and failing with Violation of PRIMARY KEY constraint.

I have tried returning isNew as false but still it is not working,

@Override
@Transient
public boolean isNew() {
    return false;
}

Am I missing something? Highly appreciate any help.

Vikas
  • 6,868
  • 4
  • 27
  • 41

0 Answers0