我几乎是 spring-hibernate 的新手,我一直在努力让它发挥作用。
我有一个这样的数据模型:
patient prescription
---------- --------------
patient_id* prescription_id*
first_name patient_id*
last_name date
...
我正在使用带有 hibernate 模板的 spring bean 来定义我的 session /hibernatetemplate 和 dao,如下所示:
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingResources">
<list>
<value>./org/example/smartgwt/shared/model/prescription.hbm.xml</value>
<value>./org/example/smartgwt/shared/model/patient.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
</property>
</bean>
<!-- Wrapper for low-level data accessing and manipulation -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
<!-- -->
<bean id="patientDao" class="org.example.smartgwt.server.data.PatientDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
<bean id="prescriptionDao" class="org.example.smartgwt.server.data.PrescriptionDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
有一段时间我只有patient表,一切正常。但后来我决定添加一个处方,它使用类型为patient_id 的外键。基本上我有 2 个这样的 POJO 模型对象:
public class Patient implements Serializable {
///////////////////////////////////////////////////////////////////////////
// Data members
///////////////////////////////////////////////////////////////////////////
private static final long serialVersionUID = 1L;
private int patientId;
private String firstName;
private String lastName;
private Date birthDate;
private String address;
private String phone;
private Set patientPrescriptions;
public Patient() {}
public void setPatientId(int patientId) {
this.patientId = patientId;
}
public int getPatientId() {
return patientId;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Date getBirthDate() {
return birthDate;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
public void setPatientPrescriptions(Set patientPrescriptions) {
this.patientPrescriptions = patientPrescriptions;
}
public Set getPatientPrescriptions() {
return patientPrescriptions;
}
}
(类似更简单的处方)。
困扰我的是 private Set patientPrescriptions 成员。这是我的患者类别的 hbm.xml 映射。
<hibernate-mapping>
<class name="org.example.smartgwt.shared.model.Patient" table="patient" lazy="true">
<id name="patientId" column="patient_id">
<generator class="increment"/>
</id>
<property name="firstName">
<column name="first_name"/>
</property>
<property name="lastName">
<column name="last_name"/>
</property>
<property name="birthDate">
<column name="birth_date"/>
</property>
<property name="address">
<column name="address"/>
</property>
<property name="phone">
<column name="phone"/>
</property>
<set name="patientPrescriptions" cascade="all">
<key column="patient_id"/>
<one-to-many class="Prescription"/>
</set>
</class>
只要我在我的 patient.hbm.xml 映射文件中引用它,我就会收到错误。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'prescriptionDao' defined in file [C:\eclipse\workspace\smart-gwt\WebContent\WEB-INF\resources\hibernate-beans.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in file [C:\eclipse\workspace\smart-gwt\WebContent\WEB-INF\resources\hibernate-beans.xml]: Cannot resolve reference to bean 'mySessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in file [C:\eclipse\workspace\smart-gwt\WebContent\WEB-INF\resources\hibernate-beans.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Association references unmapped class: Prescription
为什么我会收到此 Set 的unmapped 错误?如果我从我的 hbm.xml 中删除它,我就可以在我的 session 中完成我的所有工作。请求 dao 对象时有什么特别要做的吗?这是我用来获取 dao 的代码。
Resource resource = new FileSystemResource("./hibernate-beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
PrescriptionDao prescriptionDao = (PrescriptionDao)factory.getBean("prescriptionDao");
PatientDao clientDao = (PatientDao) factory.getBean("patientDao");
谢谢
最佳答案
显而易见的答案是您的 Hibernate 映射没有提及 Prescription
类,因此 Hibernate 不知道如何处理它。
假设情况并非如此,而您只是没有向我们展示映射,那么下一个最明显的答案是:
<one-to-many class="Prescription"/>
class
属性应该是一个完全限定的类名,而 Prescription
不是。也许它应该是这样的:
<one-to-many class="org.example.smartgwt.shared.model.Prescription"/>
关于java - hibernate - spring/bean 映射集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3280829/