当前位置: 首页 > 知识库问答 >
问题:

多对多/连接表-Spring JPA Hibernate-无法通过反射设置值

夔波
2023-03-14

因此,我试图通过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模型的PK为ID,Person模型的字段为WSGlobalID。Person表上的键不是PK。

我有设置器、获取器、构造器和几乎所有的东西。不确定Hibernate在这里发生了什么,它找不到getter或其他什么?不知道这是怎么回事。

共有1个答案

乜嘉悦
2023-03-14

您应该有一个如下所示的setter:

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);
}
 类似资料:
  • 问题内容: 我无法使用Java反射将值设置为字段。字段数据类型为。但是,如果数据类型是基本类型,即I,我就可以设置该值。 这是带有类型和原始类型的简单VO : 这是我的java反射代码: 输出为: 我试图设定与价值,,等什么作品。 问题答案: 根据此,被调用以设置一个字段,该字段是用原语类型的值的参考布尔类型。在非反射等效项中,编译器会将原始类型’true’转换(或装箱)为引用类型,以便其类型检查

  • 我有以下两个类(和他们的pk类)与这些注释。我删除了setters/getters/hashcode/equals来压缩示例 我最终得到了这个错误 我的猜测是,因为“年”是在联接表中共享的,所以我弄乱了一些关联实体的语法。请注意,这些视图的实体之间存在隐式关系,我正试图在注释中对其建模。我尝试了一些JPA建模工具,这些工具在建模后给了我同样的错误。我已尝试将联接列设置为,并将设置为。 我当然可以只

  • 我有2个具有多对多关系的实体User和AcCountBase。我需要从连接表中选择所有具有选定用户ID的AcCountBase对象。我尝试了一些连接查询,但不起作用。

  • 我正在我的ubuntu上运行Hive2,并尝试通过hive接口和Beeline\JDBC创建表。我通过配置单元接口创建表没有问题,但是当通过jdbc访问时,我得到一个权限被拒绝的错误。 从异常中,我看到它试图在一个不存在的目录中创建表(/user/hive/warehouse/...) 那么它为什么要在/user/hive/warehouse下创建metastore_db呢?

  • 问题内容: 我在本地服务器中使用phpMyAdmin创建了一个mySQL数据库。在这个数据库中,我存储了我的朋友的名字和喜欢的NBA球队,这显然是多对多的关系。由于这个原因,我在MySQL中运行以下脚本来为此数据库创建适当的表: 显然,我在这些表中插入了一些值,但是为了节省空间,此处没有提供大量源代码。其中的一小部分如下: 运行从数据库中获取数据并打印它们的PHP脚本后,我想为我的每个朋友提供以下

  • 问题内容: 我知道这很长,但是请忍受我。这个问题很容易理解,只需花一些时间就可以完全解释它。 现在我遇到这个错误 我已经阅读了文档中的所有内容,但仍然找不到解决我问题的方法。 我在私有在线源上使用$ http.get,该私有源上的数据与json文件的形式相似(因此我无法修改数据)。数据如下所示: 我正在尝试将每个项目的videoId插值到嵌入YouTube视频的HTML iframe中。在我的co