В Spring MVC приложений с использованием Hibernate и MySQL, я получаю ошибку, которая, кажется, указывают, что Name
предприятие не может найти сеттера для id
имущества BaseEntity
суперкласс Patient
организация.не может установить значение поля путем отражения сеттер
Как я могу устранить эту ошибку?
Вот сообщение об ошибке:
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
, который суперкласс как 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<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
}
Полный трассировки стека можно посмотреть at this link.
SQL, порожденный Hibernate для приведенного выше запроса:
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, он возвращает только запись в таблице тестовой базы данных.
Как выглядит полная трассировка стека? Для этого конкретного сообщения об ошибке, как правило, более точная гранулированная ошибка в нижней половине трассировки стека. –
@isim У меня есть аналогичный вопрос. Вы готовы помочь мне с этим? Вот ссылка: http://stackoverflow.com/questions/25316761/duplicate-entry-string1-string2-for-key-primary – CodeMed