无法通过reflection设置器设置字段值
在使用hibernate和MySQL的spring mvc应用程序中,我收到一个错误,这似乎表明Name
实体无法找到Patient
实体的BaseEntity
超类的id
属性的setter。
我该如何解决这个错误?
这是错误消息:
Caused by: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of myapp.mypackage.Name.patient
以下是触发错误的代码行:
ArrayList names = (ArrayList) this.clinicService.findNamesByPatientID(patntId);
这是BaseEntity
,它是Patient
和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);} }
这是Patient
实体:
@Entity @Table(name = "patient") public class Patient extends BaseEntity{ @OneToMany(mappedBy = "patient") private Set names; protected void setNamesInternal(Set nms) {this.names = nms;} protected Set getNamesInternal() { if (this.names == null) {this.names = new HashSet();} return this.names; } public List getNames() { List sortedNames = new ArrayList(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
当我通过MySQL命令行客户端运行此查询时,它返回测试数据库表中的唯一记录。
这不是堆栈跟踪所说的。 堆栈跟踪并未说明无法设置ID。 它说:
引起:java.lang.IllegalArgumentException:无法将org.springframework.samples.knowledgemanager.model.HL7Patient字段org.springframework.samples.knowledgemanager.model.HL7USName.patient设置为org.springframework.samples.knowledgemanager.model.HL7USName
因此,您的HL7USName类有一个名为HL7Patient
类型的HL7Patient
,并且无法使用HL7USName类型的值设置此字段。
这意味着您的数据库包含一个Name,该Name具有Name类型行的外键,而不是Patient类型的行。
- LDAP:错误代码50 – 由于访问权限不足,无法添加条目uid = test @ info.com,ou = People,o = Drive,dc = company,dc = com
- Hibernate在单个表中对复合外键和主键进行一对一映射
- java web应用程序布局,请解释一些设计原则/模式
- 谁负责MySQL和Hibernate之间主键的自动增量?
- Grails – 为什么需要交易?
- 使用Tomcat和gradle进行Hibernate
- jpa将儿童从收集中删除
- Oracle遗留表没有很好的PK:如何hibernate?
- hibernate中的createSQLQuery使用Prepared Statement?