-->
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.  [ 5 posts ] 
Author Message
 Post subject: One-to-One cascade question
PostPosted: Wed Jan 14, 2004 11:25 pm 
Beginner
Beginner

Joined: Tue Dec 30, 2003 1:51 pm
Posts: 49
Location: Michigan, USA
i have a userbean that has a 1-to-1 mapping with an addressbean and a profilebean. here is the hbm.xml

Code:
<hibernate-mapping>
    <class name="omni.user.UserBean" table="users"
     dynamic-update="false" dynamic-insert="false">

        <id name="id" column="id" type="long" unsaved-value="-1" >
            <generator class="native">
            </generator>
        </id>

        <property name="email" type="java.lang.String" update="true"
            insert="true" column="email" not-null="true" unique="true" />

        <property name="password" type="java.lang.String" update="true"
            insert="true" column="password" not-null="true" unique="false"
        />

        <one-to-one name="address" class="omni.user.AddressBean"
            cascade="all" outer-join="auto" constrained="true" />

        <one-to-one name="profile" class="omni.user.ProfileBean"
            cascade="all" outer-join="auto" constrained="true" />

    </class>
</hibernate-mapping>


what i am trying to understand is if i make a call like

Code:
UserBean user = new UserBean();
user.setEmail(testEmail1);
user.setPassword("hello");
user.getProfile().setRole("user");
user.getAddress().setCity("detroit");
...
Session s = openSession();
s.save(user);
...


shouldn't hibernate cascade the saves/updates to the addressbean and profilebean table?

thanks,

jason


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 3:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes.
The constrained="true" should be on the assigned or the foreign side not on the class using a native generator. It means Hibernate will delegate id assignment to the other side.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 1:47 pm 
Beginner
Beginner

Joined: Tue Dec 30, 2003 1:51 pm
Posts: 49
Location: Michigan, USA
okay, i think i understand. so for the address and profile bean i need to be using a generator class of type foreign, correct?
which would allow the userbean to cascade its id number down to the address and profile id tables.
i should also remove the constraint from my userbean, do i need to have it in the profile or address beans since they are not bi-directional?

thanks for the help


--jason


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 2:38 pm 
Beginner
Beginner

Joined: Tue Dec 30, 2003 1:51 pm
Posts: 49
Location: Michigan, USA
having looked at this, perhaps a component mapping would be better?

jason


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 3:42 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
jpratt wrote:
having looked at this, perhaps a component mapping would be better?

If you can, prefer components, there is no lifecycle headache.

_________________
Emmanuel


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