Je suis à peu près un newb avec spring-hibernate et j'ai essayé de le faire fonctionner.hibernate - jeu de cartographie de printemps/haricot
J'ai un modèle de données comme ceci:
patient prescription
---------- --------------
patient_id* prescription_id*
first_name patient_id*
last_name date
...
J'utilise des grains de printemps avec le modèle de mise en veille prolongée pour définir ma session/HibernateTemplate et dao comme ceci:
<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>
Pendant un certain temps, je avait seulement patient table et tout allait bien. Mais alors j'ai décidé d'ajouter une prescription qui utilise une clé étrangère de type patient_id. Fondamentalement, j'ai 2 objets modèle POJO comme celui-ci:
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;
}
}
(similaire chose plus simple pour prescription). La chose qui m'embête est le membre privé Set patientPrescriptions. Voici ma cartographie hbm.xml pour ma classe de patients.
<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>
Dès que je référence dans mon fichier de mappage de patient.hbm.xml je reçois une erreur.
org.springframework.beans.factory.BeanCreationException: Erreur de création de haricot avec le nom 'prescriptionDao' défini dans le fichier [C: \ eclipse \ workspace \ smart-gwt \ WebContent \ WEB-INF \ ressources \ hibernate-haricots .xml]: Impossible de résoudre la référence au bean 'hibernateTemplate' lors de la définition de la propriété bean 'hibernateTemplate'; l'exception imbriquée est org.springframework.beans.factory.BeanCreationException: Erreur lors de la création du bean avec le nom 'hibernateTemplate' défini dans le fichier [C: \ eclipse \ espace de travail \ smart-gwt \ Contenu Web \ WEB-INF \ resources \ hibernate-beans.xml] : Impossible de résoudre la référence au bean 'mySessionFactory' lors de la définition de la propriété bean 'sessionFactory'; l'exception imbriquée est org.springframework.beans.factory.BeanCreationException: Erreur lors de la création du bean avec le nom 'mySessionFactory' défini dans le fichier [C: \ eclipse \ espace de travail \ smart-gwt \ Contenu Web \ WEB-INF \ resources \ hibernate-beans.xml] : L'appel de la méthode init a échoué; exception imbriquée est org.hibernate.MappingException: Références Association classe unmapped: Prescription
Pourquoi ai-je l'unmapped erreur pour cet ensemble? Si je le supprime de mon fichier hbm.xml, je peux tout faire avec ma session. Y at-il quelque chose de spécial à faire en demandant l'objet dao? Voici le code que j'utilise pour obtenir mon 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");
Merci
Thx! Le nom de classe complet était mon problème. J'ai passé une journée à chercher une réponse aussi simple. Maintenant ça marche! –