【发布时间】:2011-03-17 21:17:16
【问题描述】:
我几乎是 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>
有一段时间我只有 耐心 桌子,一切都很好。但后来我决定添加一个 prescription,它使用 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:创建文件 [C:\eclipse\workspace\smart-gwt\WebContent\WEB-INF\resources\hibernate-beans.xml] 中定义的名称为“prescriptionDao”的 bean 时出错: 在设置 bean 属性“hibernateTemplate”时无法解析对 bean“hibernateTemplate”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建文件 [C:\eclipse\workspace\smart-gwt\WebContent\WEB-INF\resources\hibernate-beans.xml] 中定义的名称为“hibernateTemplate”的 bean 时出错: 在设置 bean 属性 'sessionFactory' 时无法解析对 bean 'mySessionFactory' 的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException: Error created bean with name 'mySessionFactory' defined in file [C:\eclipse\workspace\smart-gwt\WebContent\WEB-INF\resources\hibernate-beans.xml] : init 方法调用失败;嵌套异常是 org.hibernate.MappingException: Association references unmapped class: Prescription
为什么我会收到此 Set 的 unmapped 错误?如果我从我的 hbm.xml 中删除它,我可以用我的会话完成我的一切。请求 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");
谢谢
【问题讨论】:
标签: java hibernate spring mapping