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 9294703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:25:58+00:00 2026-06-18T21:25:58+00:00

This question is related to Mapping interface or abstract class component I’m also trying

  • 0

This question is related to Mapping interface or abstract class component
I’m also trying to map a component declared as an interface, but I’m using the built-in mapping-by-code/Conformist approach.

Let’s say I have an entity Login (C#):

public class Login
{
    public virtual int Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual IPassword Password { get; set; }
}

I want to abstract away how the password is stored, so I’ve defined a simple interface IPassword

public interface IPassword
{
    bool Matches(string password);
}

An example implementation is HashedPassword:

public class HashedPassword : IPassword
{
    public virtual string Hash { get; set; }
    public virtual string Salt { get; set; }

    public virtual bool Matches(string password){ /* [snip] */ }
}

I want to map Login.Password as a component rather than a many-to-one or one-to-one relation. Using XML I’d map it like this:

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="..." namespace="...">
  <class name="Login">
    <id name="Id">...</id>
    <property name="UserName" .../>
    <component name="Password" class="HashedPassword">
      <property name="Hash" not-null="true" length="32"/>
      <property name="Salt" not-null="true" length="32"/>
    </component>
  </class>
</hibernate-mapping>

This works as expected.

Here’s my attempt to map it using NHibernate’s built-in mapping-by-code facilities:

public class LoginMapping : ClassMapping<Login>
{
    public LoginMapping()
    {
        Id(x => x.Id, map => map.Generator(Generators.HighLow));
        Property(x => x.UserName, map => map.Length(32));
        Component(x => x.Password, comp =>
            {
                comp.Class<HashedPassword>();
                comp.Property("Salt", map => map.Length(32));
                comp.Property("Hash", map => map.Length(32));
            });
    }
}

When I use this mapping I get the following exception:

NHibernate.MappingException: Member not found. The member ‘Salt’ does not exists in type IPassword

While it’s true that Salt isn’t a member of IPassword, it is a member of the class that I set with comp.Class<HashedPassword>()

Do you know how I can map this scenario without getting the exception?


So far there I haven’t found a solution to the question itself. There are two work-arounds at the moment:

  1. Resort to XML mapping or FluentNHibernate. This could probably done for the “problematic” mappings only.

  2. Instead of a component, use a user type. This is what I’m doing right now. The type in my case (the hashed password) is immutable and can be stored as a single column, so the user type is fairly simple.

Here’s the user type I’m currently using (for sake of completion). I use PBKDF2 to create secure hashes. Note that in my application all data (salt, hash and PBKDF2 iteration count) is stored in one property (simply named Hash) of the HashedPassword.

public abstract class ImmutableValue<T> : IUserType where T : class
{
    public abstract SqlType[] SqlTypes { get; }

    public virtual Type ReturnedType
    {
        get { return typeof (T); }
    }

    public bool IsMutable
    {
        get { return false; }
    }

    bool IUserType.Equals(object x, object y)
    {
        return InternalEquals(x, y);
    }

    protected virtual bool InternalEquals(object x, object y)
    {
        return Equals(x, y);
    }

    public virtual int GetHashCode(object x)
    {
        return x == null ? 0 : x.GetHashCode();
    }

    public virtual object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        return Load(rs, names, owner);
    }

    protected abstract T Load(IDataReader rs, string[] names, object owner);

    public virtual void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        Save(cmd, (T) value, index);
    }

    protected abstract void Save(IDbCommand cmd, T value, int index);

    public virtual object DeepCopy(object value)
    {
        return value;
    }

    public virtual object Replace(object original, object target, object owner)
    {
        return original;
    }

    public virtual object Assemble(object cached, object owner)
    {
        return cached;
    }

    public virtual object Disassemble(object value)
    {
        return value;
    }

    protected void SetParameter(IDbCommand cmd, int index, object value)
    {
        var parameter = (IDataParameter) cmd.Parameters[index];
        var parameterValue = value ?? DBNull.Value;
        parameter.Value = parameterValue;
    }
}

public class HashedPasswordType : ImmutableValue<HashedPassword>
{
    public override SqlType[] SqlTypes
    {
        get { return new SqlType[] {SqlTypeFactory.GetString(HashedPassword.ContentLength)}; }
    }

    protected override HashedPassword Load(IDataReader rs, string[] names, object owner)
    {
        var str = NHibernateUtil.String.NullSafeGet(rs, names[0]) as string;
        return HashedPassword.FromContent(str);
    }

    protected override void Save(IDbCommand cmd, HashedPassword value, int index)
    {
        SetParameter(cmd, index, value == null ? null : value.Hash);
    }
}

The required mapping is then comparatively simple:

Property(x => x.Password, map =>
    {
        map.Type<HashedPasswordType>();
        map.NotNullable(true);
    });
  • 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-06-18T21:25:59+00:00Added an answer on June 18, 2026 at 9:25 pm

    As I haven’t found a solution using component mapping yet I’ll add the workarounds as an answer for now.

    Possible workarounds:

    1. Use XML mapping for the types in question. This supports my scenario without an issue. See the question for an example.
    2. Use FluentNHibernate. It just supports this kind of mapping out of the box, see example below.
    3. Use a user type instead of a component. This is comparatively much more work, but at least it will work with mapping-by-code.

    Example of the desired mapping in FluentNH. The concrete type is passed in as the generic argument.

    Component<HashedPassword>(x => x.Password, comp =>
    {
        comp.Map(x => x.Hash);
        comp.Map(x => x.Salt);
    });
    

    An example of a user type is given at the end of the question. In the real example I use PBKDF2 to create secure hashes. Note that in the user type shown in the question all data (salt, hash and PBKDF2 iteration count) are stored in one column to keep the type simple.

    My current conclusion is that mapping-by-code simply does not support what I’m asking for. I’m now considering moving from mapping-to-code to FluentNH.

    If there’s news related to this issue I’ll update the answer and/or question accordingly.

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

Sidebar

Related Questions

This question is related to another question I wrote: Trouble using DOTNET from PHP.
This question is related to the one I asked here . I'm trying to
This question is related to this SO post Rather than using a recursive CTE
This question is related to Hibernate using JPA (annotated Entities) and liquibase . I
In this article http://www.jroller.com/eyallupu/entry/hibernate_the_any_annotation and also in this question How to use Hibernate @Any-related
This question might be better asked over on ServerFault, but since this is related
This question is related to this post but I don't see how I can
This is related to another question I asked last week, but the current issue
I've seen several questions related to properly mapping an enum type using NHibernate. This
This is related (but fairly independent) to my question here: Why SELECT N +

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.