Conform and Any Mapping

20 views
Skip to first unread message

Mario Dal Lago

unread,
Aug 4, 2011, 5:26:03 PM8/4/11
to codec...@googlegroups.com
hello, i'm migrating from conform 1.0.0.0 with nh 2.1.0.1001 to conform 2.0.0.0 with nh 3.2.0.4000 and the generated mapping are different.

these are the classes:

public interface IRule : IEntity
    {
        bool Evaluate(object instance);
    }

public abstract class AbstractRule : Entity, IRule
    {
        public virtual string PropertyName { get; protected set; }
        public virtual string ExpectedValue { get; protected set; }
        public virtual Type Type { get; protected set; }
}


public class AndRule : Entity, IRule
    {
        protected AndRule()
        {
        }

        public virtual IRule LeftRule { get; protected set; }
        public virtual IRule RigthRule { get; protected set; }

    }


public class EqualsRule : AbstractRule
    {
        protected EqualsRule()
        {
        }

    }


public class RulesSet : Entity
    {
        public virtual string Description { get; set; }
        public virtual IRule ParentRule { get; set; }
        public virtual Type RootType { get; set; }
    }

i'm, using this code to configure nh


IEnumerable<Type> mappings = new List<Type>
                                             {
                                                 typeof (RulesSet),
                                                 typeof (AbstractRule),
                                                 typeof (IRule),
                                                 typeof (AndRule),
                                                 typeof (EqualsRule)
                                             };

            var orm = new ObjectRelationalMapper();
            orm.TablePerClass(new[] { typeof(RulesSet) });
            orm.TablePerClassHierarchy(new[] { typeof(IRule) });
            orm.Cascade<RulesSet, IRule>(CascadeOn.All);

            var mapper = new Mapper(orm);
            
            mapper.Class<IRule>(pc => pc.Table("Rules"));
            
            mapper.Class<AndRule>(pc =>
                                      {
                                          pc.ManyToOne(x => x.LeftRule, mom =>
                                                                            {
                                                                                mom.Cascade(Cascade.Persist);
                                                                                mom.Lazy(LazyRelation.NoLazy);
                                                                            });

                                          pc.ManyToOne(x => x.RigthRule, mom =>
                                                                             {
                                                                                 mom.Lazy(LazyRelation.NoLazy);
                                                                                 mom.Cascade(Cascade.Persist);
                                                                             });
                                      });

           
            mapper.Class<RulesSet>(pc => pc.ManyToOne(r => r.ParentRule, mom => mom.Lazy(LazyRelation.NoLazy)));
            

            return mapper.CompileMappingFor(mappings);


with Conform 1.0 this is the generated mapping :


<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsi=" http://www.w3.org/2001/ XMLSchema-instance" xmlns:xsd= "http://www.w3.org/2001/ XMLSchema" assembly=" RulesEngine.Core" xmlns="urn: nhibernate-mapping-2.2">
  <class name="RulesEngine.Core.Rules.RulesSet">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <property name="Description" />
    <many-to-one name="ParentRule" cascade="all" lazy="false" />
    <property name="RootType" />
  </class>
  <class name="RulesEngine.Core.Rules.IRule" table="Rules" abstract="true">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <discriminator />
  </class>
  <subclass name="RulesEngine.Core.Rules.AbstractRule" extends="RulesEngine.Core.Rules.IRule">
    <property name="PropertyName" />
    <property name="ExpectedValue" />
    <property name="Type" />
  </subclass>
  <subclass name="RulesEngine.Core.Rules.AndRule" extends="RulesEngine.Core.Rules.IRule">
    <many-to-one name="LeftRule" cascade="save-update, persist" lazy="false" />
    <many-to-one name="RigthRule" cascade="save-update, persist" lazy="false" />
  </subclass>

  <subclass name="RulesEngine.Core.Rules.Impl.EqualsRule" extends="RulesEngine.Core.Rules.AbstractRule" />
</hibernate-mapping>

and with conform 2.0 :

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" assembly="RulesEngine.Core" xmlns="urn:nhibernate-mapping-2.2">
  <class name="RulesEngine.Core.Rules.RulesSet">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <property name="Description" />
    <any id-type="Int32" name="ParentRule">
      <column name="ParentRuleClass" />
      <column name="ParentRuleId" />
  & nbsp; </any>
    <property name="RootType" />
  </class>
  <class name="RulesEngine.Core.Rules.IRule" table="Rules" abstract="true">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <discriminator />
  </class>
  <subclass name="RulesEngine.Core.Rules.AbstractRule" extends="RulesEngine.Core.Rules.IRule">
    <property name="PropertyName" />
    <property name="ExpectedValue" />
    <property name="Type" />
  </subclass>
  <subclass name="RulesEngine.Core.Rules.AndRule"  ;extends="RulesEngine.Core.Rules.IRule">
    <any id-type="Int32" name="LeftRule">
      <column name="LeftRuleClass" />
      <column name="LeftRuleId" />
    </any>
    <any id-type="Int32" name="RigthRule">
      <column name="RigthRuleClass" />
      <column name="RigthRuleId" />
    </any>
  </subclass>
 

  <subclass name="RulesEngine.Core.Rules.Impl.EqualsRule" extends="RulesEngine.Core.Rules.AbstractRule" />

</hibernate-mapping>


the problem is the many to one changed to Any mapping in addition didn't mantein the cascade and lazy

thanks in advance


Mario Dal Lago

Fabio Maulo

unread,
Aug 4, 2011, 5:39:22 PM8/4/11
to codec...@googlegroups.com
The 2.0 mapping is the correct since IRule is implemented by two different hierarchy :
AbstractRule and AndRule

To have a real many-to-one both AbstractRule and AndRule should be part of the same hierarchy of concrete classes.

btw you can remove the Polymorphic pack.
--
Fabio Maulo

Mario Dal Lago

unread,
Aug 4, 2011, 5:54:46 PM8/4/11
to codec...@googlegroups.com
oh, ok... you are rigth...

i'll try to remove the polymorphic pack and test again.

thank you very much!


Mario Dal Lago


--- El jue 4-ago-11, Fabio Maulo <fabio...@gmail.com> escribió:

Mario Dal Lago

unread,
Aug 6, 2011, 8:34:50 PM8/6/11
to codec...@googlegroups.com
hello

i resolved the issue cleaning the HeterogeneousAssociations patterns collection with this line of code:

orm.Patterns.HeterogeneousAssociations.Clear();



Mario Dal Lago


De: Mario Dal Lago <the4...@yahoo.com.ar>
Para: codec...@googlegroups.com
Enviado: jueves, 4 de agosto de 2011 18:54
Reply all
Reply to author
Forward
0 new messages