Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 399923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:53:38+00:00 2026-05-12T16:53:38+00:00

I have a problem mapping an Id. The structures of the entities are as

  • 0

I have a problem mapping an Id. The structures of the entities are as follows:

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>
{
    /* ... */
}

In my model, every EntityA must contain exactly one EntityB, and every EntityB that exists is there to be part of an EntityA. It’s a common one-to-one relationship.

Now, to the mappings:

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 an EntityA, that creates by itself an EntityB. Then when I save it

var entityA = EntityAFactory.CreateNewValidEntityA();
session.SaveOrUpdate(entityA);

NHibernate throws an exception, “Unable to resolve property: Id”.

However, my log shows that an EntityA was “inserted” into the DB, and by debugging, I can see that EntityA.Id was attributed a value (ie, nhibernate did a good job saving entityA, retrieving the Id generated by the DB and setting the entityA.Id property accordingly).

However, no entityB was created (empty database, and log shows nothing). So, it looks to me that NHibernate had problems to access this property, when saving EntityB, through the “GeneratedBy.Foreign(“Id”)” definition. Maybe it’s because the property “Id” is not directly a property of EntityA, but from EntityBase, however what I did looks correct to me.

Where is the problem? How can I solve it?

Thanks!

EDIT:
Here I show some of the stack trace, if it might help.
As you can see, it did Cascade, and it did SaveOrUpdate on the other entity.

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...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:53:38+00:00Added an answer on May 12, 2026 at 4:53 pm

    The Foreign function takes a class rather than a property.

    Firstly, you need to reference EntityA from EntityB:

    public class EntityB<EntityA, long> : Entity<EntityB, long>
    {
        // this is new!
        public virtual EntityA EntityA { get; private set; }
    
        /* ... */
    }
    

    Here’s the new mapping file for EntityB:

    public EntityBMap()
    {
        // first reference EntityA....
        References(x => x.EntityA)
            .SetAttributes(new Attributes
                {
                    {"insert", "false"}, 
                    {"update", "false"}
                });
    
        // ... then use it in the Foreign function
        Id(x => x.Id)
            .GeneratedBy.Foreign("EntityA");
        /* ... */
    }
    

    The SetAttributes call avoids NHibernate trying to map the Id field twice (and hence blowing up).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a mapping for this structure that i designed public
I have a problem with the mapping of Oracle Float double precision datatype to
I have a problem with nhibernate mapping. I use the nhibernate 3.2 mapping by
I have a problem which requires a reversable 1:1 mapping of keys to values.
Let's say I have a simple parent->child class structure as shown below Public Class
I have the following classes (trimmed to only show the basic structure): public abstract
I have a stored proc mapped as follows in NHibernate: <sql-query name=HistoricSearch> <return class=ResultItem>
i have a rather difficult mapping problem. EDIT: reformulated descritpion for historical reasons texts
I have strange problem with disposing entity framework connection in asp.net mvc application. I
I have problem creating new instance of excel 2007 using VBA (from Access 2002).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.