Hi,
I am facing a problem when i am using composite-id with 2 columns.
I have two tables 1.code 2.salesrep.
"code" table contains "codevalue" and "codetype". this "codevalue" is saved in "salesrep" table column "mktregion". Now i want to get the code object in salesrep entity using "many-to-one" relation.
following are the mapping files.
code.hbm.xmlCode:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NorthwindObjects.EntityClasses.MarketingRegion, NorthwindObjects" table="code"
optimistic-lock="version" dynamic-update="true" where =" codetype='R'" >
<composite-id >
<key-property name="MarketingRegionCode" column="codevalue"></key-property>
<key-property name="CodeType" column="codetype" ></key-property>
</composite-id>
<version name="EntityVersion" column="recver" />
<property name="Id" column="primkey" insert="false" update="false" />
<property name="Code" column="code" update="false" />
<property name="Description" column="cdata" />
</class>
</hibernate-mapping>
salesrep.hbm.xmlCode:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true">
<class name="NorthwindObjects.EntityClasses.SalesRep1, NorthwindObjects" table="salesrep" optimistic-lock="version" >
<id name="Id" column="primkey" unsaved-value="0">
<generator class="identity" />
</id>
<version name="EntityVersion" column="recver" />
<property name="SalesRepCode" column="salesrep" />
<property name="SalesRepName" column="repname" />
<property name="MarketRegionCode" column="mktregion" />
<many-to-one name="MarketingRegionObject"
class="NorthwindObjects.EntityClasses.MarketingRegion, NorthwindObjects"
cascade="none" insert="false" update="false" >
<column name="mktregion"/>
</many-to-one>
</class>
</hibernate-mapping>
Our database is developed 15 years back using Foxpro. So we have to use our existing database only.In "code" table we are saving multiple objects data depending upon "Codetype".
Ex : for "MarketingRegion" object codetype is 'R' , for "Terms" object codetype is 'T' like this we are saving 13 objects data in codes table. "codevalue" is mapped to different objects like "salesrep".
We want to get the "MarketingRegionObject" in salersrep entity using its "mktregion" column. It is throwing following error message
Foreign key (FK3152420DD627985E:salesrep [mktregion])) must have same number of columns as the referenced primary key (code [codevalue, codetype])
I am facing 2 problems in this relation ship.
If we remove "composit-id" column property " <key-property name="CodeType" column="codetype" ></key-property>" from code.hbm.xml , salesrep entity is fetching all the records and load the "MarketingRegionObject" also. every thing is working fine. But at this moment i am facing another problem with "code" entity. "codevalue" is duplicate in "codes" table we identified them depending upon "codetype". For every object only one "codevalue" is exist, we are validating this before saving the code objects. for ex: MarketingRegion code contains codevalue as '00' and codetype='R' and "Terms" contains codevalue as '00' and codetype as 'T' like this for all 13 objects we have similar data in "code" table.
1.If we update any "MarketingRegion" code or "Terms" code objects it is throwing an error message like ..."Duplicate identifier in table for:[NorthwindObjects.EntityClasses.MarketingRegion#NorthwindObjects.EntityClasses.MarketingRegion]"
2.If we give "<key-property name="CodeType" " in "composit-id" it is adding codetype also in where condition while updating code objects records. But it is throwing errors for remaing objects like "salesrep" object mapping.
How can we resolve this problems?
1. Is there any way to add a where condition while commit the transactions.(update/save). In transaction.commit it is commit all the changes made on the session.
2. If we add "codetype" in composit-id how can we avoid the "
Foreign key (FK3152420DD627985E:salesrep [mktregion])) must have same number of columns as the referenced primary key (code [codevalue, codetype])
" error message. How can we give the "many-to-one" relation using a single column?
we have to use our existing database only.
How can we give the mapping "many-to-one" relations in salerep.hbm.xml?
Regards
KK