Reputation: 4558
I have two classes and mapping for the collection:
class User
{
Guid ID;
string Name;
}
class Group
{
Guid ID;
string Name;
IList<User> Members;
}
// GroupMap
HasMany(x=>x.Members).Inverse().Cascade.AllDeleteOrhpan().etc.
This one works. When I add a User to the Members collection NHibernate cascades the operation. Same for deletes and updates.
Now I want to change my model a bit and this will change also the mapping. The collection is IList<Guid
>. What I really want is the cascade to remain. That means that I have to do some custom persister or IUserType. The mapping should tell the object type like HasMany(x=>x.Members) and the collection will hold the IDs
class User
{
Guid ID;
string Name;
}
class Group
{
Guid ID;
string Name;
IList<Guid> Members;
}
// GroupMap
HasMany<User>(x=>x.Members).Inverse().Cascade.AllDeleteOrhpan().etc.
Any ideas where can I start from? I think that there is no out of the box solution but who knows...
PS: NHib user group: https://groups.google.com/forum/#!topic/nhusers/pSUOaGxdxVM
Upvotes: 0
Views: 72
Reputation: 30813
For the 1st comment: The custom persiter or something else should take care about backreference.
so the first mapping doesnt really work already? You could map a virtual backreference on User and set it through an interceptor, i can post something if needed. However i hardly see any advantage.
For the 2nd: Group and User are two aggregate roots. I want to connect the two ARs only by their identity. The two ARs do not need any other information except the other's ID.
// readonly mapping
HasMany(x => x.Members).Column("Group_Id").Element("Id").Readonly();
I think you will run into a lot of subtle problems when you have a non readonly collection of guids pointing to Users. For example what sql should be generated for:
var user = new User { Id = Guid.New(), Name = "Joe", Age = 12 };
group.Members.Add(user.Id);
session.SaveOrUpdate(group);
session.Flush();
a) INSERT INTO Users (Id, Group_id) Values(...);
b) nothing
c) INSERT INTO Users (Id, Group_id, Name, Age) Values(...);
or this
group.Members.Remove(someGuid);
session.SaveOrUpdate(group);
session.Flush();
a) DELETE FROM Users WHERE group_id = 1;
b) DELETE FROM Users WHERE id = <guid>;
c) Update Users Set group_id = null WHERE Id = <guid>;
Upvotes: 1