NHibernate删除sql null检查问题

时间:2013-10-26 21:56:50

标签: sql nhibernate fluent-nhibernate

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

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;}
}

1 个答案:

答案 0 :(得分:0)

确实您使用的是set,然后按this documented limitation(如@ oguzh4n的pointed所示):

  

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

此限制将在NHibernate 5.2中删除,应在2018年第三季度提供。