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