java 反射 transient_java – 无法通过反射设置器设置字段值

在使用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);}

}

这是患者实体:

@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

}

完整的堆栈跟踪可以查看at this link.

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命令行客户端运行此查询时,它返回测试数据库表中的唯一记录.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值