I want mapped in file mapping (Product.hbm.xml --> Product.java) a collection with a property
naturalId that not the primary key...Collection items are object mapped with a <composite-id> (Product_DopingClassification.hbm.xml --> Product_DopingClassification.java)
But when I save
PRODUCT, the relation between this two items is made with the primary key of
PRODUCT and not with the property
naturalId...
Then my question is : how can I mappe this relation ?
Please...
Hibernate version:
3.13
Mapping documents:
Product.hbm.xml
Code:
<class name="com.vidal.bo.model.product.Product" table="product">
<id name="id" type="long" column="id">
<generator class="increment"/>
</id>
<property name="naturalId" type="integer" column="product_Id"/>
[...]
<!-- product_dopingClassification link -->
<set name="product_dopingClassification_set" lazy="false" cascade="all" inverse="true">
<key column="product_id" property-ref="naturalId"/>
<one-to-many class="com.vidal.bo.model.product.dopingProduct.Product_DopingClassification" />
</set>
[...]
Product_DopingClassification.hbm.xml
Code:
<hibernate-mapping>
<class name="com.vidal.bo.model.product.dopingProduct.Product_DopingClassification" table="product_dopingClassification">
<composite-id>
<key-many-to-one name="product" class="com.vidal.bo.model.product.Product" column="product_Id" lazy="false"/>
<key-many-to-one name="dopingClassification" class="com.vidal.bo.model.dictionary.molecule.dopingMolecule.DopingClassification" column="dopingClassification_Id" lazy="false" />
<key-many-to-one name="versionIn" class="com.vidal.bo.model.versioning.domain.EntityVersion" column="vIn" lazy="false"/>
</composite-id>
<property name="isValid" type="true_false" column="isValid" />
<!-- many-to-one name="versionIn" column="vIn" class="com.vidal.bo.model.versioning.domain.EntityVersion"/ -->
<many-to-one name="versionOut" column="vOut" class="com.vidal.bo.model.versioning.domain.EntityVersion"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Product product = new Product();
product.setName("product name NIASPAN in Version 1");
Product_DopingClassification productDopingClassification = new Product_DopingClassification();
productDopingClassification.setProduct(product);
productDopingClassification.setDopingClassification(currentEntitiesDopingClassification.get(0));
product.getProduct_dopingClassification_set().add(productDopingClassification);
productDao.saveOrUpdate(product);
Full stack trace of any exception that occurs:
No explicite error but the value in
product_id column in
product_dopingClassification table is value of the primary key
id and not the
naturalId value
Name and version of the database you are using:
HSQLDB
The generated SQL (show_sql=true):
Hibernate:
insert
into
product
(product_Id, vIn, vOut, name, shortName, veryShortName, spcProductName, spcFolderNumber, spcPosology, spcInternalId, cis, isUnmarket, productDataSempProductionId, maximumDeliveryDuration, pharmacyClassificationRootCode, ranking, pharmacokineticComment, spectrumComment, incompatibilityComment, pharmacodynamicComment, modalityHandlingComment, preclinicalSafetyComment, country_id, productLine_id, pharmacyClassification_id, usefullComplement_id, tasteComplement_id, formComplement_id, composantComplement_id, commonNameGroup_id, form_id, list_id, dosage_id, procedureType_id, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
product_dopingClassification
(isValid, vOut, product_Id, dopingClassification_Id, vIn)
values
(?, ?, ?, ?, ?)