Hi!
We are doing kind of investigation/functionality testing to determine whether Hibernate is an appropriate tool for our needs. In the course of our tests we've run into problems with one-to-one relationships.
The specific issue we are having is the precise semantical meanings of the constrained="true" option.
The documentation says:
Quote:
specifies that a foreign key constraint on the primary key of the mapped table references the table of the associated class. This option affects the order in which save() and delete() are cascaded, and determines whether the association may be proxied (it is also used by the schema export tool).
This is fine and understandable.
HOWEVER, I'm getting reports from my team that when constrained="true" is used together with "foreign" id generator (as per documentation and desired mapping), then Hibernate ignores changes to the "parent" property of the "child" object. I.e. with the mapping outlined below (and assumming that Contract1 with ContractExtension1 are already saved and committed), the following code will have absolutely
no effect on database:
Code:
ContractExtension1.setContract(Contract2); // Attempting to re-parent ContractExtension.
It is as if inverse="true" is implied by the constrained="true".
QUESTION1: Is inverse="true" implied by constrained="true"? Does constrained="true" have any other semantical and/or functional implications?
All things considered, it makes sense for constrained="true" to also imply inverse="true". In this kind of association, re-parenting would mean changing the ID of persistent entity, which is not a good thing (tm). However the documentation makes no mention of this, so I'm wondering whether this is intended this way or is it a problem (either in our code or Hibernate).
QUESTION2: If inverse="true" is indeed implied by constrained="true", then does it mean that there's no way to re-parent "child" entity (i.e. ContractExtension)?
Again, it would make sense if so, but I'd very much like to know for sure whether it is explicitly intended this way.
QUESTION3: Is it possible to map one-to-one relationship as mandatory?
With the given mapping nothing prevents creation of the Contract object without ContractExtension object. This is committed and saved in database without an error (and you can't very well put database constraints to that effect). Of course it is easy to prevent on Java object level, but I'm just wondering if there's mapping that would enforce that constraint?
Hibernate version:3.0.1
Mapping documents (relevant section):Contract:Code:
<class name="Contract">
<id name="id" column="CONTRACT_PK" type="long" unsaved-value="null">
<generator class="sequence">
<param name="sequence">CONTRACT_SEQ</param>
</generator>
</id>
<one-to-one name="extension" cascade="all-delete-orphan"/>
</class>
Contract extension:Code:
<class name="ContractExtension">
<id name="id" column="CONTRACTEXTENSION_PK" type="long" unsaved-value="null">
<generator class="foreign">
<param name="property">contract</param>
</generator>
</id>
<one-to-one name="contract" constrained="true"/>
</class>
Thanks in advance for your time!