File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes OneToOne Unidirectional Mapping - Extremely Weird Behavior Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "OneToOne Unidirectional Mapping - Extremely Weird Behavior" Watch "OneToOne Unidirectional Mapping - Extremely Weird Behavior" New topic

OneToOne Unidirectional Mapping - Extremely Weird Behavior

Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 131
Hi all,

I am finding a situation which I am literrally freaking out with.

I have a OTO unidirectional mapping between two classes: Contract and AttachedFile. Contract is the owner of the relationship and is mapped as:

@OneToOne(cascade=CascadeType.ALL) @JoinColumn(name="scan_id") AttachedFile scan = null;

The weird behavior arises when attaching the file, in the setter method:

The weird thing is, that af is supposed to be a new instance of the AttachedFile class. But what I find out, is that if I debug, inside this method before the assignation is made, af doesn't have its 'id' attribute set (before the assignation it is null), but once the assignation is done, the id member of the AttachedFile instance af is indeed populated. So it seems Hibernate populates this data automatically.

This is leading me to the problem of ConstraintViolationException's being triggered when saving the Contract instance, since the id created for the AttachedFile instance doesn't exist in the database and the ForeignKey check complains.

Has anybody got an explanation? Shouldn't the id be created when persisting or merging the contract instance insttead?

Any answers are very welcome.

thanks for your time.

H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 471
If possible, can you post the code for Contract and AttachedFile.
and the code where/how Contract and AttachedFile are created and persisted.
I agree. Here's the link:
subject: OneToOne Unidirectional Mapping - Extremely Weird Behavior
It's not a secret anymore!