I have a class Utilisateur and class Evenement with a one-to-many association with the following code :
Code:
class Utilisateur
{
//...
Set evenements;
//...
}
class Evenement
{
//...
}
The association is uni-directionnal.
The mapping file is :
<class name="com.cfort.calendrier.Evenement" table="evenement">
<id name="id" column="id">
<generator class="hilo"/>
</id>
<property name="moment" type="string" length="12"/>
<property name="debut" type="string" length="10"/>
<property name="fin" type="string" length="10"/>
<property name="titre" type="string" length="40"/>
<property name="description" type="string" length="200"/>
</class>
<class name="com.cfort.utilisateur.Utilisateur" table="utilisateur" >
<id name="identifiant" column="identifiant" type="string" length="20">
<generator class="assigned"/>
</id>
<property name="dateAbonnement" type="date"/>
<property name="datePaiement" type="date"/>
<property name="modeReglement">
<column name="modeReglement" sql-type="varchar(30)"/>
</property>
<property name="nbPoints" type="integer"/>
<set name="evenements">
<key column="utilisateur_numero"/>
<one-to-many class="com.cfort.calendrier.Evenement"/>
</set> </class>
[/b]
The generating tables by hibernate plugin for eclispe are :
Code:
create table utilisateur(
identifiant varchar(20) PRIMARY KEY not null,
type char(1) not null,
dateAbonnement date,
datePaiement date,
modeReglement varchar(30),
nbPoints int(11),
)
create table evenement(
id int(11) PRIMARY KEY default 0 not null,
moment varchar(12),
debut varchar(10),
fin varchar(10),
titre varchar(40),
description varchar(200),
utilisateur_numero varchar(20))
My question is about how to insert a evenement associated to an existing utilisateur ?
I have wrote the following code :
Code:
public class HibernateCalendrierDAO implements CalendrierDAO {
/* (non-Javadoc)
* @see com.cfort.dao.CalendrierDAO#insererEvenement(com.cfort.calendrier.Evenement)
*/
public void insererEvenement(Evenement evt) {
try
{
Session session = HibernateDAOFactory.creerConnection();
Transaction tx = session.beginTransaction();
session.save(evt);
tx.commit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
As i have no informations about the utilisateur in the Evenement's class,
does it mean that i have to insert a new evenement from the utilisateur's object.
Can someone explainto me how to solve the problem ?
Thanks