【问题标题】:org.hibernate.PropertyAccessException: Could not set field value with Composite Keyorg.hibernate.PropertyAccessException:无法使用复合键设置字段值
【发布时间】:2017-10-10 17:25:15
【问题描述】:

将 Spring Boot 与 Hibernate JPA 结合使用

我无法访问具有复合键的 @Entity 的 DAO,其中一列是外键。它给了我 org.hibernate.PropertyAccessException: Could not set field value [...] by reflection 当我尝试使用 DAO 执行 findOne() 时。

所以我有两个 MySQL 关系,all_contactscontact_phones,在这里按顺序表示:

contact_phones 有一个由contactid + number 组成的复合主键,在这两个中,contactId 也是all_contacts 中相同值的外键。我已经使用正确的 @OneToMany@ManyToOne 注释建立了关系

all_contacts 的实体:

    @Entity
    @Table(name = "all_contacts")
    public class Contact {

      @Column(name="userid", columnDefinition ="bigint(13)")
      private BigInteger userId;

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name="contactid", columnDefinition ="bigint(13)")
      private BigInteger contactId;


     @OneToMany(mappedBy = "contact", cascade = CascadeType.ALL)
     @ElementCollection(targetClass=ContactPhones.class)
     private Set<ContactPhones> phones = new HashSet<ContactPhones>(0);

    // the rest of the fields, including getters and setters

    }

contact_phones 的实体:

@Entity
@Table( name ="contact_phones")
@IdClass(ContactPhonesKey.class)
public class ContactPhones {


  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name="contactid", nullable = false)
  @Id
  private Contact contact;

  @Column(name="phone_type", columnDefinition = "")
  private String phoneType;

  @Id
  @Column(columnDefinition ="bigint(13)")
  private BigInteger number;

  // getters and setters
}

而且,由于 contact_phones 类的主键是复合的(因此是 @IdClass(ContactPhonesKey.class) ),我不得不创建一个 Key 类来指导它:

ContactPhonesKey 类:

public class ContactPhonesKey implements Serializable {

  private Contact contact;
  private String number;

  public ContactPhonesKey() {}

  public ContactPhonesKey(Contact contact, String number) {
    this.contact = contact;
    this.number = number;
  }

// getters and setters 

}

但是,每当我尝试通过 DAO 访问某些内容时(当我通过 @Autowired 创建了它的实例时),我都会为 contact_phones 类创建:

public interface ContactPhonesRepository extends CrudRepository<ContactPhones, BigInteger> {

  List<ContactPhones> findByNumberContaining(String number);


  @Query(value ="SELECT * FROM contact_phones cp WHERE cp.contactid= :contactId",
          nativeQuery=true)
  List<ContactPhones> findAllPhonesByContactId(@Param("contactId")BigInteger contactId);

}

我收到一个关于由于反射而无法设置 ContactPhonesKey 类的错误。这是我得到的完整错误:

Could not set field value [111456666] value by reflection : [class app.models.relationentities.ContactPhonesKey.number] setter of app.models.relationentities.ContactPhonesKey.number; nested exception is org.hibernate.PropertyAccessException: Could not set field value [111456666] value by reflection : [class app.models.relationentities.ContactPhonesKey.number] setter of app.models.relationentities.ContactPhonesKey.number

【问题讨论】:

    标签: java mysql spring hibernate


    【解决方案1】:

    在您的实体 ContactPhones 和 ID 类 ContactPhonesKey 之间的字段 number 上存在类型不匹配。在实体上,它被声明为BigInteger,而在ID类上,它被声明为String

    【讨论】:

      猜你喜欢
      • 2019-09-02
      • 2020-06-10
      • 1970-01-01
      • 2019-06-30
      • 2018-04-19
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      • 2020-08-25
      相关资源
      最近更新 更多