简体   繁体   中英

Spring Boot & Hibernate - identifier of an instance of x was altered to null

We have a super class for all entities called Heartcore with @MappedSuperclass .

@Id
private String id;

@Column(nullable = false)
@NonNull
private String uuid;

@NonNull
@Column(nullable = false)
private String kanton;

// specific Setter
public void setKanton(String kanton) {
  if (kanton != null) {
    id = uuid + "_" + kanton;
  }
  this.kanton = kanton;
}

public void setUuid(String uuid) {
  if (kanton != null) {
    id = uuid + "_" + kanton;
  }
  this.uuid = uuid;
}

I have the following unidirectional one-to-one relation:

Child-Entity

class OtoUniChild extends Heartcore {

@Id
private String id;

@OneToOne
@MapsId
private OtoUniParent otoUniParent;

public void setOtoUniParent(OtoUniParent otoUniParent) {
    if (otoUniParent != null) {
        if (StringUtils.isNoneBlank(otoUniParent.getUuid(), otoUniParent.getKanton())) {
            this.otoUniParent = otoUniParent;
            setUuid(otoUniParent.getUuid());
            setKanton(otoUniParent.getKanton());
            return;
        }
        throw new InvalidObjectException("Es wurde ein invalides Objekt übergeben!");
    }
    this.otoUniParent = null;
}

When I persist a parent and a child and then try to get a child from the child repository with findById(149727fd-b0ce-4069-bcd9-d67fdbe69021_SG) I just got the error "org.springframework.orm.jpa.JpaSystemException: identifier of an instance of x.data.OtoUniChild was altered from 149727fd-b0ce-4069-bcd9-d67fdbe69021_SG to null;" . I don't know how this can happen!

*Spring Boot v.1.5.8.RELEASE

I believe this is due to doubly defined @Id in your OtoUniChild class:

  • once in Heartcore superclass.
  • once in the entity itself

While such re-definition is not allowed with @MappedSuperclass , you happen to re-define it with same name and type. However, you still can expect some methods to see the former, and some the latter fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM