Nhibernate: One-To-Many mapping problem - Cannot cascade delete without inverse. Set NULL error
I have the current scenario whereby an Article has only 1 Outcome each. Each Article may or may not have an Outcome.
In theory, this is a one-to-one mapping, but since NHibernate does not really support one-to-one, I used a One-To-Many to substitute. My Primary Key on the child table is actually the ArticleID (FK).
So I have the following setup:
Classes
public class Article
{
public virtual Int32 ID { get;set;}
private ICollection<ArticleOutcome> _Outcomes {get;set;}
public virtual ArticleOutcome Outcome
{
get {
if( this._Outcomes !=null && this._Outcomes.Count > 0 )
return this._Outcomes.First();
return null;
}
set {
if( value == null ) {
if( this._Outcomes !=null && this._Outcomes.Count > 0 )
this._Outcomes.Clear();
}
else {
if( this._Outcomes == null )
this._Outcomes = new HashSet<ArticleOutcome>();
else if ( this._Outcomes.Count >= 1 )
this._Outcomes.Clear();
this._Outcomes.Add( value );
}
}
}
}
public class ArticleOutcome
{
public virtual Int32 ID { get;set; }
public virtual Article ParentArticle { get;set;}
}
Mappings
public class ArticleMap : ClassMap<Article>
{
public ArticleMap() {
this.Id( x=> x.ID ).GeneratedBy.Identity();
this.HasMany<ArticleOutcome>( Reveal.Propert开发者_如何学Goy<Article>("_Outcomes") )
.AsSet().KeyColumn("ArticleID")
.Cascade.AllDeleteOrphan() //Cascade.All() doesn't work too.
.LazyLoad()
.Fetch.Select();
}
}
public class ArticleOutcomeMap : ClassMap<ArticleOutcome>
{
public ArticleOutcomeMap(){
this.Id( x=> x.ID, "ArticleID").GeneratedBy.Foreign("ParentArticle");
this.HasOne( x=> x.ParentArticle ).Constrained ();
//This do not work also.
//this.References( x=> x.ParentArticle, "ArticleID" ).Not.Nullable();
}
}
Now my problem is this:
It works when I do an insert/update of the Outcome. e.g.
var article = new Article();
article.Outcome = new ArticleOutcome { xxx = "something" };
session.Save( article );
However, I encounter SQL errors when attempting to delete via the Article itself.
var article = session.Get( 123 );
session.Delete( article ); //throws SQL error.
The error is something to the like of Cannot insert NULL into ArticleID in ArticleOutcome table.
The deletion works if I place Inverse() to the Article's HasMany() mapping, but insertion will fail.
Does anyone have a solution for this? Or do I really have to add a surrogate key to the ArticleOutcome table?
Solution
Here's the mapping for Fluent if anyone is interested.
public class ArticleMap : ClassMap<Article>
{
public ArticleMap() {
this.Id( x=> x.ID ).GeneratedBy.Identity();
this.HasOne( x=> x.Outcome ).Cascade.All();
}
}
public class Article
{
//... other properties
public virtual ArticleOutcome Outcome { get;set;}
}
NHibernate does support one-to-one.
I have a case very similar to yours. Here are the relevant parts:
class Articule
{
public virtual ArticleOutcome Outcome { get; set; }
}
class ArticuleOutcome
{
public virtual Article ParentArticle { get; set; }
}
Mapping (sorry, I don't use Fluent, but it shouldn't be hard to translate):
<class name="Article">
<id name="ID">
<generator class="identity"/>
</id>
<one-to-one name="Outcome" cascade="all"/>
</class/>
<class name="ArticleOutcome">
<id name="ID" column="ArticleId">
<generator class="foreign">
<param name="property">ParentArticle</param>
</generator>
</id>
<one-to-one name="ParentArticle" constrained="true"/>
</class/>
Usage:
var article = new Article();
article.Outcome = new ArticleOutcome
{
xxx = "something",
ParentArticle = article
};
session.Save(article);
Delete should work as you are using it now.
精彩评论