NHibernate Collection Sub Query

后端 未结 3 1303
半阙折子戏
半阙折子戏 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:06

    If you have subclasses of Participant with the status being the DiscriminatorValue, I think you can have them as separate lists since this way nHibernate knows how to differentiate them.

    public class ParticipantMap : ClassMap<Participant>
    {
        public ParticipantMap()
        {
            DiscriminateSubClassesOnColumn("Status");
        }
    }
    public class ActiveParticipantMap : SubclassMap<ActiveParticipant>
    {
        public ActiveParticipantMap ()
        {
            DiscriminatorValue("Active");
        }
    }
    // other subclasses of participant e.g. NonActive, etc.
    
    public class Course {
        public IList<Participant> Participants {get;set;}
        public IList<ActiveParticipant> ActiveParticipants {get;set;}
        // any other lists of participant subclasses that you may have
    }
    
    0 讨论(0)
  • 2021-01-21 15:11

    You dont need a subquery to load a collection with additional condition.

    HasMany(x => x.ActiveParticipants).Where("Status = 'Active'");
    
    0 讨论(0)
  • 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:

    <filter-def name="myFilter">
        <filter-param name="myFilterParam" type="String"/>
    </filter-def>
    

    Then, this filter can be attached to a class:

    <class name="MyClass" ...>
        ...
        <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/>
    </class>
    

    or, to a collection:

    <set ...>
        <filter name="myFilter" condition=":myFilterParam = MY_FILTERED_COLUMN"/>
    </set>
    

    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
    0 讨论(0)
提交回复
热议问题