From: <fab...@us...> - 2008-07-23 16:39:20
|
Revision: 3655 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3655&view=rev Author: fabiomaulo Date: 2008-07-23 16:39:27 +0000 (Wed, 23 Jul 2008) Log Message: ----------- Merge r3654 (fix NH-1403) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Cfg/XmlHbmBinding/ClassBinder.cs trunk/nhibernate/src/NHibernate/Mapping/Any.cs trunk/nhibernate/src/NHibernate/NHibernate-2.0.csproj trunk/nhibernate/src/NHibernate/NHibernateUtil.cs trunk/nhibernate/src/NHibernate/Type/TypeType.cs trunk/nhibernate/src/NHibernate.DomainModel/FooBar.hbm.xml trunk/nhibernate/src/NHibernate.Test/Legacy/FooBarTest.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test-2.0.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate/Type/ClassMetaType.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Female.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Fixture.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Hobby.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Male.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Mappings.hbm.xml trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Person.cs Modified: trunk/nhibernate/src/NHibernate/Cfg/XmlHbmBinding/ClassBinder.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Cfg/XmlHbmBinding/ClassBinder.cs 2008-07-23 15:38:48 UTC (rev 3654) +++ trunk/nhibernate/src/NHibernate/Cfg/XmlHbmBinding/ClassBinder.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -819,40 +819,33 @@ XmlAttribute metaAttribute = node.Attributes["meta-type"]; if (metaAttribute != null) { - IType metaType = TypeFactory.HeuristicType(metaAttribute.Value); - if (metaType == null) - throw new MappingException("could not interpret meta-type"); - model.MetaType = metaType.Name; - - IDictionary<object, string> values = new Dictionary<object, string>(); - foreach (XmlNode metaValue in node.SelectNodes(HbmConstants.nsMetaValue, namespaceManager)) - try - { - object value = ((IDiscriminatorType) metaType).StringToObject(metaValue.Attributes["value"].Value); - string entityName = GetClassName(metaValue.Attributes["class"].Value, mappings); - values[value] = entityName; - } - catch (InvalidCastException) - { - throw new MappingException("meta-type was not an IDiscriminatorType: " + metaType.Name); - } - catch (HibernateException he) - { - throw new MappingException("could not interpret meta-value", he); - } - catch (TypeLoadException cnfe) - { - throw new MappingException("meta-value class not found", cnfe); - } - - if (values.Count > 0) + model.MetaType = metaAttribute.Value; + XmlNodeList metaValues = node.SelectNodes(HbmConstants.nsMetaValue, namespaceManager); + if (metaValues != null && metaValues.Count > 0) { - model.MetaValues = values; + IDictionary<object, string> values = new Dictionary<object, string>(); + IType metaType = TypeFactory.HeuristicType(model.MetaType); + foreach (XmlNode metaValue in metaValues) + try + { + object value = ((IDiscriminatorType)metaType).StringToObject(metaValue.Attributes["value"].Value); + string entityName = GetClassName(metaValue.Attributes["class"].Value, mappings); + values[value] = entityName; + } + catch (InvalidCastException) + { + throw new MappingException("meta-type was not an IDiscriminatorType: " + metaType.Name); + } + catch (HibernateException he) + { + throw new MappingException("could not interpret meta-value", he); + } + catch (TypeLoadException cnfe) + { + throw new MappingException("meta-value class not found", cnfe); + } + model.MetaValues = values.Count > 0 ? values : null; } - else - { - model.MetaValues = null; - } } BindColumns(node, model, isNullable, false, null); Modified: trunk/nhibernate/src/NHibernate/Mapping/Any.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/Any.cs 2008-07-23 15:38:48 UTC (rev 3654) +++ trunk/nhibernate/src/NHibernate/Mapping/Any.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -40,7 +40,7 @@ type = new AnyType( metaValues == null - ? TypeFactory.HeuristicType(metaTypeName) + ? ("class".Equals(metaTypeName) ? new ClassMetaType(): TypeFactory.HeuristicType(metaTypeName)) : new MetaType(metaValues, TypeFactory.HeuristicType(metaTypeName)), TypeFactory.HeuristicType(identifierTypeName)); } @@ -70,4 +70,4 @@ set { metaValues = value; } } } -} \ No newline at end of file +} Modified: trunk/nhibernate/src/NHibernate/NHibernate-2.0.csproj =================================================================== --- trunk/nhibernate/src/NHibernate/NHibernate-2.0.csproj 2008-07-23 15:38:48 UTC (rev 3654) +++ trunk/nhibernate/src/NHibernate/NHibernate-2.0.csproj 2008-07-23 16:39:27 UTC (rev 3655) @@ -1028,6 +1028,7 @@ <Compile Include="Type\AnsiCharType.cs" /> <Compile Include="Type\AnyType.cs" /> <Compile Include="Type\AbstractCharType.cs" /> + <Compile Include="Type\ClassMetaType.cs" /> <Compile Include="Type\CollectionType.cs" /> <Compile Include="Type\CustomCollectionType.cs" /> <Compile Include="Type\EmbeddedComponentType.cs" /> Modified: trunk/nhibernate/src/NHibernate/NHibernateUtil.cs =================================================================== --- trunk/nhibernate/src/NHibernate/NHibernateUtil.cs 2008-07-23 15:38:48 UTC (rev 3654) +++ trunk/nhibernate/src/NHibernate/NHibernateUtil.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -221,6 +221,12 @@ public static readonly NullableType Class = new TypeType(); /// <summary> + /// NHibernate class meta type for associtiation of kind <code>any</code>. + /// </summary> + /// <seealso cref="AnyType"/> + public static readonly IType ClassMetaType = new ClassMetaType(); + + /// <summary> /// NHibernate serializable type /// </summary> public static readonly NullableType Serializable = new SerializableType(); @@ -526,4 +532,4 @@ } } } -} \ No newline at end of file +} Added: trunk/nhibernate/src/NHibernate/Type/ClassMetaType.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Type/ClassMetaType.cs (rev 0) +++ trunk/nhibernate/src/NHibernate/Type/ClassMetaType.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -0,0 +1,125 @@ +using System; +using System.Data; +using System.Xml; +using NHibernate.Engine; +using NHibernate.SqlTypes; + +namespace NHibernate.Type +{ + /// <summary> + /// ClassMetaType is a NH specif type to support "any" with meta-type="class" + /// </summary> + /// <remarks> + /// It work like a MetaType where the key is the entity-name it self + /// </remarks> + [Serializable] + public class ClassMetaType : AbstractType + { + public override SqlType[] SqlTypes(IMapping mapping) + { + return new SqlType[] { NHibernateUtil.String.SqlType }; + } + + public override int GetColumnSpan(IMapping mapping) + { + return 1; + } + + public override System.Type ReturnedClass + { + get { return typeof (string); } + } + + public override object NullSafeGet(IDataReader rs, string[] names, ISessionImplementor session, object owner) + { + return NullSafeGet(rs, names[0], session, owner); + } + + public override object NullSafeGet(IDataReader rs,string name,ISessionImplementor session,object owner) + { + int index = rs.GetOrdinal(name); + + if (rs.IsDBNull(index)) + { + return null; + } + else + { + string str = (string) NHibernateUtil.String.Get(rs, index); + return string.IsNullOrEmpty(str) ? null : str; + } + } + + public override void NullSafeSet(IDbCommand st, object value, int index, bool[] settable, ISessionImplementor session) + { + if (settable[0]) NullSafeSet(st, value, index, session); + } + + public override void NullSafeSet(IDbCommand st,object value,int index,ISessionImplementor session) + { + if (value == null) + { + ((IDataParameter)st.Parameters[index]).Value = DBNull.Value; + } + else + { + NHibernateUtil.String.Set(st, value, index); + } + } + + public override string ToLoggableString(object value, ISessionFactoryImplementor factory) + { + return ToXMLString(value, factory); + } + + public override string Name + { + get { return "ClassMetaType"; } + } + + public override object DeepCopy(object value, EntityMode entityMode, ISessionFactoryImplementor factory) + { + return value; + } + + public override bool IsMutable + { + get { return false; } + } + + public override bool IsDirty(object old, object current, bool[] checkable, ISessionImplementor session) + { + return checkable[0] && IsDirty(old, current, session); + } + + public override object FromXMLNode(XmlNode xml, IMapping factory) + { + return FromXMLString(xml.Value, factory); + } + + public object FromXMLString(string xml, IMapping factory) + { + return xml; //xml is the entity name + } + + public override object Replace(object original, object current, ISessionImplementor session, object owner, System.Collections.IDictionary copiedAlready) + { + return original; + } + + public override void SetToXMLNode(XmlNode node, object value, ISessionFactoryImplementor factory) + { + node.Value = ToXMLString(value, factory); + } + + public override bool[] ToColumnNullness(object value, IMapping mapping) + { + throw new NotSupportedException(); + } + + public string ToXMLString(object value, ISessionFactoryImplementor factory) + { + return (string)value; //value is the entity name + } + } +} Modified: trunk/nhibernate/src/NHibernate/Type/TypeType.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Type/TypeType.cs 2008-07-23 15:38:48 UTC (rev 3654) +++ trunk/nhibernate/src/NHibernate/Type/TypeType.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -90,7 +90,7 @@ /// </remarks> public override void Set(IDbCommand cmd, object value, int index) { - NHibernateUtil.String.Set(cmd, ((System.Type)value).FullName, index); + NHibernateUtil.String.Set(cmd, ((System.Type)value).AssemblyQualifiedName, index); } /// <summary> @@ -101,7 +101,7 @@ /// <returns>An Xml formatted string that contains the Assembly Qualified Name.</returns> public override string ToString(object value) { - return ((System.Type)value).FullName; + return ((System.Type)value).AssemblyQualifiedName; } /// <summary> @@ -134,4 +134,4 @@ } } } -} \ No newline at end of file +} Modified: trunk/nhibernate/src/NHibernate.DomainModel/FooBar.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.DomainModel/FooBar.hbm.xml 2008-07-23 15:38:48 UTC (rev 3654) +++ trunk/nhibernate/src/NHibernate.DomainModel/FooBar.hbm.xml 2008-07-23 16:39:27 UTC (rev 3655) @@ -197,7 +197,7 @@ <element column="date_" type="DateTime"/> </array> </component> - <any name="Object" id-type="Int64" cascade="all"> + <any name="Object" meta-type="class" id-type="Int64" cascade="all"> <!-- made clazz 200 instead of 100 because of all the extra info stored such as assembly, key, culture Modified: trunk/nhibernate/src/NHibernate.Test/Legacy/FooBarTest.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Legacy/FooBarTest.cs 2008-07-23 15:38:48 UTC (rev 3654) +++ trunk/nhibernate/src/NHibernate.Test/Legacy/FooBarTest.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -5129,7 +5129,7 @@ s = OpenSession(); IList list = s.CreateQuery("from Bar bar where bar.Object.id = ? and bar.Object.class = ?") - .SetParameter(0, oid, NHibernateUtil.Int64).SetParameter(1, typeof(One), NHibernateUtil.Class).List(); + .SetParameter(0, oid, NHibernateUtil.Int64).SetParameter(1, typeof(One).FullName, NHibernateUtil.ClassMetaType).List(); Assert.AreEqual(1, list.Count); // this is a little different from h2.0.3 because the full type is stored, not Copied: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Female.cs (from rev 3654, branches/2.0.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Female.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Female.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Female.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -0,0 +1,9 @@ +namespace NHibernate.Test.NHSpecificTest.NH1403 +{ + public class Female : Person + { + public Female() {} + + public Female(string name) : base(name) {} + } +} \ No newline at end of file Copied: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Fixture.cs (from rev 3654, branches/2.0.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Fixture.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Fixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Fixture.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -0,0 +1,40 @@ +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.NH1403 +{ + [TestFixture] + public class Fixture : BugTestCase + { + [Test] + public void Bug() + { + Hobby h = new Hobby("Develop software"); + Person p = new Male("Diego"); + h.Person = p; + Hobby h1 = new Hobby("Drive Car"); + Person p1 = new Female("Luciana"); + h1.Person = p1; + object savedIdMale; + object saveIdFemale; + using (ISession s = OpenSession()) + using(ITransaction t = s.BeginTransaction()) + { + savedIdMale = s.Save(h); + saveIdFemale = s.Save(h1); + t.Commit(); + } + + using (ISession s = OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + h = s.Get<Hobby>(savedIdMale); + h1 = s.Get<Hobby>(saveIdFemale); + Assert.IsTrue(h.Person is Male); + Assert.IsTrue(h1.Person is Female); + s.Delete(h); + s.Delete(h1); + t.Commit(); + } + } + } +} \ No newline at end of file Property changes on: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Fixture.cs ___________________________________________________________________ Added: svn:mergeinfo + Copied: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Hobby.cs (from rev 3654, branches/2.0.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Hobby.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Hobby.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Hobby.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -0,0 +1,34 @@ +namespace NHibernate.Test.NHSpecificTest.NH1403 +{ + public class Hobby + { + private int id; + private string name; + private Person person; + + public Hobby() {} + + public Hobby(string name) : this() + { + this.name = name; + } + + public virtual int Id + { + get { return id; } + set { id = value; } + } + + public virtual string Name + { + get { return name; } + set { name = value; } + } + + public virtual Person Person + { + get { return person; } + set { person = value; } + } + } +} \ No newline at end of file Copied: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Male.cs (from rev 3654, branches/2.0.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Male.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Male.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Male.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -0,0 +1,9 @@ +namespace NHibernate.Test.NHSpecificTest.NH1403 +{ + public class Male : Person + { + public Male() {} + + public Male(string name) : base(name) {} + } +} \ No newline at end of file Copied: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Mappings.hbm.xml (from rev 3654, branches/2.0.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Mappings.hbm.xml) =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Mappings.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Mappings.hbm.xml 2008-07-23 16:39:27 UTC (rev 3655) @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + assembly="NHibernate.Test" + namespace="NHibernate.Test.NHSpecificTest.NH1403"> + + <class name="Person"> + <id name="Id"> + <generator class="native" /> + </id> + + <discriminator column="Gender" type="String" /> + + <property name="Name" /> + + <subclass name="Female" discriminator-value="Female"> + </subclass> + + <subclass name="Male" discriminator-value="Male"> + </subclass> + </class> + + <class name="Hobby"> + <id name="Id"> + <generator class="native" /> + </id> + + <property name="Name" /> + <any name="Person" meta-type="class" id-type="int" cascade="all"> + <column name="ORIGINAL_CLASS"/> + <column name="ORIGINAL_ID"/> + </any> + </class> +</hibernate-mapping> Copied: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Person.cs (from rev 3654, branches/2.0.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Person.cs) =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Person.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1403/Person.cs 2008-07-23 16:39:27 UTC (rev 3655) @@ -0,0 +1,27 @@ +namespace NHibernate.Test.NHSpecificTest.NH1403 +{ + public class Person + { + private int id; + private string name; + + public Person() {} + + public Person(string name) : this() + { + this.name = name; + } + + public virtual int Id + { + get { return id; } + set { id = value; } + } + + public virtual string Name + { + get { return name; } + set { name = value; } + } + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test-2.0.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test-2.0.csproj 2008-07-23 15:38:48 UTC (rev 3654) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test-2.0.csproj 2008-07-23 16:39:27 UTC (rev 3655) @@ -388,6 +388,11 @@ <Compile Include="NHSpecificTest\NH1355\CustomVersionType.cs" /> <Compile Include="NHSpecificTest\NH1355\UserTypeTimestamp.cs" /> <Compile Include="NHSpecificTest\NH1399\Fixture.cs" /> + <Compile Include="NHSpecificTest\NH1403\Female.cs" /> + <Compile Include="NHSpecificTest\NH1403\Fixture.cs" /> + <Compile Include="NHSpecificTest\NH1403\Hobby.cs" /> + <Compile Include="NHSpecificTest\NH1403\Male.cs" /> + <Compile Include="NHSpecificTest\NH1403\Person.cs" /> <Compile Include="NHSpecificTest\NH280\Fixture.cs" /> <Compile Include="NHSpecificTest\NH280\Foo.cs" /> <Compile Include="NHSpecificTest\NH1018\Employee.cs" /> @@ -1364,6 +1369,7 @@ <ItemGroup> <EmbeddedResource Include="DynamicEntity\Interceptor\Customer.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> + <EmbeddedResource Include="NHSpecificTest\NH1403\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1253\Mappings.hbm.xml" /> <EmbeddedResource Include="EntityModeTest\Map\Basic\ProductLine.hbm.xml" /> <EmbeddedResource Include="DynamicEntity\Tuplizer\Customer.hbm.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |