Cod*_*Med 5 java mysql spring hibernate
在使用hibernate和MySQL的spring mvc应用程序中,我收到的错误似乎表明Name实体无法找到实体超类id属性的setter .BaseEntityPatient
我该如何解决这个错误?
这是错误消息:
Caused by: org.hibernate.PropertyAccessException: could not set a field value by
reflection setter of myapp.mypackage.Name.patient
Run Code Online (Sandbox Code Playgroud)
以下是触发错误的代码行:
ArrayList<Name> names = (ArrayList<Name>) this.clinicService.findNamesByPatientID(patntId);
Run Code Online (Sandbox Code Playgroud)
这是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);}
}
Run Code Online (Sandbox Code Playgroud)
这是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
}
Run Code Online (Sandbox Code Playgroud)
这是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
}
Run Code Online (Sandbox Code Playgroud)
可以在此链接中查看完整的堆栈跟踪.
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
Run Code Online (Sandbox Code Playgroud)
当我通过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 类有一个名为patienttype 的字段HL7Patient,并且无法使用 HL7USName 类型的值设置该字段。
这意味着您的数据库包含一个 Name,该 Name 具有指向 Name 类型的行而不是 Patient 类型的行的外键。