blaks
blaks

Reputation: 123

Self-Reference One-to-One mapping in Hibernate

I have a class A which has a property type indicating whether it is a receipt or a delivery. A receipt can be mapped to a single delivery and vice-versa. Now consider that the receipt transaction and the delivery transaction are siblings.
So class A has a sibling of type A. "A" and its sibling have a one-to-one relationship as established above.

Class A {  
    private A sibling;  
}

<hibernate-mapping>  
    <class name="A" table="A">  
        <id name="Id" type="java.lang.Integer" column="id">  
            <generator class="native"></generator>  
        </id>  
        <one-to-one  name="sibling" class="A" lazy="proxy" />  
    </class>  
</hibernate-mapping>  

I am unable to figure out a way to create a self-referencing one-to-one mapping.

Upvotes: 3

Views: 4742

Answers (1)

Joel Hudon
Joel Hudon

Reputation: 3215

Use the many-to-one unidirectional association. See Unidirectional associations in the hibernate reference documentation.

<hibernate-mapping>   
    <class name="A" table="A">   
        <id name="Id" type="java.lang.Integer" column="id">   
            <generator class="native"></generator>   
        </id>   
        <many-to-one  name="sibling" class="A" cascade="all" />   
    </class>   
</hibernate-mapping> 

You can read those answers for more information on

Upvotes: 2

Related Questions