NHibernate Collection Sub Query

后端 未结 3 1308
半阙折子戏
半阙折子戏 2021-01-21 14:51

Is there a way to add criteria to a mapping for NHibernate?

public class Course {
   public IList Participants { get; set; }
   public IList&l         


        
3条回答
  •  旧巷少年郎
    2021-01-21 15:12

    It seems, that in this case, we would need "dynamic" filtering. The mapped collection will sometimes contain contain all records, sometimes just those which meet some filtering criteria.

    Exactly for this, we have:

    18.1. NHibernate filters

    NHibernate adds the ability to pre-define filter criteria and attach those filters at both a class and a collection level. A filter criteria is the ability to define a restriction clause very similiar to the existing "where" attribute available on the class and various collection elements. Except these filter conditions can be parameterized. The application can then make the decision at runtime whether given filters should be enabled and what their parameter values should be. Filters can be used like database views, but parameterized inside the application.

    In order to use filters, they must first be defined and then attached to the appropriate mapping elements. To define a filter, use the element within a element:

    
        
    
    

    Then, this filter can be attached to a class:

    
        ...
        
    
    

    or, to a collection:

    
        
    
    

    Check more details here (xml)

    • Is it possible to use NHibernate Filters to filter through references?
    • NHibernate load entity with part of sub collection

    (fluent mapping)

    • Limit collection to retrieve only recent entries for readonly entity

提交回复
热议问题