-->
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.  [ 4 posts ] 
Author Message
 Post subject: No cascade insert to one-to-one relation
PostPosted: Sun Jan 09, 2005 6:05 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Hi,

I have two tables:
Subscriber and Profile
Subscriber is a child of User, persisted in a single table.
Each subscriber has max one profile.
I have mapeed the profile as a foreign key association. However, when I create a new subscriber with an emtpy profile, the profile doesn't get inserted.

On another note, why do I need to have two columns in the Profile table(subscriberID and subscriber) when they point to the same value?

Any help's appreciated. It's driving me nuts that I can't achieve this simple thing.

Kind regards,

Marc

Subscriber.java

/**
* @hibernate.one-to-one outer-join="false" unsaved-value="null"
* class="nl.msw.dates4free.business.entities.profile.Profile" cascade="all"
* @return Returns the profile.
*/
public Profile getProfile() {
return profile;
}


Profile.java

/**
* @hibernate.id generator-class="foreign" column="SubscriberID" type="long" unsaved-value="null"
* @hibernate.generator-param name="property" value="subscriber"
* @return Returns the subscriberID.
*/
public long getSubscriberID() {
return subscriberID;
}
/**
* @param subscriberID The subscriberID to set.
*/
public void setSubscriberID(long subscriberID) {
this.subscriberID = subscriberID;
}
/**
* @hibernate.many-to-one class="nl.msw.dates4free.business.entities.Subscriber" unique="true" constrained="true" cascade="all"
* @return Returns the subscriber.
*/
public Subscriber getSubscriber() {
return subscriber;
}



Testcode

Subscriber user = new Subscriber();
user.setEmail(email);
user.setProfile(new Profile());
user.setIdeal(new IdealProfile());

Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.save(sub);
tx.commit();
HibernateUtil.closeSession();


Subscriber.hbm.xml

<class
name="nl.msw.dates4free.business.entities.User"
table="Users"
discriminator-value="U"
>

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

<discriminator
column="Subclass"
type="char"
/>

<one-to-one
name="profile"
class="nl.msw.dates4free.business.entities.profile.Profile"
cascade="all"
outer-join="false"
constrained="false"
/>

...


Profile.hbm.xml

<class
name="nl.msw.dates4free.business.entities.profile.Profile"
table="Profiles"
proxy="nl.msw.dates4free.business.entities.profile.Profile"
>

<id
name="subscriberID"
column="SubscriberID"
type="long"
unsaved-value="null"
>
<generator class="foreign">
<param name="property">subscriber</param>
</generator>
</id>

<many-to-one
name="subscriber"
class="nl.msw.dates4free.business.entities.Subscriber"
cascade="all"
outer-join="auto"
column="subscriber"
unique="true"
/>
...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 10, 2005 11:43 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you must set profile.setSubscriber()

_________________
Emmanuel


Top
 Profile  
 
 Post subject: What if I don't need to navigate back to Subscriber
PostPosted: Mon Jan 10, 2005 12:37 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Hi and thanks for the reply.

What if I don't need to navigate back from the Profile to the Subscriber? The only reason I put that subscriber field there, was to allow a foreign key reference. It would seem so much more logical to be able to specify the foreign key in the one-to-one key in the Subscriber class.

Is there a way not to have to specifify the subscriber in the profile but for the foreign key to be automatically inserted? If not: is there a way not to have two columns with the same information: subscriberID and subscriber (both in the Profile class)?

Kind regards,

marc


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 10, 2005 5:36 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
No, you have to explicit the relationship
use <one-to-one/> it'll avoid extra column

_________________
Emmanuel


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