I have a “Activity” entity, this entity can have several “Task”.
Here the code for the entities :
public class Activity : Entity<int>
{
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual Iesi.Collections.Generic.ISet<Task> Tasks { get; set; }
public Activity()
{
Tasks = new Iesi.Collections.Generic.HashedSet<Task>();
}
}
public class Task
{
public virtual int Id { get; set; }
public virtual string Code { get; set; }
public virtual string Name { get; set; }
public virtual Activity Activity { get; set; }
}
The mapping :
public ActivityMap()
{
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Code);
Map(x => x.Name);
HasMany(x => x.Tasks)
.KeyColumns.Add("Activity")
.AsSet()
.Inverse()
.Cascade.AllDeleteOrphan();
}
public TaskMap()
{
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Code);
Map(x => x.Name);
}
The test :
activity = new Activity
{
Code = "...",
Name = "..."
};
Task task = new Task
{
Code = "...",
Name ="...",
Activity = activity
};
session.Save(activity);
I see whit NHProf 2 insert.
After the commit, I do a GetById of the entity “Activity” but the “Tasks” property is empty. In the database, the field “Activity” (entity “Task”) is null.
You have to keep the relation between two objects in both sides.
P.S. maybe you should change the cascade to be
.Cascade.All();because when I tryed Delete orphan it just didn’t seem to work right.Update: as @Stefan Steinegger mentioned you didn’t Map the activity.
But you still need to update the relation in both sides.