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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:51:17+00:00 2026-06-14T18:51:17+00:00

I have a basic structure. A User object and a UserDetails object. The User

  • 0

I have a basic structure. A User object and a UserDetails object. The User table has the identity primary key to generate the UserId, and then I want to also save a UserDetails object for this UserId. Doing this separately this would be easy, but I’m trying to find a way if I can do it in one go, as the User class contains a reference to the UserDetails object. eg

User u = new User() { Name=”me”, Age=17, UserDetails = new UserDetails() { Detail1 = 1 } };

So all I have to do is pass around the user object, which then contains object based references to other related sub-information (I have simplified it greatly for this example but there are several more similar classes as UserDetails, like UserMatchConfiguration etc and a lot of fields for each one)

I want to be able to build up the object in code, or to have it passed around and modified, then to call save on the parent User object, and for it to then save all related objects. I have achieved this so far using one-to-one mappings and save cascades, but the problem is when you create a new object, the UserId is set to zero for all related classes when I want it to save the User class first, then to propogate the generated UserId to all the related classes and then save them.

Mapping is as follows so far.

<class name="User" table="[User]">
    <id name="UserId" unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="FirstName" />
    <property name="LastName" />
    <property name="EmailAddress" />
    <property name="DateOfBirth" />
    <property name="Gender" />
    <property name="PostcodePartOne" />
    <property name="PostcodePartTwo" />
    <many-to-one name="UserLocation" column="UserLocationId" />
    <property name="DateJoined" />
    <property name="LastLoggedOn" />
    <property name="Status"/>
    <property name="StatusNotes" />
    <bag name="Photos" order-by="DisplayOrder asc" inverse="true" lazy="false" cascade="all">
      <key column="UserId" />
      <one-to-many class="UserPhoto" />
    </bag>
    <bag name="Interests" inverse="true" lazy="false" cascade="all">
      <key column="UserId" />
      <one-to-many class="UserMatchInterest" />
    </bag>
    <bag name="Preferences" inverse="true" lazy="false" cascade="all">
      <key column="UserId" />
      <one-to-many class="UserPreference" />
    </bag>
    <one-to-one name="Profile" class="UserProfile" cascade="save-update" />
    <one-to-one name="MatchCriteria" class="UserMatchCriteria" />
    <one-to-one name="MatchLifestyle" class="UserMatchLifestyle" />
    <property name="LastUpdated" />
  </class>

As you can see I am trialing it just with the Profile object for now to try and get it working. How can I have it so that the main User object saves first, then passes the UserId to the other classes as they all use that as their primary key?

Again I could not do the cascade save, then set the UserId manually on each sub class and save them separately, but I am trying to do it in a single call.

  • 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-14T18:51:18+00:00Added an answer on June 14, 2026 at 6:51 pm

    I would say that you are almost there. your user class mapping seems correct, so the suspected for me is UserProfile.

    NHibernate supports exactly this style of relations and it is the one-to-one mapping (as you’ve tried). While we cannot see the UserProfile mapping, let’s extend it following the documentation:

    • http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-onetoone (docs)
    • http://ayende.com/blog/3960/nhibernate-mapping-one-to-one (more exciting reading)

    UserProfile hbm

    <class name="UserProfile" table="[UserProfile]">
      <id name="UserProfileId" column="UserId">
        <generator class="foreign">
          <param name="property">User</param>
        </generator>
      </id>
      ...
      <one-to-one name="User" class="User" constrained="true"/>
    </class>
    

    The UserProfile need these properties:

    public class UserProfile 
    {
       public virtual int UserProfileId { get; set; }
       public virtual User User { get; set; }
       ...
    }
    

    Before insert we have to connect both ends (this is very important step):

    user.Profile =  profile;
    profile.User = user;
    

    and if the cascade mapping in the user.hbm.xml will look like this:

     <one-to-one name="Profile" class="UserProfile" cascade="all" />
    

    we can save just a user and all other entities will be (in correct orther) persisted by NHiberante. No need to handle ID anyhow.

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

Sidebar

Related Questions

I have a basic structure of 2 domain entities: User UserDetails Where a User
I have a very basic Table structure to be placed in middle/center of the
I have a mysql table jobs . This is the basic structure of jobs
I have a very simple database structure (basically only a user table) with two
I'm starting with DITA technology. I have read about the basic structure of a
My permalink structure is set so I have url.com/page I made a basic PHP
I have a basic PHP form (a few fields and 2 checkboxes, I want
I have a basic structure like this: > db.users.findOne() { _id : ObjectId(4f384903cd087c6f720066d7), current_sign_in_at
Here is the basic structure of my User class: class User { private $_userId
I have a gdb user defined command that does some setup, then might do

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.