gpt4 book ai didi

java - 组织.hibernate.PropertyAccessException : Could not set field value with Composite Key

转载 作者:行者123 更新时间:2023-11-29 05:59:00 24 4
gpt4 key购买 nike

将 Spring Boot 与 Hibernate JPA 结合使用

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

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

all_contacts contact_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

最佳答案

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

关于java - 组织.hibernate.PropertyAccessException : Could not set field value with Composite Key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46672520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com