All of my classes are being mapped with Not.LazyLoad(), if that is part of the issue.
So I figured out a fix to my previous problem, but it was ugly and I would still like to know if anyone has a better solution. More detail, I was trying to construct a configuration like so:
return Fluently.Configure()
.Database(SQLiteConfiguration.Standard
.UsingFile(DbFile))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ConsensusTargetMap>().Conventions.Add<CascadeConvention>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<TargetMap> ().Conventions.Add<CascadeConvention>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
and had a ClassMap like this:
public class TargetMap : ClassMap<Target>
{
public TargetMap()
{
Not.LazyLoad();
Id(x => x.Id).GeneratedBy.Assigned();
Map(x => x.Foo1);
Map(x => x.Foo2);
Map(x => x.Foo3);
Map(x => x.Foo4);
//References(x => x.Parent);;
References(x => x.Type1).ForeignKey("Some_Field");
References(x => x.Type2).ForeignKey("Some_Other_Field");
References(x => x.Type3).ForeignKey("Yet_Another_Field");
//References(x => x.Parent).ForeignKey("Item_In_Parent's_List").Not.LazyLoad();
}
}
Type1, Type2, and Type 3 are objects contained within the Target class, and I want them put into tables of their own, in which they are also unique by a particular field(s). I want to be able to insert all of them when I insert the Target. If multiple Targets share a relationship to a Type, I want to reference the existing one, not create a new one for each Target.
As for the issue with the Parent, Target is a Child of Consensus Target, which has a HasMany relationship with Target. How can I create a reference back to the Parent from the Child? Or should I even bother? I ended up just pulling the objects out from the Target and putting them in lists where they were unique, and then essentially reversing the relationship from Child ManyToOne to Parent HasMany, but I was really hoping for a cleaner way to do this. TL; DR: Is there any way for NHibernate to insert objects that are unique by a particular field? I have a class that contains objects in a ManyToOne relationship, and these objects do not store the information about the object that References them, so how can I insert them properly?