NHibernate(HQL)有效地删除具有分层数据的对象树

时间:2014-03-14 15:13:32

标签: sql performance nhibernate hql

我正在实现一个执行一些复杂过程的程序,并将步骤保存到数据库中,用于监视进程。我的数据模型如下:

//Objects
public class Execution
{
    public Execution()
    {
        Jobs = new List<Job>();
    }

    public virtual Guid Id { get; set; }
    public virtual Guid ProcessId { get; set; }
    public virtual string Name { get; set; }
    public virtual DateTime SubmissionTime { get; set; }
    public virtual DateTime? StartTime { get; set; }
    public virtual DateTime? EndTime { get; set; }
    public virtual ExecutionStatus ExecutionStatus { get; set; }
    public virtual IList<Job> Jobs{ get; set; }
}

public class Job
{
    public Job()
    {
        Tasks = new List<Task>();
    }

    public virtual Guid Id { get; set; }
    public virtual DateTime? StartTime { get; set; }
    public virtual DateTime? EndTime { get; set; }
    public virtual IList<Task> Tasks { get; set; }
    public virtual JobStatus JobStatus { get; set; }
    public virtual string FailureInfo { get; set; }
}   

public class Task
{
    public Task()
    {
        SubTasks = new List<Task>();
    }

    public virtual TaskStatus TaskStatus { get; set; }
    public virtual TaskType TaskType { get; set; }
    public virtual DateTime? StartTime { get; set; }
    public virtual DateTime? EndTime { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Task> SubTasks { get; set; }
    public virtual int ProcessOrder { get; set; }
    public virtual string FailureInfo { get; set; }
}   


//Mappings  
public class ExecutionMap : ClassMap<Execution>
{
    public ExecutionMap()
    {
        Table("Executions");
        Id(entity => entity.Id).GeneratedBy.Assigned();
        Map(entity => entity.BookId);
        Map(entity => entity.Name);
        Map(entity => entity.SubmissionTime);
        HasMany(entity => entity.Jobs).KeyColumn("JobId").Cascade.All();
    }
}   

public class JobMap : ClassMap<Job>
{
    public JobMap()
    {
        Table("Jobs");
        Id(entity => entity.Id).GeneratedBy.Assigned();
        Map(entity => entity.StartTime);
        Map(entity => entity.EndTime);
        Map(entity => entity.JobStatus).CustomType<JobStatus>().CustomSqlType("INT");
        Map(entity => entity.LastChangeTime);
        Map(entity => entity.FailureInfo);
        HasMany(x => x.Tasks).KeyColumn("JobId").Cascade.All();
    }
}   

public class TaskMap : ClassMap<Task>
{
    public TaskMap()
    {
        Table("Tasks");
        Id(entity => entity.Id).GeneratedBy.Assigned();
        Map(entity => entity.StartTime);
        Map(entity => entity.EndTime);
        Map(entity => entity.Name);
        Map(entity => entity.TaskType).CustomType<TaskType>().CustomSqlType("INT");
        Map(entity => entity.TaskStatus).CustomType<TaskStatus>().CustomSqlType("INT");
        Map(entity => entity.ProcessOrder);
        Map(entity => entity.FailureInfo);
        HasMany(x => x.SubTasks).KeyColumn("ParentId").Cascade.All();
    }
}   

因此,上述代码的要点是:

执行有许多具有多个任务的作业,可以有子任务(目前只有一个级别)。

我要求您提供的是删除项目数据树的有效方法,无需加载项目数据树并逐个删除对象。

任何帮助将不胜感激。

谢谢,

克里斯

PS:现在我用这种方法删除对象:

public void DeleteWhereBookId(Guid procesId){

IList<Execution> executions = Session.QueryOver<Execution>()
                         .Where(entity => entity.BookId == procesId)
                         .List<Execution>();

foreach (var execution in executions)
{
    Session.Delete(execution);
}}

0 个答案:

没有答案