• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate Composite keymapping

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

This is Hameem.M. Now I am stuck up with a hibernate issue.

I have a parent and child table, between which I want to map bidirectional <one-to-many> and <many-to-one> mapping.

Parent table:

Name: person_contact

In the parent table Person_Id and Contact_Id are composite primary keys.

Person_Id(FK)int
Contact_Idint

Mapping file:

<hibernate-mapping>
<class name="com.POJOs.PersonContact" table="person_contact" catalog="testing">
<comment></comment>
<composite-id name="id" class="com.POJOs.PersonContactId">
<key-property name="personId" type="int">
<column name="Person_id" />
</key-property>
<key-property name="contactId" type="int">
<column name="Contact_id" />
</key-property>

</composite-id>


<set name="contactRelationships" inverse="true">
<key>
<column name="Person_id">
<comment></comment>
</column>
<column name="Contact_id" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="com.POJOs.ContactRelationship" />
</set>
</class>
</hibernate-mapping>


Child Table:

Name: contact_relationship

In the child table Contact_Id and Contact_Method_Id are composite primary keys.

Contact_Id(Fk)Int
Contact_Method_Id(FK)Int
Person_Id(FK)Int


Mapping file is:

<hibernate-mapping>
<class name="com.POJOs.ContactRelationship" table="contact_relationship" catalog="testing">
<comment></comment>
<composite-id name="id" class="com.POJOs.ContactRelationshipId">
<key-property name="contactMethod" type="int">
<column name="Contact_method" />
</key-property>
<key-property name="contactId" type="int">
<column name="Contact_id" />
</key-property>
</composite-id>

<many-to-one name="personContact" class="com.POJOs.PersonContact" update="false" insert="false" fetch="select">
<column name="Person_id">
<comment></comment>
</column>
<column name="Contact_id" not-null="true">
<comment></comment>
</column>
</many-to-one>
</hibernate-mapping>

Issue: I am not able to insert the Person_Id value in the child table(contact_relationship) because of this complex relationship between the tables.



The Query used to create the table is:


contact_relationship Table:



DROP TABLE IF EXISTS `testing`.`contact_relationship`;

CREATE TABLE `testing`.`contact_relationship` (

`Contact_id` int(10) unsigned NOT NULL,

`Contact_method` int(10) unsigned NOT NULL,

`Person_id` int(10) unsigned default NULL,

`Party_id` int(10) unsigned default NULL,

PRIMARY KEY (`Contact_id`,`Contact_method`),

KEY `FK_contact_relationship_1` (`Contact_method`),

KEY `FK_contact_relationship_2` (`Party_id`,`Contact_id`),

KEY `FK_contact_relationship_4` (`Person_id`,`Contact_method`),

KEY `FK_contact_relationship_5` (`Person_id`,`Contact_id`),

CONSTRAINT `FK_contact_relationship_1` FOREIGN KEY (`Contact_method`) REFERENCES `contact_type` (`Contact_Method`),

CONSTRAINT `FK_contact_relationship_2` FOREIGN KEY (`Party_id`, `Contact_id`) REFERENCES `party_contact` (`Party_id`, `Contact_id`),

CONSTRAINT `FK_contact_relationship_5` FOREIGN KEY (`Person_id`, `Contact_id`) REFERENCES `person_contact` (`Person_id`, `Contact_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;



Contact_type Table



DROP TABLE IF EXISTS `testing`.`contact_type`;

CREATE TABLE `testing`.`contact_type` (

`Contact_Method` int(10) unsigned NOT NULL default '0',

`Contact_Type` varchar(45) NOT NULL,

PRIMARY KEY (`Contact_Method`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;





Party_contact Table:



DROP TABLE IF EXISTS `testing`.`party_contact`;

CREATE TABLE `testing`.`party_contact` (

`Party_id` int(10) unsigned zerofill NOT NULL,

`Contact_id` int(10) unsigned NOT NULL,

PRIMARY KEY (`Party_id`,`Contact_id`),

KEY `FK_party_contact_1` USING BTREE (`Contact_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;



Person_contact:



DROP TABLE IF EXISTS `testing`.`person_contact`;

CREATE TABLE `testing`.`person_contact` (

`Person_id` int(10) unsigned NOT NULL,

`Contact_id` int(10) unsigned NOT NULL,

PRIMARY KEY USING BTREE (`Person_id`,`Contact_id`),

KEY `FK_person_contact_1` USING BTREE (`Contact_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;


I have attached the main class which am using to insert data for your reference.

Please let me know the solution for this problem.

Thanks in Advance.
reply
    Bookmark Topic Watch Topic
  • New Topic