Domanda

I Have the following code one-directional one-to-many association:

Class A{

    [Set(0, Lazy = CollectionLazy.False, Cascade = "none")]
    [Key(1, Column = "A_id")]
    [OneToMany(2, ClassType = typeof(B))]
    public virtual ISet<B> Values { get; set; }
}

 Class B{
 }

Then, I have table A and table B, which has a foreign key column called A_ID.

Whenever I merge a fresh instance of entity A, I´m getting a error, saying that column A_ID cannot be null, which means to me that nhibernate is still trying to perform some "sort" of cascading on the B entity, even though I´m explicitly using "none".

The stack is as follows:

could not delete collection: [A.Values#/Global/Rowstamps/User][SQL: UPDATE B SET A_id = null WHERE A_id = ?p0]]
   NHibernate.Persister.Collection.AbstractCollectionPersister.Remove(Object id, ISessionImplementor session) +1057
   NHibernate.Action.CollectionRemoveAction.Execute() +206
   NHibernate.Engine.ActionQueue.Execute(IExecutable executable) +48
   NHibernate.Engine.ActionQueue.ExecuteActions(IList list) +128
   NHibernate.Engine.ActionQueue.ExecuteActions() +65
   NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) +215
   NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) +179
   NHibernate.Impl.SessionImpl.Flush() +293

I´m merging a new instance of A, which has a empty Set of B´s.

the question is why nhibernate is assuming he has to handle that collection, and how to avoid that behaviour?

È stato utile?

Soluzione

Set the Inverse property to true. This will let nhibernate know that it should not handle the collection changes, instead you have to persist changes to any B reference somewhere else.

You can still cascade delete orphans to remove references from A to B if the reference has been removed from Values for example...

Example:

[Set(0, Cascade = "all-delete-orphan", Inverse=true, Lazy = CollectionLazy.False)]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top