2010-07-19 83 views
1

我幾乎是一個spring-hibernate的新手,我一直在努力使它工作。冬眠 - 彈簧/ bean映射集

我有這樣一個數據模型:

patient    prescription 
----------    -------------- 
patient_id*   prescription_id* 
first_name    patient_id* 
last_name    date 
... 

我使用的春天豆休眠模板來定義我的會議/的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_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; 
} 
} 

(類似的處方更簡單的事情)。

糾纏我的事情是私人設置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:錯誤創建具有名稱豆 'prescriptionDao' 在文件[C定義爲:\蝕\工作空間\智能GWT \的WebContent \ WEB-INF \資源\冬眠-豆.xml]:設置bean屬性'hibernateTemplate'時無法解析對bean'hibernateTemplate'的引用;嵌套的異常是org.springframework.beans.factory.BeanCreationException:在文件[C:\ eclipse \ workspace \ smart-gwt \ WebContent \ WEB-INF \ resources \ hibernate-beans.xml]中定義名爲'hibernateTemplate' :設置bean屬性'sessionFactory'時無法解析對bean'mySessionFactory'的引用;嵌套異常是org.springframework.beans.factory.BeanCreationException:在文件[C:\ eclipse \ workspace \ smart-gwt \ WebContent \ WEB-INF \ resources \ hibernate-beans.xml]中定義名稱爲'mySessionFactory' :調用init方法失敗;嵌套的例外是org.hibernate.MappingException:協會引用未映射類:處方

爲什麼我得到未映射誤差爲這套?如果我從我的hbm.xml中刪除它,我可以在會話中完成我的一切。請求道對象時有什麼特別的事嗎?這是我用來獲取我的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"); 

謝謝

回答

4

那麼明顯的答案是,你的Hibernate映射隻字不提Prescription類的,所以Hibernate不知道該怎麼辦。

假設並非如此,那不是你只是沒有告訴我們的映射,那麼下一個最明顯的答案是這樣的:

<one-to-many class="Prescription"/> 

class屬性應該是一個完全限定類名,其中Prescription不是。也許它應該是這樣的:

<one-to-many class="org.example.smartgwt.shared.model.Prescription"/> 
+0

Thx!完全合格的課程名稱是我的問題。花了一天時間尋找這樣一個簡單的答案。現在它工作了! – 2010-07-19 12:50:25