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.  [ 3 posts ] 
Author Message
 Post subject: Many-to-one & one-to-many deleting of object issue.
PostPosted: Tue Jan 08, 2008 1:34 pm 
Newbie

Joined: Fri Nov 16, 2007 6:20 pm
Posts: 15
I have three objects: Staff, Project, and ProjectTeam. Project team looks like so:

Code:
CREATE TABLE [dbo].[ut_ProjTeam](
   [ptKey] [bigint] IDENTITY(1,1) NOT NULL,
   [ptProjKey] [int] NOT NULL,
   [ptStKey] [smallint] NULL,
   [ptStRole] [smallint] NOT NULL
)


Essentially this table is part of a many-to-many relationship between the project (ptProjKey) and the staff member (ptStKey).

I've created this table a a separate business object so I can get to the ptStRole field. I can add items just fine it is when I delete that things become problematic. Either I get :
Quote:
deleted object would be re-saved by cascade

Or
Quote:
Cannot insert the value NULL into column 'ptProjKey',


I'm trying to remove the reference to the ProjectTeam entry from the Project and Staff objects BEFORE I do the delete, but this doesn't seem to work. It appears that when I try to remove the reference with a call like this:
Code:
projTeamToRemove.Project.RemoveFromProjectTeam(projTeamToRemove);

the database tries to do an UPDATE first, which of course will fail since the foreign keys can't be null.

Solutions? Work-around? Thoughts?

Thanks
Jack


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 08, 2008 7:08 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
What you infact want to do is remove an member of the staff from the project, correct?

If you skip the databasethinking for a while, this would in an OO-way-of-thinking be done like this:

myProjectInstance.Members.Remove(MyStaffMemberInstance);

And thats also one way to go at it from nHibernate, delete the item from the collection and persist the object again, this could be done something like this:

Project p = sess.Get<Project>(identifier);
StaffMember sm = sess.Get<StaffMember>(identifier);
p.Members.Remove(sm);
sess.SaveOrUpdate(p);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 6:56 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
In fact, due to transparent persistence you don't even need the SaveOrUpdate call - the changes will be persisted into the database on the next flush and committed with your transaction.

Cheers,

Symon.


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