Is there something similar to the discriminator column that can be used for sets in Hibernate 4.3.7?
We have a base class AbstractMaterial, and two subclasses RawMaterial and CarbonMaterial. Three sets are contained in a Product class, one for each material type. Each collection is stored in the same table, and I recently noticed that this results in only the last set actually getting saved. This seems to be because when updating the collection, Hibernate will first delete everything in the table with a Product ID matching the object we are saving and then insert all elements in the set.
Mapping excerpt:
<set lazy="true" name="rawMaterials" sort="natural" table="R053_TPG_RMALLOWED">
<key column="TP_GROUP_ID"/>
<many-to-many class="AbstractMaterial" column="RAWMATERIAL_ID" not-found="ignore" unique="false"/>
</set>
<!-- RawMaterial has extra data in table R023 -->
<set lazy="true" name="rawMaterialWithConstraints" table="R053_TPG_RMALLOWED">
<key column="TP_GROUP_ID"/>
<composite-element class="RawMaterial">
<property column="RAWMATERIAL_ID" name="id"/>
<many-to-one cascade="save-update,delete" class="MassConstraint" column="MASS_CONSTRAINT_ID" lazy="false" name="massConstraint" not-found="ignore"/>
</composite-element>
</set>
<!-- CarbonMaterial has extra data in table R025 -->
<set lazy="true" name="carbonMaterialWithConstraints" table="R053_TPG_RMALLOWED">
<key column="TP_GROUP_ID"/>
<composite-element class="CarbonMaterial">
<property column="RAWMATERIAL_ID" name="id"/>
<many-to-one cascade="save-update,delete" class="MassConstraint" column="MASS_CONSTRAINT_ID" lazy="false" name="massConstraint" not-found="ignore"/>
</composite-element>
</set>
I found something called subselect in the DTD, but could not find any documentation on how to use it. So I would like to to something like
<set lazy="true" name="rawMaterials" sort="natural" table="R053_TPG_RMALLOWED">
<key column="TP_GROUP_ID"/>
<discriminator column="MATERIAL_CATEGORY" value="2">
<many-to-many class="AbstractMaterial" column="RAWMATERIAL_ID" not-found="ignore" unique="false"/>
</set>
<set lazy="true" name="rawMaterialWithConstraints" table="R053_TPG_RMALLOWED">
<key column="TP_GROUP_ID"/>
<discriminator column="MATERIAL_CATEGORY" value="5">
<composite-element class="RawMaterial">
<property column="RAWMATERIAL_ID" name="id"/>
<many-to-one cascade="save-update,delete" class="MassConstraint" column="MASS_CONSTRAINT_ID" lazy="false" name="massConstraint" not-found="ignore"/>
</composite-element>
</set>
<set lazy="true" name="carbonMaterialWithConstraints" table="R053_TPG_RMALLOWED">
<key column="TP_GROUP_ID"/>
<discriminator column="MATERIAL_CATEGORY" value="7">
<composite-element class="CarbonMaterial">
<property column="RAWMATERIAL_ID" name="id"/>
<many-to-one cascade="save-update,delete" class="MassConstraint" column="MASS_CONSTRAINT_ID" lazy="false" name="massConstraint" not-found="ignore"/>
</composite-element>
</set>
The materialCategory value is part of the AbstractMaterial class.
EDIT: We have a whole bunch of Material classes, around half of which are subclasses of RawMaterial, but then we also have some others. We want this list to contain RawMaterials (and its subclasses) or CarbonMaterials, but not the other kinds. In all other mappings we can use AbstractMaterial as the class, but for composite-element it tries to instantiate the (abstract) AbstractMaterial rather than the actual class that ID represents.