-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem of insertion with <one-to-many> association
PostPosted: Thu Nov 20, 2003 6:17 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 6:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Load the existing object, add the new object to its collection and either

(a) map the <set> with cascade="save-update" OR
(b) save() the new object explicitly

then commit() the txn.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.