-->
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: Help with simple modelling confusion.
PostPosted: Tue Jun 28, 2005 1:20 pm 
Newbie

Joined: Thu Sep 30, 2004 9:21 am
Posts: 3
Hi,

I think the problem I have has to do with how I have modelled my classes. So can someone please straighten me out on my thinking on this?

I have two Classes. One is a Job class and the other is a Skill class.

A job has one skill related to it. The mapping for the Job class has a many-to-one association to the Skill class -- in the database, I allow the foreign-key reference from the Job table to the Skill table to be null. The mapping for the Skill class has no association specified to the Job class.

In my model, I create several Skills and Jobs and persist them. I set a Skill for a Job and persist them. Later, if I delete the Skill that was set for the Job and then try and to retrieve the Job I get the following error:

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [uk.co.smg.pmat.model.Skill#606]

This is because the foreign key in the Job table has not been set to null to reflect the fact that the Skill to which it was associated has now been deleted. It contains the id of the Skill (now deleted) that was originally set for it. It has not been updated.

Basically, I want to be able to delete Skills and have the references in the Jobs that were associated to them to be automatically set to null -- because it's alright for my Jobs to have no skill associated with them.

From a modelling viewpoint, how do I obtain this?

Thanks,

Frank.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 2:32 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
The reason you are getting that exception is from a problem with your database. You don't actually have a foreign key from job to skill in the database, otherwise you wouldn't have been able to delete the skill with jobs still referencing it.

If you actually did have a foreign key constraint, when deleting a skill, you would first set all the references to that skill to null in the jobs table. This is where it would be useful to have the inverse side of the association defined in the skill table (i.e. <set><one-to-many>). When deleting a skill, you can retrieve all the jobs referencing that skill and set the ref to null.

Hibernate also provides the attribute not-found="ignore" in many-to-one to handle the case where you don't have a FK constraint, automatically, I think. I haven't used it, but it seems to be what you are looking for.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 2:46 pm 
Newbie

Joined: Thu Sep 30, 2004 9:21 am
Posts: 3
nathanmoon wrote:
... you would first set all the references to that skill to null in the jobs table. This is where it would be useful to have the inverse side of the association defined in the skill table (i.e. <set><one-to-many>). When deleting a skill, you can retrieve all the jobs referencing that skill and set the ref to null.


So you're saying that Skill should keep a collection of all Jobs that reference it and then, before deleting that Skill, I iterate over its collection of Jobs, setting their Skill references to null and only THEN I would delete Skill?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 2:56 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
If you want to have a FK constraint - yes. (That's always my preference.) But if not, then try the not-found="ignore" option.


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.