简体   繁体   English

org.hibernate.PropertyAccessException:无法使用复合键设置字段值

[英]org.hibernate.PropertyAccessException: Could not set field value with Composite Key

Using Spring Boot with Hibernate JPA 将Spring Boot与Hibernate JPA结合使用

I am having trouble accessing a DAO for an @Entity which has a composite key where one of the columns is a foreign key. 我在访问具有组合键的@Entity的DAO时遇到问题,其中列之一是外键。 It's giving me org.hibernate.PropertyAccessException: Could not set field value [...] by reflection when I try to do a findOne() using the DAO. 它给了我org.hibernate.PropertyAccessException: Could not set field value [...] by reflection当我尝试使用DAO进行findOne()时, org.hibernate.PropertyAccessException: Could not set field value [...] by reflection

So I have two MySQL relations, all_contacts and contact_phones , represented in order here: 所以我有两个MySQL关系, all_contactscontact_phones ,在这里按顺序表示:

所有联系人 contact_phones

contact_phones has a composite primary key consisting of contactid + number , of those two, contactId is also a foreign key for the same value in all_contacts . contact_phones具有由一个复合主键contactid + number ,这两个的, contactId也是在相同的值的外键all_contacts I've established the relationship using the proper @OneToMany and @ManyToOne annotations 我已经使用正确的@OneToMany@ManyToOne注释建立了关系

Entity for all_contacts: 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

    }

Entity for contact_phones: 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
}

And, because the primary key of the contact_phones class was composite (hence the @IdClass(ContactPhonesKey.class) ), I was forced to create a Key class to direct it: 并且,由于contact_phones类的主键是复合键(因此@IdClass(ContactPhonesKey.class) ),我被迫创建一个Key类来对其进行定向:

Class for ContactPhonesKey: 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 

}

However, whenever I try to access something by the DAO (when I have created an instance of it by @Autowired) I made for the contact_phones class: 但是,每当我尝试通过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);

}

I am getting an error about not being able to set the ContactPhonesKey class due to reflection. 我由于反射而无法设置ContactPhonesKey类,并收到一个错误消息。 Here's the full error I get: 这是我得到的全部错误:

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

There's a type mismatch on the field number between your entity ContactPhones and ID Class ContactPhonesKey . 您的实体ContactPhones和ID类ContactPhonesKey之间的字段number类型不匹配。 On the entity, it is declared as BigInteger , while on the ID Class, it is declared as String . 在实体上,它被声明为BigInteger ,在ID类上,它被声明为String

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ManyToOne获取org.hibernate.PropertyAccessException的JPA Composite键:无法通过反射设置器设置字段值 - JPA Composite key with ManyToOne getting org.hibernate.PropertyAccessException: could not set a field value by reflection setter of 嵌套的异常是org.hibernate.PropertyAccessException:无法设置字段值 - nested exception is org.hibernate.PropertyAccessException: Could not set field value Hibernate &amp; Spring - org.hibernate.PropertyAccessException: 无法通过反射设置字段值 [1] 值 - Hibernate & Spring - org.hibernate.PropertyAccessException: Could not set field value [1] value by reflection org.hibernate.PropertyAccessException:无法通过反射getter获取字段值 - org.hibernate.PropertyAccessException: could not get a field value by reflection getter of org.hibernate.PropertyAccessException: 无法通过反射为 String 设置字段值 [STRING] 值 - org.hibernate.PropertyAccessException: Could not set field value [STRING] value by reflection for String 如何修复org.hibernate.PropertyAccessException:无法通过反射设置器设置字段值 - How to fix org.hibernate.PropertyAccessException: Could not set field value value by reflection setter of 错误:-org.hibernate.PropertyAccessException:无法通过反射获取器获取字段值 - Error :- org.hibernate.PropertyAccessException: could not get a field value by reflection getter org.hibernate.PropertyAccessException持续存在 - org.hibernate.PropertyAccessException on persist org.hibernate.PropertyAccessException:空值已分配给布尔类型的属性 - org.hibernate.PropertyAccessException: Null value was assigned to a property of boolean type org.hibernate.PropertyAccessException-如何从数据库中获取空值? - org.hibernate.PropertyAccessException - How to get a null value out of database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM