java - Hibernate: foreign key partially used as primary key -
i have been working hours trying solve problem, seems quite simple database point of view hard figure out hibernate.
i have got 2 tables: vehiclecategory, , vehicle.
a vehicle has 1 vehiclecategory, vehicule references vehiculecategory using foreign key.
vehiclecategory:
- string attr1
- string attr2
- integer attr3
- string attr4
- primary key (attr1, attr2, attr3, attr4)
vehicle
- string name
- string attr1
- string attr2
- integer attr3
- string attr4
- foreign key (attr1, attr2, attr3, attr4) references vehiclecategory
- primary key (name, attr1, attr2, attr3)
here point: do not want use attr4 primary key, want use other foreign attributes in primary key.
this mapping did:
<class name="vehicle" table="vehicle"> <composite-id> <key-property name="name" column="name"/> <key-many-to-one name="vehiclecategory" class="vehiclecategory"> <column name="attr1" not-null="true" /> <column name="attr2" not-null="true" /> <column name="attr3" not-null="true" /> </key-many-to-one> </composite-id> <many-to-one name="vehiclecategory" class="vehiclecategory" fetch="select" insert="false" update="false" > <column name="attr1" not-null="true" /> <column name="attr2" not-null="true" /> <column name="attr3" not-null="true" /> <column name="attr4" not-null="true" /> </many-to-one> </class>
i following exception:
foreign key (fk_s94md9vv63hrpwhyaw4rfxec5:vehicle [attr1,attr2,attr3])) must have same number of columns referenced primary key (vehicle_category [attr4,attr1,attr2,attr3])
i understand exception means. not see why because, point of view, did reference 4-columns primary key in foreign key, , used 3 in vehicule primary key.
i using hibernate 4.3.6
thank !
Comments
Post a Comment