无法通过反射设置器设置字段值 [英] could not set a field value by reflection setter

查看:472
本文介绍了无法通过反射设置器设置字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用休眠和MySQL的spring mvc应用程序中,出现一个错误,似乎表明Name实体找不到Patient实体的BaseEntity超类的id属性的设置器. .

如何解决此错误?

这是错误消息:

Caused by: org.hibernate.PropertyAccessException: could not set a field value by  
reflection setter of myapp.mypackage.Name.patient

以下是触发错误的代码行:

ArrayList<Name> names = (ArrayList<Name>) this.clinicService.findNamesByPatientID(patntId);

这是BaseEntity,它是PatientName的超类:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorFormula("(CASE WHEN dtype IS NULL THEN 'BaseEntity' ELSE dtype END)")
public class BaseEntity {

    @Transient
    private String dtype = this.getClass().getSimpleName();

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    protected Integer id;

    public void setId(Integer id) {this.id = id;}
    public Integer getId() {return id;}

    public void setDtype(String dt){dtype=dt;}
    public String getDtype(){return dtype;}

    public boolean isNew() {return (this.id == null);}

}

这是Patient实体:

@Entity
@Table(name = "patient")
public class Patient extends BaseEntity{

    @OneToMany(mappedBy = "patient")
    private Set<Name> names;

    protected void setNamesInternal(Set<Name> nms) {this.names = nms;}

    protected Set<Name> getNamesInternal() {
        if (this.names == null) {this.names = new HashSet<Name>();}
        return this.names;
    }

    public List<Name> getNames() {
        List<Name> sortedNames = new ArrayList<Name>(getNamesInternal());
        PropertyComparator.sort(sortedNames, new MutableSortDefinition("family", true, true));
        return Collections.unmodifiableList(sortedNames);
    }

    public void addName(Name nm) {
        getNamesInternal().add(nm);
        nm.setPatient(this);
    }

    //other stuff
}

这是Name实体:

@Entity
@Table(name = "name")
public class Name extends BaseEntity{

    @ManyToOne
    @JoinColumn(name = "patient_id")
    private Patient patient;

    public Patient getPatient(){return patient;}
    public void setPatient(Patient ptnt){patient=ptnt;}

//other stuff

}

可以在此链接> 中查看.

Hibernate为上述查询生成的SQL是:

select distinct hl7usname0_.id as id1_0_0_, givennames1_.id as id1_45_1_,  
hl7usname0_.family as family1_44_0_, hl7usname0_.patient_id as patient3_44_0_,
hl7usname0_.person_id as person4_44_0_, hl7usname0_.suffix as suffix2_44_0_,  
hl7usname0_.usecode as usecode5_44_0_, hl7usname0_.codesystem as codesyst6_44_0_,  
givennames1_.given as given2_45_1_, givennames1_.name_id as name3_45_1_,  
givennames1_.name_id as name3_0_0__, givennames1_.id as id1_45_0__  
from hl7_usname hl7usname0_  
left outer join hl7_usname_given givennames1_ on hl7usname0_.id=givennames1_.name_id  
where hl7usname0_.patient_id=1

当我通过My​​SQL命令行客户端运行此查询时,它返回测试数据库表中的唯一记录.

解决方案

那不是堆栈跟踪所说的.堆栈跟踪并不表示无法设置ID.它说:

原因:java.lang.IllegalArgumentException:无法将org.springframework.samples.knowledgemanager.model.HL7Patient字段org.springframework.samples.knowledgemanager.model.HL7USName.Patient设置为org.springframework.samples.knowledgemanager.model .HL7USName

因此,您的HL7USName类具有一个名为patient且类型为HL7Patient的字段,并且无法使用HL7USName类型的值来设置此字段.

这意味着您的数据库包含一个Name,该Name具有到Name类型的行而不是Patient类型的行的外键.

In a spring mvc application using hibernate and MySQL, I am getting an error which seems to indicate that a Name entity cannot find the setter for the id property of the BaseEntity superclass of the Patient entity.

How can I resolve this error?

Here is the error message:

Caused by: org.hibernate.PropertyAccessException: could not set a field value by  
reflection setter of myapp.mypackage.Name.patient

Here is the line of code that triggers the error:

ArrayList<Name> names = (ArrayList<Name>) this.clinicService.findNamesByPatientID(patntId);

Here is the BaseEntity, which is the superclass of both Patient and Name:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorFormula("(CASE WHEN dtype IS NULL THEN 'BaseEntity' ELSE dtype END)")
public class BaseEntity {

    @Transient
    private String dtype = this.getClass().getSimpleName();

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    protected Integer id;

    public void setId(Integer id) {this.id = id;}
    public Integer getId() {return id;}

    public void setDtype(String dt){dtype=dt;}
    public String getDtype(){return dtype;}

    public boolean isNew() {return (this.id == null);}

}

Here is the Patient entity:

@Entity
@Table(name = "patient")
public class Patient extends BaseEntity{

    @OneToMany(mappedBy = "patient")
    private Set<Name> names;

    protected void setNamesInternal(Set<Name> nms) {this.names = nms;}

    protected Set<Name> getNamesInternal() {
        if (this.names == null) {this.names = new HashSet<Name>();}
        return this.names;
    }

    public List<Name> getNames() {
        List<Name> sortedNames = new ArrayList<Name>(getNamesInternal());
        PropertyComparator.sort(sortedNames, new MutableSortDefinition("family", true, true));
        return Collections.unmodifiableList(sortedNames);
    }

    public void addName(Name nm) {
        getNamesInternal().add(nm);
        nm.setPatient(this);
    }

    //other stuff
}

Here is the Name entity:

@Entity
@Table(name = "name")
public class Name extends BaseEntity{

    @ManyToOne
    @JoinColumn(name = "patient_id")
    private Patient patient;

    public Patient getPatient(){return patient;}
    public void setPatient(Patient ptnt){patient=ptnt;}

//other stuff

}

The complete stack trace can be viewed at this link.

The SQL generated by Hibernate for the above query is:

select distinct hl7usname0_.id as id1_0_0_, givennames1_.id as id1_45_1_,  
hl7usname0_.family as family1_44_0_, hl7usname0_.patient_id as patient3_44_0_,
hl7usname0_.person_id as person4_44_0_, hl7usname0_.suffix as suffix2_44_0_,  
hl7usname0_.usecode as usecode5_44_0_, hl7usname0_.codesystem as codesyst6_44_0_,  
givennames1_.given as given2_45_1_, givennames1_.name_id as name3_45_1_,  
givennames1_.name_id as name3_0_0__, givennames1_.id as id1_45_0__  
from hl7_usname hl7usname0_  
left outer join hl7_usname_given givennames1_ on hl7usname0_.id=givennames1_.name_id  
where hl7usname0_.patient_id=1

When I run this query through the MySQL command line client, it returns the only record in the test database table.

解决方案

That's not what the stack trace says. The stack trace doesn't say that the ID can't be set. It says:

Caused by: java.lang.IllegalArgumentException: Can not set org.springframework.samples.knowledgemanager.model.HL7Patient field org.springframework.samples.knowledgemanager.model.HL7USName.patient to org.springframework.samples.knowledgemanager.model.HL7USName

So, your HL7USName class has a field named patient of type HL7Patient, and it's impossible to set this field with a value of type HL7USName.

This means that your database contains a Name that has a foreign key to a row of type Name instead of a row of type Patient.

这篇关于无法通过反射设置器设置字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