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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:37:37+00:00 2026-05-25T19:37:37+00:00

Here are the mappings: <?xml version=1.0 encoding=utf-8 ?> <hibernate-mapping xmlns=urn:nhibernate-mapping-2.2 assembly=cs_nhibernate_test1 namespace=cs_nhibernate_test1> <class name=User

  • 0

Here are the mappings:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="cs_nhibernate_test1"
                   namespace="cs_nhibernate_test1">

    <class name="User" table="users">
      <id name="Id" type="int">
        <column name="Id" not-null="true" />
        <generator class="native"/>
      </id>
      <property name="Name" column="Name" />
      <property name="Age" column="Age" />      
      <bag name="Posts" inverse="true" cascade="all">
        <key column="UserId" />
        <one-to-many class="Post" />
      </bag>      
    </class>

    <class name="Post" table="posts">
      <id name="Id" type="int">
        <column name="Id" not-null="true" />
        <generator class="native"/>
      </id>
      <property name="Text" column="Text" />
      <many-to-one name="User" column="UserId" />
    </class>

</hibernate-mapping>

Here’s the code:

public class User
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual int Age { get; set; }
    public virtual IList<Post> Posts { get; set; }

    public override string ToString()
    {
        return string.Format("User{{id={0}, name='{1}', age={2}}}", Id, Name, Age);
    }
}

public class Post
{
    public virtual int Id { get; set; }
    public virtual string Text { get; set; }
    public virtual User User { get; set; }

    public override string ToString()
    {
        return string.Format("Post{{id={0}, text='{1}', user={2}}}", Id, Text, User);
    }
}

    static void Main(string[] args)
    {
        ...
        ISessionFactory sessionFactory = cfg.BuildSessionFactory();
        using (ISession session = sessionFactory.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                for (int i = 0; i < 3; ++i)
                {
                    var user = new User
                    {
                        Name = string.Format("John Smith #{0}", i + 1),
                        Age = 25 + i,
                        Posts = new List<Post>()
                    };

                    for (int j = 0; j < 3; ++j )
                    {
                        var post = new Post
                        {
                            Text = string.Format("qwerty {0} {1}", i, j)
                        };
                        user.Posts.Add(post);
                    }

                    session.SaveOrUpdate(user);
                }
                transaction.Commit();
            }

            foreach (User u in session.CreateCriteria(typeof (User)).List<User>())
            {
                Console.WriteLine(u);
                foreach (Post post in u.Posts)
                {
                    // post.User is always null!
                    Console.WriteLine("  {0}", post);
                }
            }

            foreach (Post p in session.CreateCriteria(typeof(Post)).List<Post>())
            {
                // p.User is always null!
                Console.WriteLine(p);
            }
        }
    }

Post.User is always null. Already spent 3 hours. Where am I wrong?

  • 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-25T19:37:37+00:00Added an answer on May 25, 2026 at 7:37 pm
    1. You never set the Post.User property and the User.Posts collection is inverse, so no reference will be created in the database (if the posts.UserId column would be not null you would get an error on insert).
    2. You are still in the same session, so even if the User.Posts collection would be non-inverse, the Post.User property would still be null. NHibernate doesn’t modify your objects on save (other than replacing the collections with its own implementations) and the criteria returns the same objects you just saved because they are in the session cache.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my NHibernate mapping. <?xml version=1.0 encoding=utf-8 ?> <hibernate-mapping xmlns=urn:nhibernate-mapping-2.2 assembly=HelloNHibernate namespace=HelloNHibernate> <class name=Showing
I'm having a bit o' trouble... Here is my mappings.xml file... <?xml version=1.0 encoding=utf-8?>
I have a mapping in NHibernate that works like so: <?xml version=1.0 encoding=utf-8 ?>
here is my web.xml : <?xml version=1.0 encoding=UTF-8?> <web-app version=2.4 xmlns=http://java.sun.com/xml/ns/j2ee xmlns:xml=http://www.w3.org/XML/1998/namespace xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee
Here's a working web.xml: <?xml version=1.0 encoding=UTF-8?> <web-app xmlns=http://java.sun.com/xml/ns/javaee xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd version=3.0> <session-config>
I'm having real problems with setting up nHibernate with sqlite. Here is the hibernate.cfg.xml
Why don't I have any class mappings after calling Configuration.Configure()? Here is my class
I'm trying to extend an xml schema (nhibernate here, for example), to add my
I have a Nhibernate mapping file for a simple user/role mapping. Here are the
As I don't want to hijack another thread here comes my question about mappings.

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.