From: <fab...@us...> - 2010-09-25 15:24:50
|
Revision: 5216 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5216&view=rev Author: fabiomaulo Date: 2010-09-25 15:24:43 +0000 (Sat, 25 Sep 2010) Log Message: ----------- Example for NH-2324 (only to show the right syntax) Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/BulkUpdateWithCustomCompositeType.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/CompositeData.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/CompositeUserType.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/Entity.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/Mappings.hbm.xml Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/BulkUpdateWithCustomCompositeType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/BulkUpdateWithCustomCompositeType.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/BulkUpdateWithCustomCompositeType.cs 2010-09-25 15:24:43 UTC (rev 5216) @@ -0,0 +1,60 @@ +using System; +using NUnit.Framework; +using SharpTestsEx; + +namespace NHibernate.Test.NHSpecificTest.NH2324 +{ + public class BulkUpdateWithCustomCompositeType: BugTestCase + { + public class Scenario: IDisposable + { + private readonly ISessionFactory factory; + + public Scenario(ISessionFactory factory) + { + this.factory = factory; + using (ISession s = factory.OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + var e = new Entity + { + Data = new CompositeData {DataA = new DateTime(2010, 1, 1), DataB = new DateTime(2010, 2, 2)} + }; + s.Save(e); + t.Commit(); + } + } + + public void Dispose() + { + using (ISession s = factory.OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + s.CreateQuery("delete from Entity").ExecuteUpdate(); + t.Commit(); + } + } + } + + [Test] + public void ShouldAllowBulkupdateWithCompositeUserType() + { + using (new Scenario(Sfi)) + { + string queryString = @"update Entity m set m.Data.DataA = :dataA, m.Data.DataB = :dataB"; + + using (ISession s = OpenSession()) + using (ITransaction t = s.BeginTransaction()) + { + var query = s.CreateQuery(queryString) + .SetDateTime("dataA", new DateTime(2010, 3, 3)) + .SetDateTime("dataB", new DateTime(2010, 4, 4)); + + query.Executing(q => q.ExecuteUpdate()).NotThrows(); + + t.Commit(); + } + } + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/CompositeData.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/CompositeData.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/CompositeData.cs 2010-09-25 15:24:43 UTC (rev 5216) @@ -0,0 +1,10 @@ +using System; + +namespace NHibernate.Test.NHSpecificTest.NH2324 +{ + public class CompositeData + { + public virtual DateTime DataA { get; set; } + public virtual DateTime DataB { get; set; } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/CompositeUserType.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/CompositeUserType.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/CompositeUserType.cs 2010-09-25 15:24:43 UTC (rev 5216) @@ -0,0 +1,225 @@ +using System; +using System.Data; +using NHibernate.Engine; +using NHibernate.Type; +using NHibernate.UserTypes; + +namespace NHibernate.Test.NHSpecificTest.NH2324 +{ + public class CompositeUserType : ICompositeUserType + { + #region ICompositeUserType Members + + /// <summary> + /// Compare two instances of the class mapped by this type for persistence + /// "equality", ie. equality of persistent state. + /// </summary> + /// <param name="x"></param> + /// <param name="y"></param> + /// <returns></returns> + public new bool Equals(object x, object y) + { + if (ReferenceEquals(x, y)) + { + return true; + } + else if (x == null || + y == null) + { + return false; + } + else + { + return x.Equals(y); + } + } + + /// <summary> + /// Get the "property names" that may be used in a query. + /// </summary> + /// <value></value> + public string[] PropertyNames + { + get + { + return new[] + { + "DataA", + "DataB" + }; + } + } + + /// <summary> + /// Get the corresponding "property types" + /// </summary> + /// <value></value> + public IType[] PropertyTypes + { + get + { + return new IType[] + { + NHibernateUtil.DateTime, + NHibernateUtil.DateTime + }; + } + } + + /// <summary> + /// Get the value of a property + /// </summary> + /// <param name="component">an instance of class mapped by this "type"</param> + /// <param name="property"></param> + /// <returns>the property value</returns> + public object GetPropertyValue(object component, int property) + { + var p = (CompositeData) component; + if (property == 0) + { + return p.DataA; + } + else + { + return p.DataB; + } + } + + /// <summary> + /// Set the value of a property + /// </summary> + /// <param name="component">an instance of class mapped by this "type"</param> + /// <param name="property"></param> + /// <param name="value">the value to set</param> + public void SetPropertyValue(object component, int property, object value) + { + throw new NotImplementedException(); + } + + /// <summary> + /// The class returned by NullSafeGet(). + /// </summary> + /// <value></value> + public System.Type ReturnedClass + { + get { return typeof (CompositeData); } + } + + /// <summary> + /// Get a hashcode for the instance, consistent with persistence "equality" + /// </summary> + /// <param name="x"></param> + /// <returns></returns> + public int GetHashCode(object x) + { + return x.GetHashCode(); + } + + /// <summary> + /// Retrieve an instance of the mapped class from a IDataReader. Implementors + /// should handle possibility of null values. + /// </summary> + /// <param name="dr">IDataReader</param> + /// <param name="names">the column names</param> + /// <param name="session"></param> + /// <param name="owner">the containing entity</param> + /// <returns></returns> + public object NullSafeGet(IDataReader dr, string[] names, ISessionImplementor session, object owner) + { + var data = new CompositeData(); + data.DataA = (DateTime) NHibernateUtil.DateTime.NullSafeGet(dr, new[] {names[0]}, session, owner); + data.DataB = (DateTime) NHibernateUtil.DateTime.NullSafeGet(dr, new[] {names[1]}, session, owner); + + return data; + } + + /// <summary> + /// Write an instance of the mapped class to a prepared statement. + /// Implementors should handle possibility of null values. + /// A multi-column type should be written to parameters starting from index. + /// </summary> + /// <param name="cmd"></param> + /// <param name="value"></param> + /// <param name="index"></param> + /// <param name="session"></param> + public void NullSafeSet(IDbCommand cmd, object value, int index, ISessionImplementor session) + { + if (value == null) + { + ((IDataParameter) cmd.Parameters[index]).Value = DBNull.Value; + ((IDataParameter) cmd.Parameters[index + 1]).Value = DBNull.Value; + } + else + { + var data = (CompositeData) value; + NHibernateUtil.DateTime.Set(cmd, data.DataA, index); + NHibernateUtil.DateTime.Set(cmd, data.DataB, index + 1); + } + } + + /// <summary> + /// Return a deep copy of the persistent state, stopping at entities and at collections. + /// </summary> + /// <param name="value">generally a collection element or entity field</param> + /// <returns></returns> + public object DeepCopy(object value) + { + return value; + } + + /// <summary> + /// Are objects of this type mutable? + /// </summary> + /// <value></value> + public bool IsMutable + { + get { return true; } + } + + /// <summary> + /// Transform the object into its cacheable representation. + /// At the very least this method should perform a deep copy. + /// That may not be enough for some implementations, method should perform a deep copy. That may not be enough for some implementations, however; for example, associations must be cached as identifier values. (optional operation) + /// </summary> + /// <param name="value">the object to be cached</param> + /// <param name="session"></param> + /// <returns></returns> + public object Disassemble(object value, ISessionImplementor session) + { + return DeepCopy(value); + } + + /// <summary> + /// Reconstruct an object from the cacheable representation. + /// At the very least this method should perform a deep copy. (optional operation) + /// </summary> + /// <param name="cached">the object to be cached</param> + /// <param name="session"></param> + /// <param name="owner"></param> + /// <returns></returns> + public object Assemble(object cached, ISessionImplementor session, object owner) + { + return DeepCopy(cached); + } + + /// <summary> + /// During merge, replace the existing (target) value in the entity we are merging to + /// with a new (original) value from the detached entity we are merging. For immutable + /// objects, or null values, it is safe to simply return the first parameter. For + /// mutable objects, it is safe to return a copy of the first parameter. However, since + /// composite user types often define component values, it might make sense to recursively + /// replace component values in the target object. + /// </summary> + /// <param name="original"></param> + /// <param name="target"></param> + /// <param name="session"></param> + /// <param name="owner"></param> + /// <returns></returns> + public object Replace(object original, object target, ISessionImplementor session, object owner) + { + return original; + } + + #endregion + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/Entity.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/Entity.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/Entity.cs 2010-09-25 15:24:43 UTC (rev 5216) @@ -0,0 +1,9 @@ +namespace NHibernate.Test.NHSpecificTest.NH2324 +{ + public class Entity + { + public virtual long Id { get; set; } + + public virtual CompositeData Data { get; set; } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/Mappings.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/Mappings.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2324/Mappings.hbm.xml 2010-09-25 15:24:43 UTC (rev 5216) @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + namespace="NHibernate.Test.NHSpecificTest.NH2324" + assembly="NHibernate.Test"> + + <class name="Entity"> + <id name="Id"> + <generator class="hilo" > + <param name="max_lo">100</param> + </generator> + </id> + + <property name="Data" type="CompositeUserType"> + <column name="DataA"/> + <column name="DataB"/> + </property> + </class> + +</hibernate-mapping> \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-09-24 13:45:53 UTC (rev 5215) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-09-25 15:24:43 UTC (rev 5216) @@ -501,6 +501,10 @@ <Compile Include="NHSpecificTest\NH2322\Fixture.cs" /> <Compile Include="NHSpecificTest\NH2322\Model.cs" /> <Compile Include="NHSpecificTest\NH2322\PostUpdateEventListener.cs" /> + <Compile Include="NHSpecificTest\NH2324\CompositeData.cs" /> + <Compile Include="NHSpecificTest\NH2324\CompositeUserType.cs" /> + <Compile Include="NHSpecificTest\NH2324\Entity.cs" /> + <Compile Include="NHSpecificTest\NH2324\BulkUpdateWithCustomCompositeType.cs" /> <Compile Include="NHSpecificTest\NH2344\Fixture.cs" /> <Compile Include="NHSpecificTest\NH2344\Model.cs" /> <Compile Include="TypesTest\CharClass.cs" /> @@ -2297,6 +2301,7 @@ <EmbeddedResource Include="CollectionTest\NullableValueTypeElementMapFixture.hbm.xml" /> <EmbeddedResource Include="DriverTest\EntityForMs2008.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> + <EmbeddedResource Include="NHSpecificTest\NH2324\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2344\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2112\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2147\Mappings.hbm.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |