[ http://jira.andromda.org/browse/NETCART-2?page=comments#action_12709 ] 

Naresh Bhatia commented on NETCART-2:
-------------------------------------

Additional comment by Manish Agrawal on this issue 
(http://galaxy.andromda.org/forum/viewtopic.php?t=2161)

Problem summary: 
1-to-1 and 1-to-0..1 relationships both generate a foreign key at the primary 
end of the relationship along with a unique constraint on the foreign key 
column. In addition, in the 1-to-0..1 case, the foreign key column is marked 
nullable. 
However, a nullable column with a unique constraint allows only one row with a 
null value in MS-SQL server. This effectively allows only one row with a 
1-to-0..1 relationship and all other rows must have a 1-to-1 relationship. 


Proposed solution: 
In case of a 1-to-0..1 relationship, the template should generate a foreign key 
at the non-primary end and mark the column with not null and unique 
constraints. 
Is there some tag value or setting that I can change in the model to get the 
desired behaviour? 

If not, an extra if condition is needed in the following template code: 

hibernate.hbm.xml.vm 
#if ($sourceEnd.one2One) 
--- The code below checks if the sourceEnd is the primary then it will generate 
a <many-to-one> mapping (except when other class is using foreign id generator) 
#if($sourceEnd.one2OnePrimary || (!$sourceEnd.navigable && 
!$entity.foreignHibernateGeneratorClass)) 
#if ($otherEnd.type.foreignHibernateGeneratorClass) 
<one-to-one name="$otherEnd.name" 
class="$otherEnd.type.fullyQualifiedEntityImplementationName" #if 
($otherEnd.hibernateCascade) 
cascade="$otherEnd.hibernateCascade"#end#if($otherEnd.lazy) 
lazy="proxy" fetch="select"#else lazy="false" fetch="$otherEnd.outerJoin"#end/> 
#else 
<many-to-one name="$otherEnd.name" 
class="$otherEnd.type.fullyQualifiedEntityImplementationName" 
foreign-key="$otherEnd.foreignKeyConstraintName"#if 
($otherEnd.hibernateCascade) 
cascade="$otherEnd.hibernateCascade"#end#if($otherEnd.columnIndex) 
index="$otherEnd.columnIndex"#end#if($otherEnd.lazy) lazy="proxy" 
fetch="select"#else lazy="false" fetch="$otherEnd.outerJoin"#end> 
<column name="$otherEnd.columnName" not-null="$otherEnd.required" 
sql-type="$otherEnd.sqlType" unique="true"/> 
</many-to-one> 
#end 

#else 
--- The code below is executed when generating mapping for the class on the 
non-primary end 
#if ($entity.foreignHibernateGeneratorClass) 
<one-to-one name="$otherEnd.name" 
class="$otherEnd.type.fullyQualifiedEntityImplementationName" 
foreign-key="$otherEnd.foreignKeyConstraintName"#if 
($otherEnd.hibernateCascade) cascade="$otherEnd.hibernateCascade"#end 
constrained="true"#if($otherEnd.lazy) lazy="proxy" fetch="select"#else 
lazy="false" 
fetch="$otherEnd.outerJoin"#end/> 
#else 
<one-to-one name="$otherEnd.name" 
class="$otherEnd.type.fullyQualifiedEntityImplementationName" #if 
($sourceEnd.navigable) property-ref="$sourceEnd.name"#end#if 
($otherEnd.hibernateCascade) 
cascade="$otherEnd.hibernateCascade"#end#if($otherEnd.lazy) lazy="proxy" 
fetch="select"#else lazy="false" fetch="$otherEnd.outerJoin"#end/> 
#end 
#end 
#end 

HibernateAssociationEndLogicImpl.java 
protected boolean handleIsOne2OnePrimary() 
{ 
return (this.isOne2One() && (this.isAggregation() || this.isComposition())); 
} 

AssociationEndFacadeLogicImpl.java 
protected boolean handleIsOne2One() 
{ 
return !this.isMany() && !this.getOtherEnd().isMany(); 
} 
protected boolean handleIsMany() 
{ 
boolean isMany = false; 
final Multiplicity multiplicity = this.metaObject.getMultiplicity(); 
// we'll say a null multiplicity is 1 
if (multiplicity != null) 
{ 
final Collection ranges = multiplicity.getRange(); 
if (ranges != null && !ranges.isEmpty()) 
{ 
final Iterator rangeIt = ranges.iterator(); 
while (rangeIt.hasNext()) 
{ 
final MultiplicityRange multiplicityRange = (MultiplicityRange)rangeIt.next(); 
final int upper = multiplicityRange.getUpper(); 
isMany = upper > 1 || upper < 0; 
} 
} 
} 
return isMany; 
} 

> unidirectional reflexive association, problem with "unique"
> -----------------------------------------------------------
>
>          Key: NETCART-2
>          URL: http://jira.andromda.org/browse/NETCART-2
>      Project: .NET Cartridges
>         Type: Bug

>  Environment: A n d r o M D A  -  3.2-RC1-20060125041715
>     Reporter: EricR
>     Assignee: Naresh Bhatia

>
> I have an unidirectional reflexive association 1-to-(0..1). the association 
> end got the rolename "previous" and is navigable.
> the generated mapping file contains :
> <many-to-one name="Previous" class="nGEFHib.Domain.MyClassImpl, nGEFHibCore" 
> foreign-key="MY_CLASS_PREVIOUS_FKC" outer-join="auto">
>             <column name="PREVIOUS_FK" not-null="false" 
> sql-type="NUMERIC(19,0)" unique="true"/>
>  </many-to-one>
> The constraint "unique=true" is an error, since null is allowed in the 
> foreign key and that produces an SQL runtime error.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://jira.andromda.org/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642

Reply via email to