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

  • Home
  • SEARCH
  • 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 494177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:28:23+00:00 2026-05-13T05:28:23+00:00

I have the following tables: create table Users( Id uniqueidentifier primary key, InfoId uniqueidentifier

  • 0

I have the following tables:

create table Users(
 Id uniqueidentifier primary key,
 InfoId uniqueidentifier not null unique,
 Password nvarchar(255) not null
)

Create table UserInfo(
 Id uniqueidentifier primary key,
 Company nvarchar(255) not null,
 ContactPerson nvarchar(255) not null
)

And InfoId is a foreign key referencing UserInfo(Id).

I want to map this to the following class:

public class UserCredentials
{
    public virtual Guid Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual string PasswordHash { get; set; }

    protected UserCredentials() { }
}

I want to following mapping:

Id  --> Users(Id)
UserName --> UserInfo(Company)
PasswordHash --> Users(Password)

I tried the following mapping:

<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:nhibernate-mapping-2.2">
    <class name="UserCredentials" table="Users">
        <id name="Id" type="Guid">
            <generator class="guid.comb" />
        </id>

    <property name="PasswordHash" not-null="true" column="Password"/>
    <join table="UserInfo">
      <key column="Id" foreign-key="InfoId"/>
      <property name="UserName" column="Company" not-null="true" />
    </join>
  </class>
</hibernate-mapping>

But it seems the <key> element is incorrectly specified (the foreign-key attribute) does not do what I want. If I leave out the foreign-key attribute, it tries to join on the Id columns of both tables, which is not correct.

I don’t want to include an InfoId property on my UserCredentials class if it is possible to avoid it.

Can anyone help me achieve the desired mapping?

  • 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-13T05:28:24+00:00Added an answer on May 13, 2026 at 5:28 am

    When you want to join in a realation or just another table like you do here based on not the primary key but on some other column you need to use the property-ref attribute.

    like this:

    <property name="InfoId" not-null="true" column="InfoId"/>
    <property name="PasswordHash" not-null="true" column="Password"/>
    
    <join table="UserInfo">
      <key column="Id" property-ref="InfoId"/>
      <property name="UserName" column="Company" not-null="true" />
    </join>
    

    This means that you need to also add InfoId to your UserCredentials class, there is currently no way to mapp a relation or a join like above by using a column name you have to speficy a property (NH will use the column that property refers to)

    On a side note, the foreign-key attribute is a little missleading, it only specifies the name of the database foreign key that will be generated if you let nhibernate create the database schema.

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

Sidebar

Related Questions

No related questions found

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.