Optional one-to-one mapping in Hibernate

Posted by hibernate on Stack Overflow See other posts from Stack Overflow or by hibernate
Published on 2010-05-06T20:26:56Z Indexed on 2010/05/07 0:18 UTC
Read the original article Hit count: 656

Filed under:
|
|
|

How do I create an optional one-to-one mapping in the hibernate hbm file? For example, suppose that I have a User and a last_visited_page table. The user may or may not have a last_visited page. Here is my current one-to-one mapping in the hbm file:

User Class:

<one-to-one name="lastVisitedPage" class="LastVisitedPage" cascade="save-update">

LastVisitedPage Class:

<one-to-one name="user" class="user" constrained="true" />

The above example does not allow the creation of a user who does not have a last visited page. A freshly created user has not visited any pages yet. How do I change the hbm mapping to make the userPrefs mapping optional?

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about java