NHibernate Many-to-one cascade

前端 未结 2 446
别跟我提以往
别跟我提以往 2020-12-19 07:47

I have the below two classes:

public class Project
{

    public virtual int ProjectId { get; set; }
    public virtual string ProjectName { get; set; }
             


        
2条回答
  •  不思量自难忘°
    2020-12-19 08:20

    The many-to-one cascade does not support all-delete-orphan, see:

    • 5.1.10. many-to-one

    Also, it would be almost impossible to handle this feature by NHibernate's session. Because it does not have to be clear, that the referenced many-to-one is really orphan. There should be some farther checks in DB... there could be other places referencing this table row...

    Suggestion: do it in your code as a part of the DAO or Business Facade implementation. Check if there are really no dependencies, and then issue explicit Delete()

    EXTEND: Here is a QueryOver syntax to get a list of all "orphan" LegalEntity

    // subquery
    var subquery = QueryOver.Of()
        .Select(x => x.LegalEntity.LegalEntId);
    
    // just these legal entities, which are NOT used
    var query = session.QueryOver()
        .WithSubquery
          .WhereProperty(y => y.LegalEntId)
          .NotIn(subquery)
        ;
    
    // orphans
    var list = query
        .List();
    

提交回复
热议问题