From: <jul...@us...> - 2011-01-24 16:17:47
|
Revision: 5368 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5368&view=rev Author: julian-maughan Date: 2011-01-24 16:17:41 +0000 (Mon, 24 Jan 2011) Log Message: ----------- Fixed minor deficiency whereby the value of a 'length' attribute on the property mapping of a serialiable type is ignored [ref. NH-2484]. Thanks to Ivan Zlatev. Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Mapping/SimpleValue.cs trunk/nhibernate/src/NHibernate/Type/SerializableType.cs trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Fixture.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Mappings.hbm.xml trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Model.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/food-photo.jpg Modified: trunk/nhibernate/src/NHibernate/Mapping/SimpleValue.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Mapping/SimpleValue.cs 2011-01-24 15:51:51 UTC (rev 5367) +++ trunk/nhibernate/src/NHibernate/Mapping/SimpleValue.cs 2011-01-24 16:17:41 UTC (rev 5368) @@ -247,18 +247,21 @@ { // NH different behavior // If the mapping has a type as "Double(10,5)" our SqlType will be created with all information - // including the rigth SqlType specification but when the length/presion/scale was specified - // trough attributes the SqlType is wrong (does not include length/presion/scale specification) + // including the right SqlType specification, but when the length/precision/scale was specified + // through attributes the SqlType is wrong (does not include length/precision/scale specification) IType result = null; if (ColumnSpan == 1 && !columns[0].IsFormula) { - var col = (Column) columns[0]; - if(col.IsLengthDefined()) + var col = (Column)columns[0]; + if (col.IsLengthDefined()) { result = TypeFactory.BuiltInType(typeName, col.Length); + + if (result == null) + result = TypeFactory.HeuristicType(typeName, typeParameters, col.Length); } - else if(col.IsPrecisionDefined()) + else if (col.IsPrecisionDefined()) { result = TypeFactory.BuiltInType(typeName, Convert.ToByte(col.Precision), Convert.ToByte(col.Scale)); } Modified: trunk/nhibernate/src/NHibernate/Type/SerializableType.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Type/SerializableType.cs 2011-01-24 15:51:51 UTC (rev 5367) +++ trunk/nhibernate/src/NHibernate/Type/SerializableType.cs 2011-01-24 16:17:41 UTC (rev 5368) @@ -14,8 +14,9 @@ /// </summary> /// <remarks> /// <para> - /// The SerializableType should be used when you know that Bytes are - /// not going to be greater than 8,000. + /// For performance reasons, the SerializableType should be used when you know that Bytes are + /// not going to be greater than 8,000. Implementing a custom type is recommended for larger + /// types. /// </para> /// <para> /// The base class is <see cref="MutableType"/> because the data is stored in Modified: trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs 2011-01-24 15:51:51 UTC (rev 5367) +++ trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs 2011-01-24 16:17:41 UTC (rev 5368) @@ -21,7 +21,7 @@ /// <remarks> /// Applications should use static methods and constants on NHibernate.NHibernateUtil if the default /// IType is good enough. For example, the TypeFactory should only be used when the String needs - /// to have a length of 300 instead of 255. At this point NHibernate.String does not get you the + /// to have a length of 300 instead of 255. At this point NHibernateUtil.String does not get you the /// correct IType. Instead use TypeFactory.GetString(300) and keep a local variable that holds /// a reference to the IType. /// </remarks> @@ -33,8 +33,8 @@ Length, PrecisionScale } + private static readonly string[] EmptyAliases= new string[0]; - private static readonly char[] PrecisionScaleSplit = new[] { '(', ')', ',' }; private static readonly char[] LengthSplit = new[] { '(', ')' }; private static readonly TypeFactory Instance; @@ -446,14 +446,25 @@ } /// <summary> - /// Uses heuristics to deduce a NHibernate type given a string naming the - /// type. + /// Uses heuristics to deduce a NHibernate type given a string naming the type. /// </summary> /// <param name="typeName">the type name</param> /// <param name="parameters">parameters for the type</param> /// <returns>An instance of <c>NHibernate.Type.IType</c></returns> public static IType HeuristicType(string typeName, IDictionary<string, string> parameters) { + return HeuristicType(typeName, parameters, null); + } + + /// <summary> + /// Uses heuristics to deduce a NHibernate type given a string naming the type. + /// </summary> + /// <param name="typeName">the type name</param> + /// <param name="parameters">parameters for the type</param> + /// <param name="length">optionally, the size of the type</param> + /// <returns></returns> + public static IType HeuristicType(string typeName, IDictionary<string, string> parameters, int? length) + { IType type = Basic(typeName); if (type == null) @@ -518,6 +529,10 @@ { type = GetSerializableType(typeClass, Int32.Parse(parsedTypeName[1])); } + else if (length != null) + { + type = GetSerializableType(typeClass, length.Value); + } else { type = GetSerializableType(typeClass); Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Fixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Fixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Fixture.cs 2011-01-24 16:17:41 UTC (rev 5368) @@ -0,0 +1,79 @@ +using System; +using System.Drawing; +using NUnit.Framework; +using NHibernate.Type; +using NHibernate.SqlTypes; + +namespace NHibernate.Test.NHSpecificTest.NH2484 +{ +[TestFixture] + public class Fixture : BugTestCase + { + protected override bool AppliesTo(NHibernate.Dialect.Dialect dialect) + { + return (dialect is Dialect.MsSql2008Dialect); + } + + [Test] + public void TestPersistenceOfClassWithUnknownSerializableType() + { + var stream = this.GetType().Assembly.GetManifestResourceStream("NHibernate.Test.NHSpecificTest.NH2484.food-photo.jpg"); + var image = Bitmap.FromStream(stream); + + var model = new ClassWithImage() { Image = image }; + var imageSize = model.Image.Size; + int id = -1; + + using (ISession session = OpenSession()) + { + session.SaveOrUpdate(model); + session.Flush(); + id = model.Id; + Assert.That(id, Is.GreaterThan(-1)); + } + using (ISession session = OpenSession()) + { + model = session.Get<ClassWithImage>(id); + Assert.That(model.Image.Size, Is.EqualTo(imageSize)); // Ensure type is not truncated + } + using (ISession session = OpenSession()) + { + session.CreateQuery("delete from ClassWithImage").ExecuteUpdate(); + session.Flush(); + } + + stream.Dispose(); + } + + [Test] + public void TestPersistenceOfClassWithSerializableType() + { + var stream = this.GetType().Assembly.GetManifestResourceStream("NHibernate.Test.NHSpecificTest.NH2484.food-photo.jpg"); + var image = Bitmap.FromStream(stream); + + var model = new ClassWithSerializableType() { Image = image }; + var imageSize = ((Image)model.Image).Size; + int id = -1; + + using (ISession session = OpenSession()) + { + session.SaveOrUpdate(model); + session.Flush(); + id = model.Id; + Assert.That(id, Is.GreaterThan(-1)); + } + using (ISession session = OpenSession()) + { + model = session.Get<ClassWithSerializableType>(id); + Assert.That(((Image)model.Image).Size, Is.EqualTo(imageSize)); // Ensure type is not truncated + } + using (ISession session = OpenSession()) + { + session.CreateQuery("delete from ClassWithSerializableType").ExecuteUpdate(); + session.Flush(); + } + + stream.Dispose(); + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Mappings.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Mappings.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Mappings.hbm.xml 2011-01-24 16:17:41 UTC (rev 5368) @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping + xmlns="urn:nhibernate-mapping-2.2" + assembly="NHibernate.Test" + namespace="NHibernate.Test.NHSpecificTest.NH2484"> + + <class name="ClassWithImage"> + + <id name="Id"> + <generator class="native" /> + </id> + + <property name="Image" type="System.Drawing.Image, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <column name="Image" length="10000" not-null="false" /> + </property> + + </class> + + <class name="ClassWithSerializableType"> + + <id name="Id"> + <generator class="native" /> + </id> + + <property name="Image" type="Serializable(10000)"> + <column name="Image" not-null="false" /> + </property> + + </class> + +</hibernate-mapping> Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Model.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Model.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/Model.cs 2011-01-24 16:17:41 UTC (rev 5368) @@ -0,0 +1,17 @@ +using System; +using System.Runtime.Serialization; + +namespace NHibernate.Test.NHSpecificTest.NH2484 +{ + public class ClassWithImage + { + public virtual int Id { get; set; } + public virtual System.Drawing.Image Image { get; set; } + } + + public class ClassWithSerializableType + { + public virtual int Id { get; set; } + public virtual ISerializable Image { get; set; } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/food-photo.jpg =================================================================== (Binary files differ) Property changes on: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2484/food-photo.jpg ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-01-24 15:51:51 UTC (rev 5367) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-01-24 16:17:41 UTC (rev 5368) @@ -91,6 +91,7 @@ </Reference> <Reference Include="System.Data" /> <Reference Include="System.Data.OracleClient" /> + <Reference Include="System.Drawing" /> <Reference Include="System.Linq.Dynamic, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\..\lib\net\3.5\System.Linq.Dynamic.dll</HintPath> @@ -608,6 +609,8 @@ <Compile Include="NHSpecificTest\NH2470\Class1Class2Tests.cs" /> <Compile Include="NHSpecificTest\NH2470\DomainObject.cs" /> <Compile Include="NHSpecificTest\NH2470\DTO.cs" /> + <Compile Include="NHSpecificTest\NH2484\Fixture.cs" /> + <Compile Include="NHSpecificTest\NH2484\Model.cs" /> <Compile Include="NHSpecificTest\Properties\CompositePropertyRefTest.cs" /> <Compile Include="NHSpecificTest\Properties\DynamicEntityTest.cs" /> <Compile Include="NHSpecificTest\Properties\Model.cs" /> @@ -1912,6 +1915,8 @@ <EmbeddedResource Include="NHSpecificTest\NH2409\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2420\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2441\Mappings.hbm.xml" /> + <EmbeddedResource Include="NHSpecificTest\NH2484\food-photo.jpg" /> + <EmbeddedResource Include="NHSpecificTest\NH2484\Mappings.hbm.xml" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\NHibernate.ByteCode.Castle\NHibernate.ByteCode.Castle.csproj"> @@ -2779,6 +2784,7 @@ <EmbeddedResource Include="DynamicEntity\Tuplizer\Customer.hbm.xml" /> </ItemGroup> <ItemGroup> + <Folder Include="NHSpecificTest\NH2484" /> <Folder Include="Properties\" /> </ItemGroup> <ItemGroup> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |