Composite-element does not fetch all data

Hello! I am trying to store extra data for some POJOs using a mapping, but when loading the collection only the extra field is loaded. They are mapped as follows:

        <set lazy="false" name="rawMaterials" sort="natural" table="R053_TPG_RMALLOWED">
            <key column="TP_GROUP_ID"/>
            <many-to-many class="com.pany.rmm.jar.material.AbstractMaterial" column="RAWMATERIAL_ID" not-found="ignore" unique="false"/>
        </set>
        <set lazy="false" name="rawMaterialWithConstraints" table="R057_TPG_RMALLOWED_RM">
            <key column="TP_GROUP_ID"/>
            <composite-element class="com.pany.rmm.jar.material.RawMaterial">
                <property column="RAWMATERIAL_ID" name="id"/>
                <many-to-one cascade="save-update,delete" class="com.pany.rmm.jar.generic.MassConstraint" column="MASS_CONSTRAINT_ID" lazy="false" name="massConstraint" not-found="ignore"/>
            </composite-element>
        </set>
        <set lazy="false" name="carbonMaterialWithConstraints" table="R056_TPG_RMALLOWED_CM">
            <key column="TP_GROUP_ID"/>
            <composite-element class="com.pany.rmm.jar.material.CarbonSourceMaterial">
                <property column="RAWMATERIAL_ID" name="id"/>
                <many-to-one cascade="save-update,delete" class="com.pany.rmm.jar.generic.MassConstraint" column="MASS_CONSTRAINT_ID" lazy="false" name="massConstraint" not-found="ignore"/>
            </composite-element>
        </set>

Fetching the rawMaterials collection works as it should:

    select
        rawmateria0_.TP_GROUP_ID as TP_GROUP1_4_0_,
        rawmateria0_.RAWMATERIAL_ID as RAWMATER2_5_0_,
        abstractma1_.RAWMATERIAL_ID as RAWMATER1_1_1_,
        abstractma1_.RAWMATERIAL_NAME as RAWMATER2_1_1_,
        abstractma1_.RM_GROUP_ID as RM_GROUP3_1_1_,
        case 
            when abstractma1_1_.RAWMATERIAL_ID is not null then 1 
            when abstractma1_2_.RAWMATERIAL_ID is not null then 2 
            when abstractma1_.RAWMATERIAL_ID is not null then 0 
        end as clazz_1_ 
    from
        R053_TPG_RMALLOWED rawmateria0_ 
    inner join
        R022_RM_ABSTRACT abstractma1_ 
            on rawmateria0_.RAWMATERIAL_ID=abstractma1_.RAWMATERIAL_ID 
    left outer join
        R023_RM_SCRAP abstractma1_1_ 
            on abstractma1_.RAWMATERIAL_ID=abstractma1_1_.RAWMATERIAL_ID 
    left outer join
        R025_RM_CARBON abstractma1_2_ 
            on abstractma1_.RAWMATERIAL_ID=abstractma1_2_.RAWMATERIAL_ID 
    where
        rawmateria0_.TP_GROUP_ID=?

but for the other two collections, no data is fetched from the material tables:

    select
        rawmateria0_.TP_GROUP_ID as TP_GROUP1_4_0_,
        rawmateria0_.RAWMATERIAL_ID as RAWMATER2_7_0_,
        rawmateria0_.MASS_CONSTRAINT_ID as MASS_CON3_7_0_,
        massconstr1_.MASSCONSTRAINT_ID as MASSCONS1_8_1_,
        massconstr1_.MAX as MAX2_8_1_,
        massconstr1_.MIN as MIN3_8_1_,
        massconstr1_.VAL as VAL4_8_1_,
        massconstr1_.TYPE as TYPE5_8_1_,
        massconstr1_.ACTIVE as ACTIVE6_8_1_,
        massconstr1_.PERCENT_BASKET_ONE as PERCENT_7_8_1_ 
    from
        R057_TPG_RMALLOWED_RM rawmateria0_ 
    left outer join
        R169_MASSCONSTRAINT massconstr1_ 
            on rawmateria0_.MASS_CONSTRAINT_ID=massconstr1_.MASSCONSTRAINT_ID 
    where
        rawmateria0_.TP_GROUP_ID=?
    select
        carbonmate0_.TP_GROUP_ID as TP_GROUP1_4_0_,
        carbonmate0_.RAWMATERIAL_ID as RAWMATER2_6_0_,
        carbonmate0_.MASS_CONSTRAINT_ID as MASS_CON3_6_0_,
        massconstr1_.MASSCONSTRAINT_ID as MASSCONS1_8_1_,
        massconstr1_.MAX as MAX2_8_1_,
        massconstr1_.MIN as MIN3_8_1_,
        massconstr1_.VAL as VAL4_8_1_,
        massconstr1_.TYPE as TYPE5_8_1_,
        massconstr1_.ACTIVE as ACTIVE6_8_1_,
        massconstr1_.PERCENT_BASKET_ONE as PERCENT_7_8_1_ 
    from
        R056_TPG_RMALLOWED_CM carbonmate0_ 
    left outer join
        R169_MASSCONSTRAINT massconstr1_ 
            on carbonmate0_.MASS_CONSTRAINT_ID=massconstr1_.MASSCONSTRAINT_ID 
    where
        carbonmate0_.TP_GROUP_ID=?

