Thanks Alex. I do think we should get something into the FNH code base
for this as the approach of relying on cascades for saving the
children rather than saving them individually is a pretty common one.

2009/12/16 Alexander Groß <[email protected]>:
> Hi Peter,
>
> I must admit that I never wrote a PersistenceSpecification involving a
> relation like to one you describe.
>
> I guess the problem can be solved by just not calling
> TransactionalSave on your artists but rather let NH save the artists
> along with the Session. Right off the top of my head, here's what you
> can to let the ReferenceList skip the TransactionalSave:
>
> 1. Derive a class from ReferenceList
> 2. Override HasRegistered in that class and let it do nothing
> 3. Add an extension method
> PersistenceSpecification<T>.CheckListWithoutSavingTheItems with the
> same signature as CheckList
> 4. CheckListWithoutSavingTheItems would have to instantiate the class
> created in step 1 and register it on the PersistenceSpecification
>
> This will cause your overridden "no-op" HasRegistered method to be
> called, allowing the setter delegate to run. What also might be needed
> is a custom equality comparer for Artist. ReferenceList knows about
> artists with a unassigned PK, whereas the artists that are read from
> the DB got a PK assigned.
>
> All of the steps above can be implemented in your code so you don't
> need to touch FNH. If it works, I think the FNH team will be happy to
> integrate your new check.
>
> Hope this helps,
>
> Alex
>
> On 9 Dez., 12:37, Peter <[email protected]> wrote:
>> First the caveats:
>>
>> 1) This is a follow-on 
>> fromhttp://groups.google.com/group/fluent-nhibernate/browse_thread/thread...
>> (didn't want to hijack the post)
>> 2) I'm new to nhibernate so please forgive me saying stupid things
>>
>> I have exactly the same problem as described by Cristian at the top of
>> this post. However, the CheckList extension method still fails when
>> you use it to test an inverse on-to-many with the child FK column set
>> to non-nullable. A PropertyValueException is thrown: "not-null
>> property references a null or transient value XXX"
>>
>> For example:
>>
>> public class ArtistMap : ClassMap<Artist>
>> {
>>     public ArtistMap()
>>     {
>>         [...]
>>         References(a => a.Genre)
>>             .Column("GenreID")
>>             .Not.Nullable();
>>         }
>>     }
>>
>> }
>>
>> public class GenreMap : ClassMap<Genre>
>> {
>>     public GenreMap()
>>     {
>>         [...]
>>         HasMany(g => g.Artists)
>>             .KeyColumn("GenreID")
>>             .Cascade.All()
>>             .Inverse();
>>         }
>>     }
>>
>> }
>>
>> [TestClass]
>> public class GenreTests : DatabaseTest
>> {
>>     [TestMethod]
>>     public void ShouldMapCorrectly()
>>     {
>>         List<Artist> artists = new List<Artist>() {
>>                 new Artist { Name = "Artist1" },
>>                 new Artist { Name = "Artist2" }
>>         };
>>
>>         new PersistenceSpecification<Genre>(Session, new Comparer())
>>             .CheckProperty(g => g.Id, 1)
>>             .CheckProperty(g => g.Name, "Genre")
>>             .CheckList(g => g.Artists, artists, (g, a) => g.AddArtist
>> (a))
>>             .VerifyTheMappings();
>>     }
>>
>> }
>>
>> Here's a loose call-stack of what happens in the code
>>
>> 1: CheckList
>> 2:     -> PersistenceSpecification.RegisterCheckedProperty
>> 3:         -> ReferenceList.HasRegistered
>> 4:             -> PersistenceSpecification.TransactionalSave
>> 5: VerifyTheMappings
>> 6:     -> InstantiateUsingParameterlessConstructor
>> 7:         -> Genre.AddArtist
>>
>> So, before fluent gets anywhere close to invoking my setter delegate
>> (7) it first persists the new artists object (4). As the artist type
>> is configured to have a genre FK that is not nullable, the test fails
>> at (4) and throws the 'transient object' exception.
>>
>> Does this mean that CheckList is only good for unidirectional
>> associations or associations where the FK column is nullable? If there
>> is a nullable FK column, then the test should be re-written the long
>> way to manually persist the objects, reload them and verify the values
>> are correct?
>
> --
>
> You received this message because you are subscribed to the Google Groups 
> "Fluent NHibernate" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/fluent-nhibernate?hl=en.
>
>
>

--

You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.


Reply via email to