Hi,
I am new to hibernate. Checked the references and tutorials but not able to find a solution for my problem. I am unable to insert records in Parent and child relation tables at a time.
I have the following tables.
Table: Protocol
protocol_id number; ( primary key )
protocol_name varchar2(50);
Table: Protocol_properties
protocol_id number;
property_id number;
property_value varchar2(50);
primary key(protocol_id, property_id)
where protocol_id is foriegn key referencing protocol_id of Protocol table.
I have the beans for both the tables with get and set methods.
public class Protocol{
int protocolId;
int protocolName;
ProtocolProperties[] properties;
/* get/ set methods for all these properties */
}
public class ProtocolProperties{
int protocolId;
int propertyId;
String propertyValue;
/* get/set methods for these properties */
}
now if i get a Protocol Object with all the properties in the array, i would like to save them to the above mentioned tables.
So, specified the following entries in my hibernate mapping xml:
<class name="Protocol" table="Protocol">
<id name="protocolId" column="PROTOCOL_ID">
<generator class="increment"/>
</id>
<property name="protocolName" column="PROTOCOL_NAME"/>
<array name="properties" table="Protocol_Properties">
<key column="PROTOCOL_ID" not-null="true"/>
<list-index column="PROPERTY_ID" /> <!-- not sure what is this for?-->
<one-to-many class="ProtocolProperties"/>
</array>
</class>
<class name="ProtocolProperties" table="Protocol_Properties">
<composite-id>
<key-property name="propertyId" column="PROPERTY_ID"/>
<key-property name="protocolId" column="PROTOCOL_ID"/>
</composite-id>
<property name="propertyValue" column="PROPERTY_VALUE"/>
</class>
I am getting duplicate mapping error for protocolId, so i also tried with key-many-to-one property for protocolId in composite-id like
<key-many-to-one name="protocolId" class="Protocol" table="Protocol"/>
but no luck, getting the same error. Could anyone please let me know how to specify the referenced protocolId correctly in the "ProtocolProperties" class.
Note: The protocolId in Protocol is generated by system and it should use the same protocolId for all the properties in Protocol_Properties table as well, without me updating that property explicitly to each of the ProtocolProperites objects.[quote][/quote]
_________________ With Regards,
Ravi
|