How do you create an optional one-to-one mapping within the hibernate hbm file? For instance, guess that I've got a User along with a last_visited_page table. The consumer might possess a last_visited page. Here's my current one-to-one mapping within 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 mentioned example doesn't allow the development of a person who doesn't have a final visited page. A freshly produced user hasn't visited any pages yet. How do you alter the hbm mapping to create the userPrefs mapping optional?

To my understanding, Hibernate does not support optional one-to-one (see HHH-2007) so you will need to make use of a fake many-to-one with not-null="false" rather.

If your user has for the most part one furtherfrequented page, then you will find two cases :

(a) some given user doesn't have last_visited page, by which situation there won't be any tuple with this user within the lastfrequentedweb page table, (b) some given user has exactly one furtherfrequented page, by which situation you will see exactly one tuple with this user within the lastfrequentedweb page table.

Which should allow it to be apparent that userid is really a candidate type in your last-visited-page table.

Which should allow it to be apparent that you ought to report that answer to the DBMS.

I had been getting a simliar problem, but using annotations. Google introduced me here, therefore if other people finds themselves within the same sitatuions, this labored for me personally:

http://opensource.atlassian.com/projects/hibernate/browse/ANN-725

If you are using annotations, you should use the @NotFound(action=NotFoundAction.IGNORE) annotation to ensure that you do not get the best. Just make certain your code inspections for nulls since it is now is probably not there -)

Just spend the majority of the day today attempting to perform a similar factor, finally found the next solution (just in-situation this can be helpful for some individuals)

@OneToOne
@JoinColumn(name="ClassA_Id", referencedColumnName="ClassB_Id", nullable=true)

Hope that can help save somebody a while