Skip to content

Commit 07f306d

Browse files
committed
NH-3453 - Proposed fix
1 parent a73e4bc commit 07f306d

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/NHibernate/Type/EntityType.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,24 @@ protected internal object GetIdentifier(object value, ISessionImplementor sessio
161161
return value;
162162
}
163163

164-
if (IsReferenceToPrimaryKey)
164+
return ForeignKeys.GetEntityIdentifierIfNotUnsaved(GetAssociatedEntityName(), value, session); //tolerates nulls
165+
}
166+
167+
protected internal object GetReferenceValue(object value, ISessionImplementor session)
168+
{
169+
if (IsNotEmbedded(session))
165170
{
166-
return ForeignKeys.GetEntityIdentifierIfNotUnsaved(GetAssociatedEntityName(), value, session); //tolerates nulls
171+
return value;
167172
}
168-
else if (value == null)
173+
174+
if (value == null)
169175
{
170176
return null;
171177
}
178+
else if (IsReferenceToPrimaryKey)
179+
{
180+
return ForeignKeys.GetEntityIdentifierIfNotUnsaved(GetAssociatedEntityName(), value, session); //tolerates nulls
181+
}
172182
else
173183
{
174184
IEntityPersister entityPersister = session.Factory.GetEntityPersister(GetAssociatedEntityName());
@@ -180,7 +190,7 @@ protected internal object GetIdentifier(object value, ISessionImplementor sessio
180190
IType type = entityPersister.GetPropertyType(uniqueKeyPropertyName);
181191
if (type.IsEntityType)
182192
{
183-
propertyValue = ((EntityType)type).GetIdentifier(propertyValue, session);
193+
propertyValue = ((EntityType)type).GetReferenceValue(propertyValue, session);
184194
}
185195

186196
return propertyValue;
@@ -279,7 +289,7 @@ public override object Replace(object original, object target, ISessionImplement
279289
}
280290
else
281291
{
282-
object id = GetIdentifier(original, session);
292+
object id = GetReferenceValue(original, session);
283293
if (id == null)
284294
{
285295
throw new AssertionFailure("non-transient entity has a null id");

src/NHibernate/Type/ManyToOneType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ public override SqlType[] SqlTypes(IMapping mapping)
4646
public override void NullSafeSet(IDbCommand st, object value, int index, bool[] settable, ISessionImplementor session)
4747
{
4848
GetIdentifierOrUniqueKeyType(session.Factory)
49-
.NullSafeSet(st, GetIdentifier(value, session), index, settable, session);
49+
.NullSafeSet(st, GetReferenceValue(value, session), index, settable, session);
5050
}
5151

5252
public override void NullSafeSet(IDbCommand cmd, object value, int index, ISessionImplementor session)
5353
{
5454
GetIdentifierOrUniqueKeyType(session.Factory)
55-
.NullSafeSet(cmd, GetIdentifier(value, session), index, session);
55+
.NullSafeSet(cmd, GetReferenceValue(value, session), index, session);
5656
}
5757

5858
public override bool IsOneToOne

0 commit comments

Comments
 (0)