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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:02:45+00:00 2026-05-25T11:02:45+00:00

Help me translate this into proper NHibernate… I have an Oracle database with 2

  • 0

Help me translate this into proper NHibernate…

I have an Oracle database with 2 tables:

Employees:
    Id (unique id)
    FirstName (string)
    LastName (string)
    Location (string)

Locations:
    Name (string)
    Address (string)

As you can see the Employees table has a unique id, but the Locations table has no id whatsoever. The Name field is a regular field and is not unique.

In Oracle SQL I can run the following query:

SELECT *
FROM Employees e
LEFT OUTER JOIN Locations l
ON e.Location = l.Name
WHERE e.Id = 42

The location where the employee 42 is, has 2 rows in the Locations table, so this query returns 2 rows, one for each location found for employee 42.

Well, I can’t figure out how to translate this into an NHibernate mapping + query (I use Fluent NHibernate). I tend to think that I should map Employees.Location as a Reference to Locations.Name, and so when running my HQL query it should return 2 objects, but NHibernate wouldn’t let me retrieve a list from a Reference. So I tried HasMany but it doesn’t work either, because NHibernate wants a field in Locations referring back to Employees, which kinda makes sense.

My HQL query looks like this:

select e
from Employees e
left join e.Locations l
where e.SGId like :sgId

Why can I do this in regular SQL and not in NHibernate?

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-25T11:02:46+00:00Added an answer on May 25, 2026 at 11:02 am

    I found the solution, you have to use HasMany(x => x.Locations) in conjunction with .PropertyRef(“Location”) so that hibernate knows that the field in Employee to be used for the join should be Location (and not the id of employee as is the default).

    class Employee
    {
        public virtual int Id {get;set}
        public virtual string FirstName {get;set;}
        public virtual string LastName {get;set;}
        public virtual string Location {get;set;}
        public virtual IList<Location> Locations {get;set;}
    }
    
    class EmployeeMapping : ClassMap<Employee>
    {
        public EmployeeMapping()
        {
           Id(x=>x.Id);
           Map(x=>x.FirstName);
           Map(x=>x.LastName);
           Map(x=>x.Location);
           HasMany<Location>(x => x.Locations)
               .PropertyRef("Location")
               .KeyColumn("Name");
        }
    }
    

    Also I found a few flaws in my approach due mainly to the weird legacy database I’m working with. That doesn’t change the solution but I want to add that if one of the tables you’re dealing with doesn’t have unique ids, you’re in trouble. Hibernate needs a unique id, so in my case I had to come up with a composite id made from multiple fields in order to make it unique. This is another debate but I wanted to mention it here to help whoever is researching this topic in the future.

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

Sidebar

Related Questions

I need to translate this into Objective-C, can anyone help ? var randomString =
I have a string abc defg h I wanted to translate each letter into
I need help with this exercise where I have to translate properties from English
Can you help me translate this sql into Linq please: select count(case when t.TypeID
I need your help to translate a query into pyes. This query is working
Who can help me to translate this XML Schema pattern [0-9]+-([0-9]|K) to java regular
Could someone help me translate the following pseudo-code into code understood by Helicon Tech's
I just want to properly translate this foo.push((000 + parseInt(foo1.charAt(loc3), 16).toString(2)).slice(-4)); into php. Anyone
I will like to translate this full code into a zend form with all
I have a MySQL database with over 40,000 records I want to import into

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.