Hello,
Today is my first day on Hibernate. I try to build a prototype application with some advanced features of Hibernate.
One of them is the cascading insert: My main object EMPLOYE owns a SET of SALAIRE (Salaries, in english). When I create a new employee with new salaries, Hibernate does the following:
- insert the new Employee : RIGHT !
- update the salaries : WRONG !
I have set the attribute "cascade=all" on the set Element, but the problem persists.
I may not set the right parameters in my mapping file, please HELP !
Thx,
Yann
-----------------------------------------
Here is my java code:
Code:
Session session = new Configuration().configure().buildSessionFactory().openSession();
Employe employe = new Employe();
employe.setNom("Welcome");
employe.setPrenom("John");
Set sals = new HashSet();
Periode p1 = (Periode) (session.find("from Periode where periode = ?","1999",Hibernate.STRING)).iterator().next();
Periode p2 = (Periode) (session.find("from Periode where periode = ?","1999",Hibernate.STRING)).iterator().next();
sals.add(new Salaire(120,employe,p1));
sals.add(new Salaire(110,employe,p2));
employe.setSalaires(sals);
session.save(employe);
session.flush();
session.connection().commit();
Here is my hbm file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Periode" table="periode">
<id name="id" column="periode_id" type="long">
<generator class="sequence">
<param name="sequence">periode_id_seq</param>
</generator>
</id>
<property name="nom" column="periode" type="string"/>
</class>
<class name="Adresse" table="Adresse">
<id name="id" column="emp_id" type="long">
<generator class="native"/>
</id>
<property name="adresse" type="string"/>
<property name="codePostal" column="code_postal " type="string"/>
<property name="ville" type="string"/>
</class>
<class name="Employe" table="employe">
<id name="id" column="emp_id" type="long">
<generator class="sequence">
<param name="sequence">employe_id_seq</param>
</generator>
</id>
<property name="nom" type="string"/>
<property name="prenom" type="string"/>
<one-to-one name="adresse" class="Adresse" cascade="all" />
<set name="salaires" cascade="all">
<key column="emp_id"/>
<one-to-many class="Salaire" />
</set>
</class>
<class name="Salaire" table="Salaire">
<composite-id>
<key-many-to-one name="employe" column="emp_id" class="Employe"/>
<key-many-to-one name="periode" column="periode_id" class="Periode"/>
</composite-id>
<property name="salaire" type="long"/>
</class>
</hibernate-mapping>