-->
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: Bug? Subclass one-to-one relation not persisted
PostPosted: Tue Jan 18, 2005 12:38 pm 
Pro
Pro

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

I have two classes:
* Subscriber, child of User
* Profile, related to Subscriber, one per subscriber

I want to automatically save Profile, when I persist Subscriber. What happens is that Subscriber gets persisted. Hibernate tries to update Profile in stead of insert it, resulting in an error.

Any ideas?

I have mapped the relation like so:

User.hbm.xml
[codeb]
<class
name="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"
/>

<subclass
name="Subscriber"
discriminator-value="S"
>
[/code]

<one-to-one
name="profile"
class="Profile"
cascade="all"
outer-join="auto"
constrained="false"
property-ref="subscriber"
/>


Profile.hbm.xml
Code:
<class
name="Profile"
table="Profiles"
proxy="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="Subscriber"
cascade="none"
outer-join="auto"
constrained="false"
unique="true"
not-null="false"
column="SubscriberID"
/>



Kind regards,

Marc


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 1:47 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Sounds to me like you've got your unsaved value set wrong. Hibernate needs to know what the unsaved value of your primary key and/or version is. For example, if you use a "long" primary key, it needs to know if -1 or zero or whatever is the "unsaved value" of the object.

Does your class use a primitive "long" or an object "Long" to represent the key? I recommend an object since NULL is a great value to represent the unsaved value of your object.

The other problem you've got is you're using one-to-one, which in most cases requires that the primary key of both tables be the same for the related records. Is that how you've mapped it? Often it is much easier to model one-to-one as a component or as a many-to-one. One-to-one borders on being an "exotic" case that is often more easily mapped another way. I suspect in your case that since you've modelled it as a one-to-one, hibernate has no way to know that your Profile object is new and thus, it's trying to update rather than insert.

Also, FYI, you won't get a very warm response in this forum my immediately jumping to the conclusion that a Hibernate bug is causing your problem. Almost all problems you'll run into are caused by mis-configuration and/or not doing enough reading and experimentation. If you want responses from the Hibernate team, don't include the word BUG in your post unless you have spent hours isolating the bug down into a simple repeatable test case.

In the mean time, please turn on Hibernate logging. It will explicitly tell you exactly why your object is not saving and will teach you a great deal about what Hibernate is doing.


Top
 Profile  
 
 Post subject: Thanks for the reply
PostPosted: Tue Jan 18, 2005 5:16 pm 
Pro
Pro

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

I meant no offense. I realize Hibernate is the result of a lot of free time donations by talented individuals. Us, less spectacularly gifted however, need the books and the forum! I got a bit frustrated after weeks of posting this question in one form or another on the forum without getting a real response. Somebody even responded with an offer of commercial support. You're the first real response.

I can offer no more apologies except by saying: at least I bought the book :-)

Hooray! It worked!. Thanks a lot!

Kind regards,

Marc


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 5:28 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Glad it worked. Also glad you picked up the book. It rocks.

Also, didn't mean to put you off with the comment about the bug. I've just personally experienced the wrath and/or total lack of responses when I use the word "bug" and wanted to pass along the tip!

As for the forum and getting responses, you know what they say about free advice! lol.


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.