-->
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.  [ 10 posts ] 
Author Message
 Post subject: Problem with save method
PostPosted: Thu Dec 04, 2003 1:30 pm 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
I have 2 classes User and Login bind by a one-to-one bidirectionnal association.

I retrieve the login's informations and i write something like that :
Code:
Login l = new Login();
l.setId("toto");
l.setPwd("toto");
...
User user = new User();
user.setName("John Doe");
...
user.setLogin(l);
...
session.save(user);


The mapping file is as follow (excerpt):
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="com.cfort.utilisateur.Login" table="login">
       <id name="identifiant" column="identifiant" type="string" length="20">
          <generator class="assigned"/>
       </id>   
       <property name="motPasse" type="string" length="20"/>
       <one-to-one name="internaute" class="com.cfort.utilisateur.Internaute"/>
    </class>
   
    <class name="com.cfort.utilisateur.Adresse" table="adresse">
       <id name="identifiant" column="identifiant" type="string" length="20">
          <generator class="assigned"/>
       </id>   
       <property name="rue" type="string" length="100"/>
       <property name="codePostal" type="string" length="5"/>
       <property name="ville" type="string" length="40"/>
       <property name="email" type="string" length="40"/>
       <property name="telephone" type="string" length="20"/>
       <property name="departement" type="string" length="30"/>
       <property name="region" type="string" length="30"/>
       <property name="fax" type="string" length="20"/>                                          
    </class>
   
   <class name="com.cfort.utilisateur.Internaute" table="internaute" discriminator-value="I">
      <id name="identifiant" column="identifiant" type="string" length="20">
          <generator class="assigned"/>
         </id>
         <discriminator column="type" type="character"/>
         <property name="nbPoints"/>
        <property name="nbPointsAcheter"/>
        <!-- Relation avec la classe Login : bidirectionnelle -->
        <one-to-one name="login" class="com.cfort.utilisateur.Login"/>   
       
        <!-- Relation avec la classe Adresse : monodirectionnelle -->
        <one-to-one name="adresse" class="com.cfort.utilisateur.Adresse"/>   
     </class>   
</hibernate-mapping>


It works but only the user's informations are saved in the DB.
Is it normal or do i have to save the login before.

I believed that when we save an object, hibernate automatically saves all the objects binded to it!!!!!!

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2003 1:39 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
add cascade="save-update" to the one-to-one.
Be sure to set the *same* id in objects linked by a one-to-one.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 4:43 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
I try cascade="save-update" on the user's side. When i run the program, an INSERT is done for the user and an UPDATE for Login. That not solves my problem because the login is not made persistent. What i want is to save the user and the login at same time.
Is it possible with hibernate or do i have to save explicitly the login before?

THANKS


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 4:43 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
I try cascade="save-update" on the user's side. When i run the program, an INSERT is done for the user and an UPDATE for Login. That not solves my problem because the login is not made persistent. What i want is to save the user and the login at same time.
Is it possible with hibernate or do i have to save explicitly the login before?

THANKS


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 5:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Set unsaved-value="any" for the Login class.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 5:16 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
I try this but no effect. An UPDATE is still done for Login.
Is it possible when saving a user to have an INSERT for user and login too.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 5:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You are mistaken. If you set unsaved-value="any" and cascade="save-update", Hibernate will insert a new Login.

Please use your debugger to resolve any further issues you have with this.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 5:30 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
Here is my hbm.xml 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="com.cfort.utilisateur.Login" table="login" unsaved-value="any">
       <id name="identifiant" column="identifiant" type="string" length="20">
          <generator class="assigned"/>
       </id>   
       <property name="motPasse" type="string" length="20"/>
       <one-to-one name="internaute" class="com.cfort.utilisateur.Internaute" />
    </class>
   
     
   <class name="com.cfort.utilisateur.Internaute" table="internaute" discriminator-value="I">
      <id name="identifiant" column="identifiant" type="string" length="20">
          <generator class="assigned"/>
         </id>
         <discriminator column="type" type="character"/>
         <property name="nbPoints"/>
        <property name="nbPointsAcheter"/>
        <!-- Relation avec la classe Login : bidirectionnelle -->
        <one-to-one name="login" class="com.cfort.utilisateur.Login" [b]cascade="save-update" [/b]/>   
       
            </class>   
</hibernate-mapping>


I do as you say!!
what's wrong ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 5:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ummmm, lets see now:


unsaved-value is an attribute of the <id> element, not the <class> element.


Please read documentation and use the Hibernate DTD when proplems arise.

TIA


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 5:50 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
OK. It works well.
Thanks for your patience.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.