Hello, I have a problem with a mapping.
My (simplified) domain model is as follows (C# code):
Code:
public abstract class Entity<TEntity, TId>
where TEntity : Entity<TEntity, TId>
{
public virtual TId Id { get; protected set; }
public override bool Equals(object obj)...
...
}
public class EntityA<EntityA, long> : Entity<EntityA, long>
{
public virtual EntityB B { get; private set; }
/* ... */
}
public class EntityB<EntityA, long> : Entity<EntityB, long>
{
/* ... */
}
Here I have a one-to-one relationship between EntityA and EntityB, ie, each EntityA must contain exactly one EntityB. I cannot just "copy" the fields of EntityB into EntityA. They must be 2 separate entites, and in the database they must be 2 separate tables.
So, I did the following mapping (using the Fluent NHibernate interface):
Code:
public class EntityAMap : ClassMap<EntityA>
{
public EntityAMap()
{
Id(x => x.Id);
HasOne(x => x.B)
.Cascade.All();
/* ... */
}
}
public class EntityBMap : ClassMap<EntityB>
{
public EntityBMap()
{
Id(x => x.Id)
.GeneratedBy.Foreign("Id");
/* ... */
}
}
Then I create a valid entityA, ready to be saved:
Code:
var entityA = EntityAFactory.CreateNewValidEntityA();
session.SaveOrUpdate(entityA);
The problem is that
NHibernate throws an exception: "Unable to resolve property: Id".
On my log (and DB) I can see that NHibernate correctly "inserted" entityA into the database, and by debuging the code I can see that it assigned the correct Id to my entityA.
Furthermore, from the stack trace, I can see that this exception was thrown when NHibernate was trying to save the "cascaded" EntityB:
Code:
at NHibernate.Tuple.Entity.EntityMetamodel.GetPropertyIndex(String propertyName)
at NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetPropertyValue(Object entity, String propertyPath)
at NHibernate.Persister.Entity.AbstractEntityPersister.GetPropertyValue(Object obj, String propertyName, EntityMode entityMode)
at NHibernate.Id.ForeignGenerator.Generate(ISessionImplementor sessionImplementor, Object obj)
...
at NHibernate.Impl.SessionImpl.SaveOrUpdate(String entityName, Object obj)
at NHibernate.Engine.CascadingAction.SaveUpdateCascadingAction.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.Impl.SessionImpl.SaveOrUpdate(Object obj)
at myproject...
It looks like NHibernate is having trouble to determine the foreign Id.
How can I solve this problem?
Thanks!
EDITI think I was misunderstanding the "GeneratedBy.Foreign". I created a property on my EntityB pointing back to my EntityA, and said that EntityB's Id is generated by EntityB.A's Id (GeneratedBy.Foreign("A")). However I still have no success. The same exception now says that NHibernate cannot access the "A" property.