I've already posted this in the Fluent NH group a long time ago, but didn't get any answers until today. SO, here's the problem: I've got a one-to-many relationship defined and the one side has the inverse flag set. the mapping code looks something like this:
public class MapeamentoReceita : ClassMap<Receita> {
public MapeamentoReceita() {
Table("Receitas");
Not.LazyLoad();
Id(rec => rec.Id, "IdReceita")
.GeneratedBy
.HiLo("TabelaHilo", "ProximoHi", "1000", "Tabela='receitas'")
.Default(0);
Version(rec => rec.Versao);
//other props go here
HasMany(rec => rec.Imagens)
.Access.CamelCaseField((Prefix.Underscore))
.AsBag()
.Cascade.All()
.KeyColumn("IdReceita")
.Not.LazyLoad()
.Inverse();
}
}
Now, Imagem's mapping looks like this:
public class MapeamentoImagem : ClassMap<Imagem> {
public MapeamentoImagem() {
Table("Imagens");
Not.LazyLoad();
Id(img => img.Id, "IdImagem")
.GeneratedBy
.HiLo("TabelaHiLo", "ProximoHi", "1000", "Tabela='imagens'")
.Default(0);
Map(img => img.Bytes)
.CustomSqlType("image")
.CustomType<Byte[]>()
.LazyLoad()
.Length(2000000000)
.Not.Nullable()
.Not.Update();
References(img => img.Receita)
.Column("IdReceita")
.Cascade.None();
}
}
And here's the code that tests the persistence of these classes:
new PersistenceSpecification<Receita>(sess)
.CheckList(rec => rec.Imagens,
_imagens,
(receita, imagem) => receita.AdicionaImagem(imagem))
.VerifyTheMappings();
Even though Inverse is "on", PersistenceSpecification tries to insert Imagem before inserting Receita. Since IdReceita is foreign key configured not to accept null, I end up with an exception. I've tried writing "real world code" that uses receita and it works (I've turned on SQL and I can see that in this case, Receita is inserted before Imagem as it should be).
Since nobody answered this question on FH group, I was wondering if someone can please confirm that this PersistenceSpecification behavior is a bug.
thanks.