-->
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.  [ 1 post ] 
Author Message
 Post subject: Child FK was set to null when parent is deleted
PostPosted: Tue Feb 09, 2010 11:58 pm 
Newbie

Joined: Tue Feb 09, 2010 11:43 pm
Posts: 1
I have two objects with one-to-many relationship with following mapping definition. Whenever, I delete a CostCentre, all LeasePty that
reference it, will have the CostCentre_id set to null and the CostCentre will be deleted. Whereas, I wish the system give me an exception
to tell me that the CostCentre is being referenced and cannot be deleted when I try to delete the CostCentre.

I tried to change the reference to mapping.References(x => x.CostCentre).WithForeignKey().not.nullable. However, this give me an database error when the transaction is actually committed. Can I get an exception at an earlier stage? Thanks!

public class CostCentreMap : IAutoMappingOverride<CostCentre>
{
public void Override(AutoMap<CostCentre> mapping) {

mapping.Id(x => x.Id)
.WithUnsavedValue(0)
.GeneratedBy.Native();

mapping.HasMany(x => x.LeasePties)
.AsBag();

}
}

public class LeasePtyMap : IAutoMappingOverride<LeasePty> {
public void Override(AutoMap<LeasePty> mapping) {

mapping.Id(x => x.Id, "ID")
.WithUnsavedValue(0)
.GeneratedBy.Native();

mapping.Map(x => x.EngAddr1)
.Not.Nullable()
.WithLengthOf(30);

mapping.Map(x => x.EngAddr2)
.Nullable()
.WithLengthOf(30);

mapping.OptimisticLock.Version();
mapping.DynamicUpdate();
mapping.Version(x => x.Version);

/*-------------------------------------- Key --------------------------------------*/
mapping.References(x => x.CostCentre)
.WithForeignKey();
}

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.