NHibernate - Save child - only set key from Parent
I have the fallowing problem: I have to tables SalesHeader and SalesPosition - where SalesPosition is a child of SalesHeader. Now. In table SalesHeader, there is a persistent Entity X. I try now to persist an Entity in SalesPosition with only give this Entity the Key to X (of SalesHeader). NHibernate does not have to save some data in SalesHeader. Now, when I flush the entity to SalesPosition, I become the fallowing exception:
object references an unsaved transient instance - save the transient instance before flushing.
at NHibernate.Engine.ForeignKeys.GetEntityIdentifierIfNotUnsaved(String entityName, Object entity, ISessionImplementor session) at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session) at NHibernate.Type.ManyToOneType.IsDirty(Object old, Object current, Boolean[] checkable, ISessionImplementor session) at NHibernate.Type.TypeFactory.FindDirty(StandardProperty[] properties, Object[] x, Object[] y, Boolean[][] includeColumns, Boolean anyUninitializedProperties, ISessionImplementor session) at NHibernate.Persister.Entity.AbstractEntityPersister.FindDirty(Object[] currentState, Object[] previousState, Object entity, ISessionImplementor session) at NHibernate.Event.Default.DefaultFlushEntityEventListener.DirtyCheck(FlushEntityEvent event) at NHibernate.Event.Default.DefaultFlushEntityEventListener.IsUpdateNecessary(FlushEntityEvent event, Boolean mightBeDirty) at NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent event) at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(FlushEvent event) at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event) at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) at NHibernate.Impl.SessionImpl.Flush() at NHibernate.Transaction.AdoTransaction.Commit()
this is my mapping of SalesPosition:
<class name="SalesPosition" table="SalesPosition" lazy="false" >
<id name="Id" column="Id" type="Guid">
开发者_开发问答 <generator class="assigned"/>
</id>
<version name="ObjectVersion" column="ObjectVersion"/>
.... some fields
<many-to-one name="SalesHeader" class="SalesHeader" foreign-key="FK_SalesHeader_SalesPosition" >
<column name="SalesHeaderId"/>
</many-to-one>
</class>
and this is the mapping of SalesHeader:
<class name="SalesHeader" table="SalesHeader" lazy="false" >
<id name="Id" column="Id" type="Guid">
<generator class="assigned"/>
</id>
<version name="ObjectVersion" column="ObjectVersion"/>
... some fields
<set name="SalesPosition" lazy="true" inverse="true" cascade="delete" >
<key>
<column name="SalesHeaderId"/>
</key>
<one-to-many class="SalesPosition"/>
</set>
</class>
Can anybody give me a tip, what I have to do, that I can only give the key of SalesHeader to the SalesPosition-Entity for persisting (or the SalesHeader-Entity and NHibernate is only using the key).
I use the newest release Version of NHibernate.
Thank you.
Best Regards, Thomas
If I understand your question correctly, you are attempting to save a SalesPosition instance but you want to just set SalesPosition.SalesHeader.Id instead of setting SalesPosition.SalesHeader to an instance of SalesHeader. You can accomplish this using ISession.Load; this posting explains how it works. Basically, if you know that an entity exists in the database, you can use Load to create a proxy for the object rather than getting it from the database. This is very handy in stateless (web) applications. Usage will be:
mySalesPosition.SalesHeader = session.Load<SalesHeader>(salesHeaderId);
session.Flush();
精彩评论