Note that there are some explanatory texts on larger screens.

plurals
  1. POhibernate - spring/bean mapping set
    text
    copied!<p>I'm pretty much a newb with spring-hibernate and I have been trying to make it work.</p> <p>I have a data model like this:</p> <pre><code>patient prescription ---------- -------------- patient_id* prescription_id* first_name patient_id* last_name date ... </code></pre> <p>I'm using spring beans with hibernate template to define my session/hibernatetemplate and dao like this:</p> <pre><code> &lt;bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt; &lt;property name="dataSource" ref="myDataSource" /&gt; &lt;property name="mappingResources"&gt; &lt;list&gt; &lt;value&gt;./org/example/smartgwt/shared/model/prescription.hbm.xml&lt;/value&gt; &lt;value&gt;./org/example/smartgwt/shared/model/patient.hbm.xml&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="hibernateProperties"&gt; &lt;value&gt;hibernate.dialect=org.hibernate.dialect.HSQLDialect&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- Wrapper for low-level data accessing and manipulation --&gt; &lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt; &lt;property name="sessionFactory"&gt; &lt;ref bean="mySessionFactory" /&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- --&gt; &lt;bean id="patientDao" class="org.example.smartgwt.server.data.PatientDao"&gt; &lt;property name="hibernateTemplate"&gt; &lt;ref bean="hibernateTemplate" /&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="prescriptionDao" class="org.example.smartgwt.server.data.PrescriptionDao"&gt; &lt;property name="hibernateTemplate"&gt; &lt;ref bean="hibernateTemplate" /&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>For a while I had only <strong>patient</strong> table and all was ok. But then I decided to add a <strong>prescription</strong> which uses a foreign key of type <strong>patient_id</strong>. Basically I have 2 POJO model objects like this:</p> <pre><code>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; } } </code></pre> <p>(Similar simpler thing for Prescription).</p> <p>The thing that is bugging me is the <strong>private Set patientPrescriptions</strong> member. Here is my hbm.xml mapping for my patient class. </p> <pre><code>&lt;hibernate-mapping&gt; &lt;class name="org.example.smartgwt.shared.model.Patient" table="patient" lazy="true"&gt; &lt;id name="patientId" column="patient_id"&gt; &lt;generator class="increment"/&gt; &lt;/id&gt; &lt;property name="firstName"&gt; &lt;column name="first_name"/&gt; &lt;/property&gt; &lt;property name="lastName"&gt; &lt;column name="last_name"/&gt; &lt;/property&gt; &lt;property name="birthDate"&gt; &lt;column name="birth_date"/&gt; &lt;/property&gt; &lt;property name="address"&gt; &lt;column name="address"/&gt; &lt;/property&gt; &lt;property name="phone"&gt; &lt;column name="phone"/&gt; &lt;/property&gt; &lt;set name="patientPrescriptions" cascade="all"&gt; &lt;key column="patient_id"/&gt; &lt;one-to-many class="Prescription"/&gt; &lt;/set&gt; &lt;/class&gt; </code></pre> <p></p> <p>As soon as I reference that in my patient.hbm.xml mapping file I get an error.</p> <blockquote> <p>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</p> </blockquote> <p>Why do I get the <strong>unmapped</strong> error for this Set? If I remove that from my hbm.xml, I can do pretty my everything with my session. Is there something special to do while requesting the dao object? Here is the code I use to get my dao.</p> <pre><code> Resource resource = new FileSystemResource("./hibernate-beans.xml"); BeanFactory factory = new XmlBeanFactory(resource); PrescriptionDao prescriptionDao = (PrescriptionDao)factory.getBean("prescriptionDao"); PatientDao clientDao = (PatientDao) factory.getBean("patientDao"); </code></pre> <p>Thank you</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload