Hi,

 

I have a problem when mapping an entity, containing a collection of value 
objects, where the collection has map-semantics, and each value object 
(composite element) have a reference to another entity that is also used as the 
key of the collection. 

 

EntityRoot: Entity with collection of value objects

ValueObjectChild: Value object with reference to EntityKey

EntityKey: Other entity used as key in indexed collection of an EntityRoot 
object.

 

The question is: Is it possible to both use the other entity (EntityKey) as the 
key of collection elements, and as the target of a many-to-one association for 
each value of collection elements, without having two separate FK columns in 
the link table in the database?

 

Okay lets see some code and mappings….Lets start with mappings

 

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"

                                                                                
          assembly="FirstSolution"

                                                                                
          namespace="FirstSolution.Domain">

                      <class name="EntityRoot">

                                            <id name="Id">

                                                                 <generator 
class="hilo"/>

                                            </id>

                                            <map name="Children" 
cascade="save-update" access="field.camelcase-underscore">

                                                                 <key 
column="EntityRoot_Id"/>

                                                                 
<index-many-to-many column="EntityKey_Id" class="EntityKey"/>

                                                                 
<composite-element class="ValueObjectChild">

                                                                                
       <!-- 

                                                                                
       How can I map a reference to the same entity used as key for this 
collection of 

                                                                                
       values without having two FK constraints (that is using the same FK 
column-name 

                                                                                
       as used in the index-many-to-many declaration)???? 

                                                                                
       -->

                                                                                
       <many-to-one name="KeyEntity" not-null="true"/> <-- Have to use this 
one, but creates a duplicate FK

                                                                                
       <!-- 

                                                                                
       The following uncommented line will generate a throw of:

                                                                                
                             'MappingException : Repeated column in mapping for 
collection:....' 

                                                                                
       -->

                                                                                
       <!--<many-to-one name="KeyEntity" column="EntityKey_Id" 
not-null="true"/>--> <-- would like this, to reuse FK, and have reference and 
key point to identical entities

                                                                                
       <property name="Qty" not-null="true"/>

                                                                 
</composite-element>

                                            </map>

                      </class>

 

                      <class name="EntityKey">

                                            <id name="Id">

                                                                 <generator 
class="hilo"/>

                                            </id>

                      </class>

                      

</hibernate-mapping>

 

 

Kind regards

Maxild

 

P.S.

 

The classes are not that interesting, but here they are for completeness

 

namespace FirstSolution.Domain

{

                      public class EntityKey : EntityBase<EntityKey, long>

                      {

                      }

 

                      public class EntityRoot : EntityBase<EntityRoot, long>

                      {

                                            private readonly 
IDictionary<EntityKey, ValueObjectChild> _children;

                                            public virtual 
IEnumerable<ValueObjectChild> Children

                                            {

                                                                 get { return 
_children.Values; }

                                            }

 

                                            public virtual void 
Add(ValueObjectChild child)

                                            {

                                                                 
_children.Add(child.KeyEntity, child);

                                            }

                      }

 

                      public class ValueObjectChild : 
IEquatable<ValueObjectChild>

                      {

                                            protected ValueObjectChild() {}

 

                                            public ValueObjectChild(EntityKey 
other)

                                            {

                                                                 if (other == 
null)

                                                                 {

                                                                                
       throw new ArgumentNullException("other");

                                                                 }

                                                                 

                                                                 KeyEntity = 
other;

                                            }

 

                                            public virtual bool 
Equals(ValueObjectChild other)

                                            {

                                                                 if (other == 
null) return false;

                                                                 return 
other.KeyEntity.Equals(KeyEntity);

                                            }

 

                                            public virtual EntityKey KeyEntity 
{ get; private set; }

                                            public virtual int Qty { get; set; }

                      }

}

 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to