nhibernate使用引用删除批处理

时间:2012-08-22 03:06:48

标签: nhibernate hql

termInfo类

public class TermInfo
{
    public TermInfo()
    {
        this.Breviary = string.Empty;
        this.CreateBy = string.Empty;
        this.CreateDate = DateTime.Now;
        this.Group = 0;
        this.Name = string.Empty;
    }

    public override int GetHashCode()

    public virtual int TermId { get; set; }

    public virtual string Name { get; set; }

    public virtual string Breviary { get; set; }

    public virtual int Group { get; set; }

    public virtual string CreateBy { get; set; }

    public virtual DateTime CreateDate { get; set; }

    public virtual IList<Taxonomy> Taxonomies { get; set; }
}

分类法

public class Taxonomy
{
    public Taxonomy()
    {
        this.TermTaxonomy = string.Empty;
        this.Description = string.Empty;
        this.ParentId = 0;
        this.OrderId = 0;
        this.PostCount = 0;
    }

    public virtual int TaxonomyId { get; set; }

    public virtual string TermTaxonomy { get; set; }

    public virtual string Description { get; set; }

    public virtual int ParentId { get; set; }

    public virtual int OrderId { get; set; }

    public virtual int PostCount { get; set; }

    public virtual TermInfo Term { get; set; }
}

映射

internal class TaxonomyMap :  ClassMap<Taxonomy>
{
    public TaxonomyMap()
    {
        Table("Smart_TermTaxonomy");
        Id(x=>x.TaxonomyId).GeneratedBy.Identity();
        Map(x => x.Description);
        Map(x => x.OrderId);
        Map(x => x.ParentId);
        Map(x => x.PostCount);
        Map(x => x.TermTaxonomy).Column("Taxonomy");


        References(x => x.Term, "TermId");

        DynamicUpdate();
        DynamicInsert();
    }
}

internal class TermInfoMap : ClassMap<TermInfo>
{
    public TermInfoMap()
    {
        Table("Smart_PostTerms");
        Id(x=>x.TermId).GeneratedBy.Identity();
        Map(x => x.Breviary);
        Map(x => x.CreateBy);
        Map(x => x.CreateDate);
        Map(x => x.Group).Column("[Group]");
        Map(x => x.Name).Column("[Name]");

        HasMany<Taxonomy>(x => x.Taxonomies).KeyColumn("TermId");
    }
}

我的问题是如何通过term.Name删除分类法 我期待的sql将是:

  

'从smart_postTaxonomy中删除termId =(从中选择termId)   smart_PostTerms,其中[Name] = @ Name)'

那么,我该怎么写hql查询字符串呢?

1 个答案:

答案 0 :(得分:4)

您是否尝试在HQL中执行类似操作?

session.CreateQuery(
    "delete from Taxonomy t where t.Term.id = " +
    "  (select ti.id from TermInfo ti where ti.Name = :name)")
    .SetParameter("name", termName)
    .ExecuteUpdate();