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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:24:18+00:00 2026-05-19T04:24:18+00:00

I started using NHibernate today, but I cannot figure out how I setup a

  • 0

I started using NHibernate today, but I cannot figure out how I setup a simple relation between two tables. I don’t really know what it’s called, it could be one-to-many or foreign key relation (I’m not that into database design and the terms used), but here’s a very simple example.

I have a table Product with attributes Id (PK), ProductName and CategoryId. Then I have a table Categories with attributes Id (PK) and CategoryName.

I created these classes:

public class Product
{
    public virtual int Id { get; set; }  
    public virtual string ProductName { get; set; }  
    public virtual int CategoryId { get; set; }  

    public virtual Category Category { get; set; }  

    public virtual string CategoryName  
    {  
        get { return this.Category == null ? String.Empty : this.Category.CategoryName; } 
    }
}

public class Category  
{  
    public virtual int Id { get; set; }
    public virtual string CategoryName { get; set; }
}

In other words, I simply want the Product to store to which category it belongs (via the CategoryId attribute which points to an Id in the Categories table). I don’t need the Category class to hold a list of related Products, if that makes it any simpler.

To make it even more clear what I’m after, this is the SQL that I’m expecting:

SELECT Products.*, Categories.*
FROM Products INNER JOIN Categories ON Products.CategoryId = Categories.Id

at least that’s what I think it should be (again, I’m not that good at database design or queries).

I can’t figure out which kind of mapping I need for this. I suppose I need to map it in the Product.hbm.xml file. But do I map the CategoryId as well? And how do I map the Category property?

It seems like I would need a ‘one-to-many’ relation since I have ONE category per product (or is this reasoning backward?) but it seems like there is no one-to-many mapping…

Thanks for any help!

Addition:

I tried to add the many-to-one relation in the Person mapping, but I keep getting an exception saying “Creating proxy failed”, and in the inner exception “Ambiguous match found”.

I should maybe mention I am using an old version of NHibernate (1.2 I think) because that is the only one I got running with MS Access due to it not finding the JetDriver in newer versions.

I’ve put the mapping files, classes, and code where the error occurs in screenshots because I can’t figure out how to post XML code here… It keeps reading it as html tags and skipping half of it. Anyway.

The mappings:
http://www.nickthissen.nl/Images/tmp7B5A.png

The classes:
http://www.nickthissen.nl/Images/tmpF809.png

The loading code where the error occurs:
http://www.nickthissen.nl/Images/tmp46B6.png
(As I said, the inner exception says “Ambiguous match found”.

(Product in my example has been replaced by Person)

The Person and Category classes inherit Entity which is an abstract base class and defines the Id, Deleted, CreatedTime and UpdatedTime properties.
The code where the error occurs is in a generic ‘manager’ class (type parameter TEntity which must inherit Entity). It is simply supposed to load all entities with the Deleted attribute false. In this case, TEntity is ‘Person’.

It works fine if I leave out the many-to-one Category mapping in the Person mapping, but then obviously the Category property is always null.

Oh yeah, sorry about the mix between C# and VB, the C# code is in a generic framework I use for multiple projects while the VB part is the actual implementation of that framework on my website and I just happened to use VB for that.

Help? Thanks!

  • 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-19T04:24:19+00:00Added an answer on May 19, 2026 at 4:24 am

    In your Product class only needs to contain a Category object, you don’t need a CategoryId property. Then in your Product mapping you need to have this entry

    <many-to-one name="Category" column="CategoryId" />
    

    UPDATE:
    Your mappings appear to be missing the fully qualified name of the mapped class in the tag. See http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-class

    UPDATE 2:

    See if this helps you NHibernate 1.2 in a .NET 4.0 solution

    • 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.