Permalink
Browse files

Merge r3654 (fix NH-1403)

SVN: trunk@3655
  • Loading branch information...
1 parent 4b4fde6 commit e3e25d9e2cb9390e84dea0a5facc23bb3acb54e0 @fabiomaulo fabiomaulo committed Jul 23, 2008
@@ -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
@@ -5129,7 +5129,7 @@ public void Any()
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
@@ -0,0 +1,9 @@
+namespace NHibernate.Test.NHSpecificTest.NH1403
+{
+ public class Female : Person
+ {
+ public Female() {}
+
+ public Female(string name) : base(name) {}
+ }
+}
@@ -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();
+ }
+ }
+ }
+}
@@ -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; }
+ }
+ }
+}
@@ -0,0 +1,9 @@
+namespace NHibernate.Test.NHSpecificTest.NH1403
+{
+ public class Male : Person
+ {
+ public Male() {}
+
+ public Male(string name) : base(name) {}
+ }
+}
@@ -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>
@@ -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; }
+ }
+ }
+}
@@ -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" />
@@ -819,39 +819,32 @@ protected void BindAny(XmlNode node, Any model, bool isNullable)
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.MetaValues = values;
- }
- else
+ model.MetaType = metaAttribute.Value;
+ XmlNodeList metaValues = node.SelectNodes(HbmConstants.nsMetaValue, namespaceManager);
+ if (metaValues != null && metaValues.Count > 0)
{
- model.MetaValues = null;
+ 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;
}
}
@@ -40,7 +40,7 @@ public override IType Type
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 @@ public virtual string MetaType
set { metaValues = value; }
}
}
-}
+}
@@ -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" />
@@ -221,6 +221,12 @@ public static IType GuessType(System.Type type)
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 @@ public static bool IsPropertyInitialized(object proxy, string propertyName)
}
}
}
-}
+}
Oops, something went wrong.

0 comments on commit e3e25d9

Please sign in to comment.