chihbn wrote:
david wrote:
Its a composite key so you must have the separate key. The fields of the key will no longer be key-many-to-one and now be simple properties.
The main class will have the many-to-one entries mapped with the update=false, insert=flase.
Where should i put columns mapping? In key class or main class ? Thanks.
I've tried put the key columns mapping in key or main class only and in both key and main class. They don't works for me, similar to key-many-to-one, only one key column got mapped , missing the second column mapping. see the following for map files and errors.
----
//Case1. map 2 key columns( composite-id )in key class only
//main class :
/**
* @hibernate.many-to-one class="edu.mnscu.housing.common.vo.YearTerm" update="false" insert="false"
*/
public YearTerm getYearTerm(){
return yearTerm;
}
//key class:
/**
* @hibernate.property column="yrtr" not-null="true"
*/
public String getYrtr(){
return _yrtr;
}
public void setYrtr( String yrtr ){
_yrtr = yrtr;
}
/**
* @hibernate.property column="rc_id"
*/
public String getRcid(){
return _rcid;
}
//mapping file for main class:
...
<class
name="edu.mnscu.housing.common.vo.AssignmentPeriod"
table="yrtr_cal_dates"
dynamic-update="false"
dynamic-insert="false"
>
<composite-id
name="id"
class="edu.mnscu.housing.common.vo.AssignmentPeriodPK"
>
<key-property
name="yrtr"
type="java.lang.String"
column="yrtr"
/>
<key-property
name="rcid"
type="java.lang.String"
column="rc_id"
/>
....
</composite-id>
<many-to-one
name="yearTerm"
class="edu.mnscu.housing.common.vo.YearTerm"
cascade="none"
outer-join="auto"
update="false"
insert="false"
access="property"
column="yearTerm"
/>
//errors:
Foreign key (yrtr_cal_dates [yearTerm])) must have same number of columns as the referenced primary key (yrtr_cal [yrtr,rc_id])
------------------
//Case2: map 2 columns in both main class and key class, missing second column mapping.
//error: Foreign key (yrtr_cal_dates [yrtr])) must have same number of columns as the referenced p
rimary key (yrtr_cal [yrtr,rc_id])
/**
* @hibernate.many-to-one class="edu.mnscu.housing.common.vo.YearTerm" column="yrtr" update="false" insert="false"
* @hibernate.many-to-one class="edu.mnscu.housing.common.vo.YearTerm" column="rc_id" update="false" insert="false"
**/
public YearTerm getYearTerm(){
return yearTerm;
<hibernate-mapping>
<class
name="edu.mnscu.housing.common.vo.AssignmentPeriod"
table="yrtr_cal_dates"
dynamic-update="false"
dynamic-insert="false"
>
<composite-id
name="id"
class="edu.mnscu.housing.common.vo.AssignmentPeriodPK"
>
<key-property
name="yrtr"
type="java.lang.String"
column="yrtr"
/>
<key-property
name="rcid"
type="java.lang.String"
column="rc_id"
/>
...
</composite-id>
<many-to-one
name="yearTerm"
class="edu.mnscu.housing.common.vo.YearTerm"
cascade="none"
outer-join="auto"
update="false"
insert="false"
access="property"
column="yrtr"
/>
---------------
//Case3: map in main class only, no <key-property> got generated
//see same errors as case2. Foreign key (yrtr_cal_dates [yrtr])) must have
same number of columns as the referenced primary key (yrtr_cal [yrtr,rc_id])
<hibernate-mapping>
<class
name="edu.mnscu.housing.common.vo.AssignmentPeriod"
table="yrtr_cal_dates"
dynamic-update="false"
dynamic-insert="false"
>
<composite-id
name="id"
class="edu.mnscu.housing.common.vo.AssignmentPeriodPK"
>
...
</composite-id>
<many-to-one
name="yearTerm"
class="edu.mnscu.housing.common.vo.YearTerm"
cascade="none"
outer-join="auto"
update="false"
insert="false"
access="property"
column="yrtr"
/>
Since those tables are read only for our apps now, our workaround was to not map associations , map key columns in key class. And do separate query. That works for us.