NHibernate Many-to-one cascade

前端 未结 2 444
别跟我提以往
别跟我提以往 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
    <many-to-one
        ...
        cascade="all|none|save-update|delete"              (4)
        ...
    

    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<Project>()
        .Select(x => x.LegalEntity.LegalEntId);
    
    // just these legal entities, which are NOT used
    var query = session.QueryOver<LegalEntity>()
        .WithSubquery
          .WhereProperty(y => y.LegalEntId)
          .NotIn(subquery)
        ;
    
    // orphans
    var list = query
        .List<LegalEntity>();
    
    0 讨论(0)
  • 2020-12-19 08:34

    Now all-delete-orphan and delete-orphan have been implemented for many-to-one as you can see in this commit from Nov 19, 2014.

    Those were not supported when the OP asked the questions or when Radim Köhler wrote his answer, but I think future visitors will appretiate the update.

    The documentation is also updated and now says:

    cascade="all|none|save-update|delete|delete-orphan|all-delete-orphan"

    But the documentation is confusing now, because it still has the following note:
    The cascade attribute permits the following values: all, save-update, delete, none.
    So I've created a defect to fix that last part of the documentation.

    0 讨论(0)
提交回复
热议问题