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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:41:46+00:00 2026-05-20T21:41:46+00:00

I have a schema that looks like this User Id INT Username VARCHAR(100) Password

  • 0

I have a schema that looks like this

User

Id INT
Username VARCHAR(100)
Password VARCHAR(100)

User_Info_Key

Id INT
Description VARCHAR(100)

User_Info

User INT
Info_Key INT
Info_Value VARCHAR(100)
PRIMARY KEY(User, Info_Key)

Here’s my user mapping file:

<class name="User" table="[user]">
  <id name="Id">
    <column name="id" />
    <generator class="native" />
  </id>
  <property name="Username" />
  <property name="Password" />
  <bag name="InfoValues" cascade="all" lazy="false">
    <key column="[user]" />
    <one-to-many class="FMInfoValue" />
  </bag>
</class>

<class name="InfoKey" table="[user_info_key]">
  <id name="Id">
    <column name="id" />
    <generator class="native" />
  </id>
  <property name="Description" />
</class>

<class name="InfoValue" table="[user_info]">
  <composite-id>
    <key-property name="User" column="[user]" type="int"></key-property>
    <key-property name="Key" column="[info_key]" type="int"></key-property>
  </composite-id>
  <property name="Value" column="[info_value]" />
  <many-to-one name="FMInfoKey" class="FMInfoKey"
             column="[info_key]"
             cascade="all"
             lazy="false" />
</class>

When I create a new user I do this:

User newuser = new User();
newuser.Username = this.Username;
newuser.Password = this.Password;
session.Save(newuser);

And this saves the base information.

But when I try to add the first name (info key value 1) and last name (info key value 2) nothing is saved to the database:

InfoValue userfname = new InfoValue();
InfoKey userfnamekey = new InfoKey();
userfnamekey.Id = 1;
userfname.InfoKey = userfnamekey;
userfname.Key = 1;
userfname.User = (int) newuser.Id;
userfname.Value = this.Firstname;
session.Save(userfname);

InfoValue userlname = new InfoValue();
InfoKey userlnamekey = new InfoKey();
userlnamekey.Id = 2;
userlname.InfoKey = userlnamekey;
userlname.User = (int)newuser.Id;
userlname.Value = this.Lastname;
session.Save(userlname);

How do I get NHibernate to save the two info values to the info value table?

  • 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-20T21:41:47+00:00Added an answer on May 20, 2026 at 9:41 pm

    First, I think what you want to do is having two many-to-one properties in your InfoValue class. That would look like this:

    class InfoValue
    {
        public virtual User User { get; set; }
        public virtual InfoKey Key { get; set; }
        public virtual String Value { get; set; }
    }
    

    Your mapping would look something like this:

    <class name="InfoValue" table="[user_info]">
      <composite-id>
        <key-many-to-one name="User" column="[user]" lazy="proxy" class="User" />
        <key-many-to-one name="Key" column="[info_key]" lazy="proxy" class="InfoKey" />
      </composite-id>
      <property name="Value" column="[info_value]" />
    </class>
    

    Example taken and adapted from here: Composite-id with many-to-one

    —– Possibly unwanted advice —–

    Second, if I may be so bold to offer some advice, do not do this. I am referring to your table structure. If my assumption is wrong, feel free to ignore my advice.

    I assume that you want to put the user information in an extra table in order to avoid having to modify the table structure if and when new properties are required. If that is the reason, I can honestly tell you from my own experience that this will lead to a lot of headache. Apart from the performance issue when querying the tables everything else will also just be much more complicated. Your example of inserting values speaks for itself. Any queries where you search for those properties will just be as bad. What will you do if you want to save anything but Strings? Casting to int, DateTime, bool, etc.?

    I could go on and on here, but unless you absolutely cannot alter the table structure later on, please do not pursue this idea.

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

Sidebar

Related Questions

I have a schema design that looks like this: Table: User Row Column Family
I have a schema that essentially looks like this: CREATE TABLE `data` ( `id`
I have schema that looks like this: create_table users, :force => true do |t|
I have a table that looks like this: Id (PK, int, not null) ReviewedBy
I have a legacy SQL schema which looks something like this: CREATE TABLE `User`
I am working with a legacy database schema that looks like this: product_table table
I have an XML structure that looks like: <Succeeded p1:type=Edm.Boolean xmlns:p1=http://schemas.microsoft.com/ado/2007/08/dataservices/metadata xmlns=http://schemas.microsoft.com/ado/2007/08/dataservices>false</Succeeded> Since I
I have a database schema that is similar to the following: | User |
Generally, MVC frameeworks have a structure that looks something like: /models /views /controllers /utils
I know that I should have schema of a table before calling NewRow method

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.