0

我有以下实体:项目、项目映射和用户,它是多对多这里是映射:

public ProjectMembershipMapping()
{
    Property(pm => pm.IsAccepted, prop => prop.NotNullable(true));
    Property(pm => pm.Permission, prop => prop.NotNullable(true));
    ManyToOne(pm => pm.Member, mapping =>
                                   {
                                       mapping.Lazy(LazyRelation.NoLazy);
                                       mapping.Cascade(Cascade.All);
                                   });
    ManyToOne(pm => pm.Project, mapping =>
                                    {
                                        mapping.Lazy(LazyRelation.NoLazy);
                                        mapping.Cascade(Cascade.All);
                                    }); 

    ComposedId(pm =>                                          
                   {
                       pm.ManyToOne(prop => prop.Member);
                       pm.ManyToOne(prop => prop.Project);
                   });
}

public ProjectMapping()
{   
    Set(proj => proj.Members,
            mapping =>
            {
                mapping.Lazy(CollectionLazy.NoLazy);
                mapping.Inverse(false);
                mapping.Cascade(Cascade.All);
                mapping.Table("ProjectMembership");
                mapping.Key(k => k.Column("Project"));
            },
            action => action.OneToMany()
        );      
}

public class DevcoopUserMapping : ClassMapping<DevcoopUser>
{
        Bag(user => user.ProjectMemberships, 
            mapping =>
                {
                    mapping.Lazy(CollectionLazy.NoLazy);
                    mapping.Inverse(false);
                    mapping.Cascade(Cascade.All);
                    mapping.Table("ProjectMembership");
                    mapping.Key(k => k.Column("Member"));
                },
            action => action.OneToMany());
}

用户可以是许多项目的成员。现在假设我要取消他对项目的订阅。只需通过 ISession 对象删除 ProjectMembership 对象就会抛出我:

deleted object would be re-saved by cascade (remove deleted object from associations)[devcoop.Daos.ProjectMembership#devcoop.Daos.ProjectMembership]

另一方面,当我第一次尝试从 Project 和 User 中删除这个 ProjectMembership 实例时(我按照建议从关联中删除它),我得到了异常:

could not delete collection: [devcoop.Daos.Project.Members#1][SQL: UPDATE ProjectMembership SET Project = null WHERE Project = @p0]

任何人都可以帮忙吗?据说我在映射周围搞砸了一些东西,但我不知道这是什么。

4

1 回答 1

0

在删除 ProjectMembership 实例之前,请将其从 DevcoopUser.ProjectMemberships- 和 Project.Members-collections 中删除。

我相信这应该有效:

  membership.Member.ProjectMemberships.Remove(membership);
  membership.Project.Members.remove(membership);
  session.Delete(membership);

而且我认为您应该从 ProjectMembership 映射中删除 CascadeAll 。我不完全确定,但我认为它会将删除级联到使用 CascadeAll 映射的集合中的所有成员和项目,您可能不希望发生这种情况。

于 2012-06-05T06:51:58.500 回答