-->
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: A simple many-to-one association stumps me!
PostPosted: Thu Nov 03, 2005 7:31 pm 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
Hibernate version: 3

Mapping documents:
Code:
<hibernate-mapping>
    <class name="UserPermissions">
       <id name="id" column="userpermissions_id">
                  <generator class="native"/>
        </id>         
   <!-- Device -->
        <many-to-one name="device" column="device_id"/>

   <!-- UserGroup -->
        <many-to-one name="group" column="usergroup_id"/>

   <!-- User -->
        <many-to-one name="user" column="user_id"/>
    </class>
</hibernate-mapping>


If one of the device, or usergroup, or user from the above mapping is null, saving UserPermissions gives me "TransientObjectException: object references an unsaved transient instance". Is it possible to have a null foreign key?

Thanks in advance,
Liem[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 11:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Check the documentation regarding cascading. That error has nothing to do with a <many-to-one/> reference being null...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 12:50 pm 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
steve wrote:
Check the documentation regarding cascading. That error has nothing to do with a <many-to-one/> reference being null...


Hi Steve,

Thanks for replying. But, unless I am smoking something, I notice that Hibernate puts an fk constraint on the foreign key device_id; so, of course, when I try to save a UserPermission record with a null Device, it gives me the mentioned error, correct?

Liem


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 1:08 pm 
Beginner
Beginner

Joined: Wed Jul 13, 2005 2:18 pm
Posts: 44
liem wrote:
so, of course, when I try to save a UserPermission record with a null Device, it gives me the mentioned error, correct?

Liem


Nope - that exception is a result of sticking a transient object into a set on a persistent object without calling session.save() on the transient object or defining a cascade behavior on the object containing the set. Read about the cascade attribute, as suggested. Setting it correctly should fix things.

_________________
- Matt


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 2:21 pm 
Beginner
Beginner

Joined: Tue Jun 14, 2005 12:14 pm
Posts: 37
Yup, I am an idiot! Figured one cascade that is the culprit. Thanks.

Liem


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.