|
From: <fab...@us...> - 2010-10-12 16:15:22
|
Revision: 5245
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5245&view=rev
Author: fabiomaulo
Date: 2010-10-12 16:15:16 +0000 (Tue, 12 Oct 2010)
Log Message:
-----------
Fix NH-2374 (thanks to Remco Ros for the investigation, the test, the port and the patch)
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Id/ForeignGenerator.cs
trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
Added Paths:
-----------
trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2374/
trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2374/Mappings.hbm.xml
trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2374/NH2374Fixture.cs
Modified: trunk/nhibernate/src/NHibernate/Id/ForeignGenerator.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Id/ForeignGenerator.cs 2010-10-12 05:41:30 UTC (rev 5244)
+++ trunk/nhibernate/src/NHibernate/Id/ForeignGenerator.cs 2010-10-12 16:15:16 UTC (rev 5245)
@@ -39,30 +39,41 @@
/// </returns>
public object Generate(ISessionImplementor sessionImplementor, object obj)
{
- ISession session = (ISession)sessionImplementor;
+ ISession session = (ISession) sessionImplementor;
- object associatedObject = sessionImplementor.Factory
- .GetClassMetadata(obj.GetType())
- .GetPropertyValue(obj, propertyName, sessionImplementor.EntityMode);
+ var persister = sessionImplementor.Factory.GetEntityPersister(entityName);
+ object associatedObject = persister.GetPropertyValue(obj, propertyName, sessionImplementor.EntityMode);
if (associatedObject == null)
{
throw new IdentifierGenerationException("attempted to assign id from null one-to-one property: " + propertyName);
}
- EntityType type = (EntityType)sessionImplementor.Factory.GetClassMetadata(obj.GetType()).GetPropertyType(propertyName);
+ EntityType foreignValueSourceType;
+ IType propertyType = persister.GetPropertyType(propertyName);
+ if (propertyType.IsEntityType)
+ {
+ foreignValueSourceType = (EntityType) propertyType;
+ }
+ else
+ {
+ // try identifier mapper
+ foreignValueSourceType = (EntityType) persister.GetPropertyType("_identifierMapper." + propertyName);
+ }
object id;
try
{
- id = ForeignKeys.GetEntityIdentifierIfNotUnsaved(type.GetAssociatedEntityName(), associatedObject, sessionImplementor);
+ id = ForeignKeys.GetEntityIdentifierIfNotUnsaved(
+ foreignValueSourceType.GetAssociatedEntityName(),
+ associatedObject,
+ sessionImplementor);
}
catch (TransientObjectException)
{
- id = session.Save(associatedObject);
+ id = session.Save(foreignValueSourceType.GetAssociatedEntityName(), associatedObject);
}
-
if (session.Contains(obj))
{
//abort the save (the object is already saved by a circular cascade)
Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2374/Mappings.hbm.xml
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2374/Mappings.hbm.xml (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2374/Mappings.hbm.xml 2010-10-12 16:15:16 UTC (rev 5245)
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
+ assembly="NHibernate.Test"
+ namespace="NHibernate.Test.NHSpecificTest.NH2374">
+ <class entity-name="Parent">
+ <id name="Id" type="System.Int32">
+ <generator class="increment"/>
+ </id>
+ <many-to-one name="Child" entity-name="Child" cascade="all-delete-orphan" />
+ </class>
+ <class entity-name="Child">
+ <id name="Id" type="System.Int32">
+ <generator class="foreign">
+ <param name="property">Parent</param>
+ </generator>
+ </id>
+ <one-to-one name="Parent" entity-name="Parent" />
+ </class>
+</hibernate-mapping>
Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2374/NH2374Fixture.cs
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2374/NH2374Fixture.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH2374/NH2374Fixture.cs 2010-10-12 16:15:16 UTC (rev 5245)
@@ -0,0 +1,90 @@
+using System.Collections;
+using NUnit.Framework;
+
+namespace NHibernate.Test.NHSpecificTest.NH2374
+{
+ [TestFixture]
+ public class NH2374Fixture : BugTestCase
+ {
+ [Test]
+ public void OneToOne_with_EntityMode_Map()
+ {
+ int id;
+
+ using (ISession sroot = OpenSession())
+ {
+ using (ISession s = sroot.GetSession(EntityMode.Map))
+ {
+ using (ITransaction t = s.BeginTransaction())
+ {
+ var parent = new Hashtable();
+ var child = new Hashtable
+ {
+ {"Parent", parent}
+ };
+
+ parent["Child"] = child;
+
+ id = (int) s.Save("Parent", parent);
+ s.Flush();
+
+ t.Commit();
+ }
+ }
+ }
+
+ using (ISession s = OpenSession())
+ {
+ using (ITransaction t = s.BeginTransaction())
+ {
+ var p = s.Get("Parent", id) as IDictionary;
+
+ Assert.That(p["Child"], Is.Not.Null);
+
+ s.Delete("Parent", p);
+
+ t.Commit();
+ }
+ }
+ }
+
+ [Test]
+ public void OneToOne_with_EntityMode_Poco()
+ {
+ int id;
+
+ using (ISession s = OpenSession())
+ {
+ using (ITransaction t = s.BeginTransaction())
+ {
+ var parent = new Hashtable();
+ var child = new Hashtable
+ {
+ {"Parent", parent}
+ };
+
+ parent["Child"] = child;
+
+ id = (int) s.Save("Parent", parent);
+ s.Flush();
+
+ t.Commit();
+ }
+ }
+
+ using (ISession s = OpenSession())
+ {
+ using (ITransaction t = s.BeginTransaction())
+ {
+ var p = s.Get("Parent", id) as IDictionary;
+
+ Assert.That(p["Child"], Is.Not.Null);
+
+ s.Delete("Parent", p);
+
+ t.Commit();
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj
===================================================================
--- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-10-12 05:41:30 UTC (rev 5244)
+++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-10-12 16:15:16 UTC (rev 5245)
@@ -528,6 +528,7 @@
<Compile Include="NHSpecificTest\NH2344\Fixture.cs" />
<Compile Include="NHSpecificTest\NH2344\Model.cs" />
<Compile Include="NHSpecificTest\NH2361\Fixture.cs" />
+ <Compile Include="NHSpecificTest\NH2374\NH2374Fixture.cs" />
<Compile Include="PolymorphicGetAndLoad\Domain.cs" />
<Compile Include="PolymorphicGetAndLoad\PolymorphicGetAndLoadTest.cs" />
<Compile Include="TypesTest\CharClass.cs" />
@@ -2328,6 +2329,7 @@
<EmbeddedResource Include="CollectionTest\NullableValueTypeElementMapFixture.hbm.xml" />
<EmbeddedResource Include="DriverTest\EntityForMs2008.hbm.xml" />
<Content Include="DynamicEntity\package.html" />
+ <EmbeddedResource Include="NHSpecificTest\NH2374\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2328\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2313\Mappings.hbm.xml" />
<EmbeddedResource Include="TypesTest\UriClass.hbm.xml" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|