[英]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_contacts
和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.