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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:10:07+00:00 2026-05-29T18:10:07+00:00

In a legacy database I have to work with nested tables that are associated

  • 0

In a legacy database I have to work with nested tables that are associated through composite keys. Translated to NHibernate, I have e.g. a class FcoTransportation that has a collection of children of class FcoConsignment. However, in one situation, I would like to load the collection based on only one of the components of the composite key and ignore the other component.

The mapping looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
  <class name="FcoLib.FcoTransportation, FcoLib" table="FCO_TRANSPORTATION">
    <composite-id>
    <key-property name="ID"/>
  <key-property name="FK_EventID"/>
</composite-id>
<!--...snip...-->
<bag name="Consignments" table="FCO_Consignment" lazy="false" cascade="save-update">
  <key>
    <column name="FK_TransportationID"/>
    <column name="FK_EventID"/>
  </key> 
  <one-to-many class="FcoLib.FcoConsignment, FcoLib"/>
</bag>
<!--...snip...-->

I have been trying to create this query this using normal NHibernate criteria, SQL and HQL.
This is what I have got so far in HQL that at least least loads the transports without errors:

 String queryString = "select ft from FcoTransportation as ft";
 queryString += " join ft.Consignments as fc on fc.FK_TransportationID = :ID";

 var query = session.CreateQuery(queryString);

 transports = query
                .SetMaxResults(100)
                .List<FcoTransportation>();  

However, the collection of consignments remains empty! How do I solve this !?

As an extra, I would like to weed out any duplicate children by preferring those entries with the highest values in two columns “ChangedDate” and “ChangedTime” respectively.


As a last resort, I am considering removing the composite key mapping alltogether. In that case, I would still have to remove duplicates on the basis of the latest ChangedDate/ChangedTime…


UPDATE: I have tried removing the composite key mappings, but then I receive an error that apparently is thrown, because the composite foreign keys are enforced even when I try to ignore them. So what is the trick of convincing NHibernate NOT to enforce this, since I easily can write an SQL query in SQL Srv Mgt Studio doing this:

SELECT TOP 100 *
  FROM [FCO_EVENT] AS e 
  INNER JOIN [FCO_TRANSPORTATION] AS t ON e.FK_TransportationID = t.ID
  --children:
  LEFT OUTER  JOIN [FCO_CONSIGNMENT] AS c ON c.FK_TransportationID = t.ID 
  LEFT OUTER JOIN [FCO_CONSIGNMENT_LINES] AS cl ON cl.FK_ConsignmentID = c.ID 

UPDATE: It has been suggested to use a join fetch, which looks promising, but still no children are fetched:

 String queryString = "select ft from FcoTransportation as ft where ft.ID ='" + guid + "'";
 queryString += " join fetch ft.Consignments as fc on fc.FK_TransportationID = '" + guid + "'";

UPDATE: It has also been suggested to do a so-called “theta-style” join, which looks like below, but also here, the children collection is not populated:

String queryString  = "select ft from FcoTransportation as ft, FcoConsignment as fc"
                    + " where ft.ID = fc.FK_TransportationID"
                    + " and ft.ID = '" + guid + "'";

NOTE: I only need to get data out, not save it back again. I already have a query for getting each transport’s consignments (which are dozens at the very most, but usually a few and in some isolated cases a few hundred). I just want to be economic with the amount of roundtrips to the database. That is why I would like the consignments to be fetched while getting the transports out at the same time.

  • 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-29T18:10:16+00:00Added an answer on May 29, 2026 at 6:10 pm

    I think you’re taking a wrong approach to this. The one-to-many relationship mapping is the object oriented equivalent of a foreign key relationship in a relational database. The ability to filter or get a different view of a child collection doesn’t make sense in this context. Assuming you could do it, how could NHibernate persist changes to the filtered child collection? If NHibernate can’t persist it, then it’s modeled incorrectly. Your desire to also “weed out any duplicate children by preferring those entries with the highest values in two columns “ChangedDate” and “ChangedTime”” reinforces this conclusion.

    I would just create a query to return the Consignments you want. Using Future, you could wrap it in a method that would return the FcoTransportation object and the query results in a single trip to the database.

    Another option is to add a method to FcoTransportation that filters the child collection. If the number of Consignments is reasonable (<10000?) and you frequently need to filter the collection in this manner, then I would choose this option.

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

Sidebar

Related Questions

I have a legacy schema that contains tables with primary keys of type binary(16)
I have a legacy database that I'm working on getting ActiveRecord to work with.
In a legacy database (SQL Server 2000), we have a clustered index that looks
As the post title implies, I have a legacy database (not sure if that
I currently have a database that gets updated from a legacy application. I'd like
I have a legacy VB6 application that uploads file attachments to a database BLOB
Im working on a legacy database, that cant be changed. I have a table
I have legacy database in which transaction tables are stored by monthly names. eg.
I have an NHibernate Transaction that does some work and makes a call to
I have a 'legacy' DB2 database that has many other applications and users. Trying

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.