我收到此异常(底部是完全例外):
NHibernate.PropertyValueException was unhandled by user code
Message="not-null property references a null or transient
valueClearwave.Models.Encounters.Insurance.Patient"
Source="NHibernate"
EntityName="Clearwave.Models.Encounters.Insurance"
PropertyName="Patient"
我做了很多谷歌搜索,这似乎是最常见的原因 该错误是指关联是双向的,但只有一半 已设置。如:Insurance.Patient =患者被召唤但是 Patient.Insurances.Add(保险)不是。事实上,我确实有 这样的场景,但我在调用之前检查了对象 保存它,Insurance.Patient和Patient.Insurances [0]都是 正确的对象。
此异常似乎引用的另一种可能性是a 瞬态值。在我的情况下每个对象都是暂时的,所以我就是 怀疑我问题的根源在这里。然而,一切都需要 现在是短暂的,因为还没有保存。我会 期望NHibernate坚持不懈而不是抱怨它们 不坚持。
以下是我映射的一些片段(流利):
public PatientMap()
{
WithTable("tPatient");
Id(x => x.Id, "uid_Patient").GeneratedBy.GuidComb
().Access.AsReadOnlyPropertyThroughCamelCaseField();
HasMany(x => x.Insurances).WithKeyColumn("uid_Patient")
.Cascade.All()
.Inverse();
...
}
public InsuranceMap()
{
WithTable("tPatientInsuranceInfo");
Id(x => x.Id,
"uid_PatientInsuranceInfo").GeneratedBy.GuidComb
().Access.AsReadOnlyPropertyThroughCamelCaseField();
References(x => x.Patient, "uid_Patient").Not.Nullable
().Cascade.All();
...
}
那么,问题可能是什么?
NHibernate.PropertyValueException was unhandled by user code
Message="not-null property references a null or transient
valueClearwave.Models.Encounters.Insurance.Patient"
Source="NHibernate"
EntityName="Clearwave.Models.Encounters.Insurance"
PropertyName="Patient"
StackTrace:
at NHibernate.Engine.Nullability.CheckNullability(Object[]
values, IEntityPersister persister, Boolean isUpdate)
at
NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate
(Object entity, EntityKey key, IEntityPersister persister, Boolean
useIdentityColumn, Object anything, IEventSource source, Boolean
requiresImmediateIdAccess)
at
NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object
entity, Object id, IEntityPersister persister, Boolean
useIdentityColumn, Object anything, IEventSource source, Boolean
requiresImmediateIdAccess)
at
NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId
(Object entity, String entityName, Object anything, IEventSource
source, Boolean requiresImmediateIdAccess)
at
NHibernate.Event.Default.DefaultMergeEventListener.EntityIsTransient
(MergeEvent event, IDictionary copyCache)
at NHibernate.Event.Default.DefaultMergeEventListener.OnMerge
(MergeEvent event, IDictionary copyCache)
at NHibernate.Impl.SessionImpl.FireSaveOrUpdateCopy(IDictionary
copiedAlready, MergeEvent event)
at NHibernate.Impl.SessionImpl.SaveOrUpdateCopy(String
entityName, Object obj, IDictionary copiedAlready)
at
NHibernate.Engine.CascadingAction.SaveUpdateCopyCascadingAction.Cascade
(IEventSource session, Object child, String entityName, Object
anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeToOne(Object child, IType
type, CascadeStyle style, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeAssociation(Object child,
IType type, CascadeStyle style, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeProperty(Object child,
IType type, CascadeStyle style, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeOn(IEntityPersister
persister, Object parent, Object anything)
at
NHibernate.Event.Default.AbstractSaveEventListener.CascadeBeforeSave
(IEventSource source, IEntityPersister persister, Object entity,
Object anything)
at
NHibernate.Event.Default.DefaultMergeEventListener.EntityIsTransient
(MergeEvent event, IDictionary copyCache)
at NHibernate.Event.Default.DefaultMergeEventListener.OnMerge
(MergeEvent event, IDictionary copyCache)
at NHibernate.Impl.SessionImpl.FireSaveOrUpdateCopy(IDictionary
copiedAlready, MergeEvent event)
at NHibernate.Impl.SessionImpl.SaveOrUpdateCopy(String
entityName, Object obj, IDictionary copiedAlready)
at
NHibernate.Engine.CascadingAction.SaveUpdateCopyCascadingAction.Cascade
(IEventSource session, Object child, String entityName, Object
anything, Boolean isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeToOne(Object child, IType
type, CascadeStyle style, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeAssociation(Object child,
IType type, CascadeStyle style, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeProperty(Object child,
IType type, CascadeStyle style, Object anything, Boolean
isCascadeDeleteEnabled)
at NHibernate.Engine.Cascade.CascadeOn(IEntityPersister
persister, Object parent, Object anything)
at
NHibernate.Event.Default.AbstractSaveEventListener.CascadeBeforeSave
(IEventSource source, IEntityPersister persister, Object entity,
Object anything)
at
NHibernate.Event.Default.DefaultMergeEventListener.EntityIsTransient
(MergeEvent event, IDictionary copyCache)
at NHibernate.Event.Default.DefaultMergeEventListener.OnMerge
(MergeEvent event, IDictionary copyCache)
at NHibernate.Event.Default.DefaultMergeEventListener.OnMerge
(MergeEvent event)
at NHibernate.Impl.SessionImpl.FireSaveOrUpdateCopy(MergeEvent
event)
at NHibernate.Impl.SessionImpl.SaveOrUpdateCopy(Object obj)
at Clearwave.Models.Data.Util.RepositoryBase`2.Save(EntityType&
entity) in C:\Projects\ClearWave\Src\Common\Domain Models
\Clearwave.Models.Data-NHibernate\Util\RepositoryBase.cs:line 25
at IntegrationWebServices.FromMirth.SubmitMessage(Message
theMessage, Guid providerOrganizationId)
at SyncInvokeSubmitMessage(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke
(Object instance, Object[] inputs, Object[]& outputs)
at
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin
(MessageRpc& rpc)
InnerException:
答案 0 :(得分:18)
另一种可能性是您正在保存整个对象图,并且该图是循环的,并且项不能为空。您可能会给NHibernate没有合法的订单来执行插入操作。 (它应该产生一个更好的错误信息,但它产生了这个)。
如果没有看到其他来源,很难获得更多帮助。尝试从映射中删除实体(而不是保存它们),直到找出导致问题的原因为止。
答案 1 :(得分:9)
不确定它是否有帮助,但这对我有用。
<many-to-one name="Company" column="CompanyId" cascade="all" not-null="true"/>
cascade =“all”是我之前错过的
答案 2 :(得分:8)
我最近遇到了这个问题,它与NHibernate双向关系的持久化方式有关。您的映射是正确的,因此NHibernate将执行患者插入没有问题。然后NHibernate需要从患者那里获取密钥并将其级联到保险中。由于患者尚不存在,因此密钥不存在且无法执行第二次插入。关键是在持久化之前通过代码设置关系,如下所示:
patient = new Patient();
patient.Insurances.Add( new Insurance{ Patient = patient } );
repository.Save( patient);
现在我不得不在集合项上设置Patient属性,但如果你忽略持久性,你将在代码中设置它,而不依赖于持久性策略。
答案 3 :(得分:1)
这对我有用。这里重要的是我们References
与Cascade.All()
Inverse()
HasMany
public PatientMap()
{
HasMany(x => x.Insurances)
.WithKeyColumn("uid_Patient")
.Cascade.All();
...
}
public InsuranceMap()
{
References(x => x.Patient, "uid_Patient")
.Not.Nullable()
.Cascade.All();
...
}
let xhr = new XMLHttpRequest()
function *statechange() {
yield xhr.readyState;
}
let gen = statechange();
xhr.open("GET", myUrl, true);
xhr.onreadystatechange = function() {console.log(gen.next())};
xhr.send();
答案 4 :(得分:0)
看起来异常发生在RepositoryBase.cs文件的第25行,可能是在你的一个瞬态对象上调用了Save()。哪一个被保存?
另外,它可能是不相关的,因为我不熟悉Fluent语法,如果子对象(在这种情况下是保险)上有.Cascade.All吗?在标准XML模式语法中,只有父映射在子对象集合上具有cascade =“all”。