0

当我调用“ task.Assignee.Clear() ”时,nhibernate 在 SQLLite 和 SQLServer 的 SQL 下创建(调用时在 Assignee 中的一条记录)

DELETE FROM TaskAssignees
WHERE  TaskId = 'd8e13c83-20b8-4ce6-a814-a264000c21a6' /* @p0 */
       AND UserId = 'f676b54b-d986-4659-a4aa-9103d928f91b' /* @p1 */
       AND TeamId = NULL /* @p2 */
       AND DepartmentId = NULL /* @p3 */
       AND VirtualUserId = NULL /* @p4 */
       AND IsMain = 1

这个查询没有执行,因为=NULL是对sql的错误检查,一定是这样的

DELETE FROM TaskAssignees
WHERE  TaskId = 'd8e13c83-20b8-4ce6-a814-a264000c21a6' /* @p0 */
       AND UserId = 'f676b54b-d986-4659-a4aa-9103d928f91b' /* @p1 */
       AND TeamId is NULL /* @p2 */
       AND DepartmentId is  NULL /* @p3 */
       AND VirtualUserId is  NULL /* @p4 */
       AND IsMain = 1

我该如何解决这个问题?

我使用 FluentNhibernate 进行映射,并使用 NHibernate 3.3

映射和类:

public TaskMap()
{
    Table("Tasks");
    CompositeId<TaskId>(x => x.Id).KeyProperty(t => t.Id);
    Map(x => x.Name).Not.Nullable();

    HasMany(x => x.Assignees)
        .AsList(c => c.Column("AssigneeIndex"))
        .Component(m =>
        {
            m.Component(x => x.UserId, mm => mm.Map(x => x.Id).Column("UserId").Nullable());
            m.Component(x => x.TeamId, mm => mm.Map(x => x.Id).Column("TeamId").Nullable());
            m.Component(x => x.DepartmentId, mm => mm.Map(x => x.Id).Column("DepartmentId").Nullable());
            m.Component(x => x.VirtualUserId, mm => mm.Map(x => x.Id).Column("VirtualUserId").Nullable());
            m.Map(x => x.IsMain).Not.Nullable();
        })
        .KeyColumn("TaskId")
        .Table("TaskAssignees")
        .Cascade.All();
}

public class Task : Entity<TaskId> 
{
    public TaskId Id {get;set;}
    public string Name {get;set;}
    public Iesi.Collections.Generic.ISet<TaskAssignee> Assignees {get;set;}
}

public class TaskAssignee : ValueObject
{
    public UserId UserId {get;protected set;}
    public TeamId TeamId {get;protected set;}
    public VirtualUserId VirtualUserId {get;protected set;}
    public DepartmentId DepartmentId {get;protected set;}
    public bool IsMain {get;protected set;}
}

public class TaskId : ValueObject
{
    public Guid Id {get;protected set;}
}

public class TeamId : ValueObject
{
    public Guid Id {get;protected set;}
}

public class VirtualUserId : ValueObject
{
    public Guid Id {get;protected set;}
}

public class DepartmentId : ValueObject
{
    public Guid Id {get;protected set;}
}

public class UserId : ValueObject
{
    public Guid Id {get;protected set;}
}
4

1 回答 1

0

当然,您使用的是set, 并达到了这个记录的限制(正如@oguzh4n所指出的那样):

请注意,如果您使用<set>. NHibernate 在删除对象时必须使用每个列的值来标识一条记录(复合元素表中没有单独的主键列),这对于空值是不可能的。您必须在复合元素中仅使用非空属性或选择 <list><map>或。<bag><idbag>

此限制将在 NHibernate 5.2 中删除,它应该在 2018 年第三季度可用。

于 2018-07-23T09:20:31.270 回答