簡體   English   中英

多對多/連接表 - Spring JPA Hibernate - 無法通過反射設置值

[英]Many-to-Many/Junction Table - Spring JPA Hibernate - could not set value by reflection

所以,我正在嘗試通過 Spring JPA 進行多對多的實現。

我試圖做一個嵌入式密鑰實現。 但我收到了這個錯誤

{
    "message": "Internal server error",
    "details": "Could not set field value [26] value by reflection : [class com.domain.configuredview.model.RelationshipViewPersonPK.personId] setter of com.domain.configuredview.model.RelationshipViewPersonPK.personId; nested exception is org.hibernate.PropertyAccessException: Could not set field value [26] value by reflection : [class com.domain.configuredview.model.RelationshipViewPersonPK.personId] setter of com.domain.configuredview.model.RelationshipViewPersonPK.personId"
}

我嘗試搜索所有答案,但無法解決此問題。

我的課是


import com.domain.person.model.Person;
import java.io.Serializable;
import javax.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@Entity
@Table(name = "relationship_view_person")
public class RelationshipViewPerson implements Serializable {

  @EmbeddedId
  private RelationshipViewPersonPK entryId;

  @ManyToOne(fetch = FetchType.LAZY)
  @MapsId("viewId")
  @JoinColumn(name = "view_id", nullable = false)
  private ConfiguredView view;

  @ManyToOne(fetch = FetchType.LAZY)
  @MapsId("personId")
  @JoinColumn(name = "person_id", nullable = false)
  private Person person;

  public RelationshipViewPerson(ConfiguredView view, Person person) {
    this.view = view;
    this.person = person;
    this.entryId = new RelationshipViewPersonPK(view.getId(), person.getWsGlobalId());
  }

  public void setEntryId(Long viewId, String personId) {
    this.entryId.setViewId(viewId);
    this.entryId.setPersonId(personId);
  }

  public void setView(ConfiguredView view) {
    this.view = view;
  }

  public void setPerson(Person person) {
    this.person = person;
  }

  public Person getPerson() {
    return this.person;
  }

  public ConfiguredView getView() {
    return this.view;
  }
}

關系鍵是


import java.io.Serializable;
import javax.persistence.*;
import lombok.NoArgsConstructor;

@Embeddable
@NoArgsConstructor
public class RelationshipViewPersonPK implements Serializable {

  @Column(name = "view_id")
  private Long viewId;

  @Column(name = "person_id")
  private String personId;

  public RelationshipViewPersonPK(Long viewId, String personId) {
    this.viewId = viewId;
    this.personId = personId;
  }

  // setters & getters
  public void setViewId(Long viewId) {
    this.viewId = viewId;
  }

  public void setPersonId(String personId) {
    this.personId = personId;
  }

  public String getPersonId() {
    return this.personId;
  }

  public Long getViewId() {
    return this.viewId;
  }

  // Override equals and hashCode

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }

    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    RelationshipViewPersonPK that = (RelationshipViewPersonPK) o;

    return this.viewId.equals(that.viewId) && this.personId.equals(that.personId);
  }

  @Override
  public int hashCode() {
    return this.viewId.hashCode() + this.personId.hashCode();
  }
}

ConfiguredView model 的 PK 為idPerson model 的字段為wsGlobalId Person 表上的鍵不是 PK。

我有 setter、getter、constructor 和幾乎所有東西。 不確定 Hibernate 在這里發生了什么,它找不到吸氣劑或其他什么? 不知道這是怎么回事。

你應該有一個像下面這樣的二傳手:

public void setEntryId(RelationshipViewPersonPK entryId) {
    this.entryId.setViewId(entryId.getViewId());
    this.entryId.setPersonId(entryId.getPersonId());
}

代替(或除此之外):

public void setEntryId(Long viewId, String personId) {
    this.entryId.setViewId(viewId);
    this.entryId.setPersonId(personId);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM