File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "OneToOne Unidirectional Mapping - Extremely Weird Behavior" Watch "OneToOne Unidirectional Mapping - Extremely Weird Behavior" New topic
Author

OneToOne Unidirectional Mapping - Extremely Weird Behavior

Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 113
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.

Carlos.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 393
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: http://aspose.com/file-tools
 
subject: OneToOne Unidirectional Mapping - Extremely Weird Behavior
 
Similar Threads
OneToOne shared PK isn't setting key in related table during insertion
Help understanding when to use em.find and query manager and their ability to fetch nested objects
populating @OneToOne from query
Problem Persisting Entity In Hibernate
mapping One-to-many relationships using NetBeans and Not Hibernate