This discussion is archived
3 Replies Latest reply: Aug 9, 2012 6:49 AM by JamesSutherland RSS

Composite Key in Toplink Mapping File - equivalent to Hibernate?

953418 Newbie
Currently Being Moderated
Hi,

I have a POJO TestEntity which refers to primary key TestEntityKey. In Hibernate mapping hbm file, we define the entry as:

          <composite-id name="id" class="TestEntityKey">
               <key-property name ="testEntityCode" column = "TEST_ENT_CODE" type = "string" />
          </composite-id>

Please confirm how to create the composite id in Toplink means kindly help that how to map my id to key:

<opm:primary-key>
     <opm:attribute-name>id</opm:attribute-name>
<opm:field table="TEST_ENTITY_B" name="TEST_ENT_CODE" xsi:type="opm:column"/>
</opm:primary-key>

Thanks!
  • 1. Re: Composite Key in Toplink Mapping File - equivalent to Hibernate?
    JamesSutherland Pro
    Currently Being Moderated
    If you are using JPA you can define composite ids using annotations, see,

    http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Composite_Primary_Keys

    For native TopLink you primary key is defined in terms of fields (columns), you only need to set the column names. The primary key is defined independently of the object's attributes/mappings. You can map the column any way that you wish (direct, aggregate, one-to-one), in your case you would have an AggregateObjectMapping to your embedded id.
  • 2. Re: Composite Key in Toplink Mapping File - equivalent to Hibernate?
    953418 Newbie
    Currently Being Moderated
    Hey.. Thanks!

    I am clear about the concept about defining annotation based composite key. Also, I read in the documentation that I'll be needing to define as direct, aggregate or one-to-one. But, I am not able to define and run the same in the project mapping xml of toplink.

    It would be great if you can share some sample code for defining the same. For e.g. in my mentioned example, there is TestEntity POJO having 'id' as the attribute which gets populated with the testEntityCode of the TestEntityKey POJO. Please suggest the same for the same:

    <opm:primary-key>
    <opm:attribute-name>id</opm:attribute-name>
    <opm:field table="TEST_ENTITY_B" name="TEST_ENT_CODE" xsi:type="opm:column"/>
    </opm:primary-key>

    Thanks!
  • 3. Re: Composite Key in Toplink Mapping File - equivalent to Hibernate?
    JamesSutherland Pro
    Currently Being Moderated
    To define a composite primary key in the native TopLink XML it would be,

    <opm:primary-key>
    <opm:field table="TEST_ENTITY_B" name="TEST_ENT_CODE" xsi:type="opm:column"/>
    <opm:field table="TEST_ENTITY_B" name="TEST_ENT_CODE2" xsi:type="opm:column"/> (or whatever your other column is)
    </opm:primary-key>

    What version of TopLink are using using? If you are migrating, I would recommend migrating to TopLink 11g using EclipseLink and JPA.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points