2

I have a main table that is D1 which has a unique id of d1Id. d1Id uniquely identifies the D1 records and is also a primary key on the table. I have table 2 which is D2 which has a d2seq as a primary key but D2 also has d1Id which is unique which has a foreign key constraint (I think) on D1. But I know for sure it's unique and the id's are the same. I am trying to retrieve d2 values when i make the D1 call using hibernate and i'm unable to get the right hbm for it as i'm getting compile time errors.

The bottom line of the issue is, I need to establish a one to one relationship between 2 tables where in the second table, the id which joins is not the primary key of the table. All the examples i've seen searching google have the case where the ID on the second table is also the primary key of the second table. Let me show you the HBM file I have right now.

<hibernate-mapping default-lazy="true"
package="com.xxx.xx.customer.dto">
<class name="D1" table="D1" dynamic-update="true">
    <id name="dString" column="DID" type="java.lang.String"
        unsaved-value="null" length="9">
        <generator class="assigned"/>
    </id>
...

<one-to-one name="d2" class="com.xxx.xx.customer.dto.D2" >
        <!-- column name="d1Id" /-->
    </one-to-one>

In my second hbm for D2, i'm stuck here

<hibernate-mapping default-lazy="true" package="com.xxx.xx.customer.dto">
<class name="D2" table="D2" dynamic-update="true">
    <id name="id" column="D2ID" type="integer">
        <generator class="native">
            <param name="sequence">D2SEQ</param>
        </generator>
    </id>

<id name="d1id" column="D1ID" type="java.lang.String"
        unsaved-value="null" length="9">
        <generator class="assigned"/>
    </id>

I obviously can't use the second id field for d1id here because 1> it's not a primary key and 2> i've already used it before. So how should my HBM files be for this operation?

Also once I have my HBMs in place, I would like to get D2 information while i query D1 through the DTOs. How can I do that?

mindas
  • 26,463
  • 15
  • 97
  • 154
p0tta
  • 1,461
  • 6
  • 28
  • 49

1 Answers1

0

Why can't you use @ManyToOne relationship? Often it provides more flexibility than @OneToOne

WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52