What do I need to add to also include the material tables (R023_RM_SCRAP and R025_RM_CARBON) to the collection fetches?

Link to test case:

A composite element is like @ElementCollection in the annotation world, but you seem to want to map a many-to-many association. You can model that by adding <column/> subelements into the <many-to-many/> tag. See e.g. Chapter 23. Example: Various Mappings

The MassConstraint is not part of the key for the material, so adding it as a <column> does not work. But by duplicating the Raw- and CarbonSource material classes and adding the massConstraint to their mapping I got the result I wanted.
Material.hbm.xml

        <joined-subclass name="com.pany.rmm.jar.material.RawMaterial" table="R023_RM_SCRAP">
            <key column="RAWMATERIAL_ID"/>
        </joined-subclass>
        <joined-subclass name="com.pany.rmm.jar.material.RawMaterialC" table="R023_RM_SCRAP">
            <key column="RAWMATERIAL_ID"/>
            <many-to-one cascade="save-update,delete" class="com.pany.rmm.jar.generic.MassConstraint" column="MASS_CONSTRAINT_ID" lazy="false" name="massConstraint" not-found="ignore"/>
        </joined-subclass>
        <joined-subclass name="com.pany.rmm.jar.material.CarbonSourceMaterial" table="R025_RM_CARBON">
            <key column="RAWMATERIAL_ID"/>
        </joined-subclass>
        <joined-subclass name="com.pany.rmm.jar.material.CarbonSourceMaterialC" table="R025_RM_CARBON">
            <key column="RAWMATERIAL_ID"/>
            <many-to-one cascade="save-update,delete" class="com.pany.rmm.jar.generic.MassConstraint" column="MASS_CONSTRAINT_ID" lazy="false" name="massConstraint" not-found="ignore"/>
        </joined-subclass>

SteelTypeGroup.hbm.xml:

        <set lazy="false" name="rawMaterialWithConstraints" table="R057_TPG_RMALLOWED_RM">
            <key column="TP_GROUP_ID"/>
            <many-to-many class="com.pany.rmm.jar.material.RawMaterialC">
                <column name="RAWMATERIAL_ID" />
            </many-to-many>
        </set>
        <set lazy="false" name="carbonMaterialWithConstraints" table="R056_TPG_RMALLOWED_CM">
            <key column="TP_GROUP_ID"/>
            <many-to-many class="com.pany.rmm.jar.material.CarbonSourceMaterialC">
                <column name="RAWMATERIAL_ID" />
            </many-to-many>
        </set>

As it turns out, it did not work when I tried to implement it in reality.
To summarize, the Material class has a field for a MassConstraint, but there is no column in the Material tables for it. Instead, the MassConstraint ID is kept in tables R056 and R057, but we want them to be inserted into the Material objects. I tried replacing <composite-element> with a straight <many-to-many> and adding a <column> for the MassConstraint, but then Hibernate protested that the foreign key (RAWMATERIAL_ID, MASS_CONSTRAINT_ID) did not match the primary key (RAWMATERIAL_ID) of the Material mapping.

We could do manual fetching of materials with constraints every time we explicitly load a TargetProductGroup, but it is also loaded implicitly in many places (ie, we load other objects that contain a TargetProductGroup reference).

Normally something like

<class name="class1" table="table1">
  <id column="t1_id" />
  <many-to-many class="class2" column="fk_id" name="child" />
</class>

<class name="class2" table="table2">
  <id column="t2_id" />
</class>

works because fk_id is a column in table1, but in this case the MASS_CONSTRAINT_ID associated with a given RAWMATERIAL_ID sits in a separate table.

I can give you this one example from our testsuite to see how you can map multi-column scenarios, but I can’t help you with your model. It’s just too complicated and hbm.xml is deprecated for removal already, so you should really start thinking about migration.

        <set name="users" table="`UserGroup`">
            <key>
                <column name="groupName"/>
                <column name="org"/>
            </key>
            <many-to-many class="org.hibernate.orm.test.schemaupdate.User">
                <column name="userName"/>
                <formula>org</formula>
            </many-to-many>
        </set>