nhibernate and “NOT IN” in a aggregate subquery

99封情书 提交于 2019-12-11 06:18:13

问题


I've got an entity Reminder which contains a collection of ReminderSchedule. This is my mapping:

<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Reminder" table="Reminders">
    <id name="Code" type="System.Guid">
        <column name="ReminderCode" />
        <generator class="guid.comb" />
    </id>
    ...
    <set access="field.pascalcase-underscore" cascade="all-delete-orphan" inverse="true" lazy="false" name="Schedules" mutable="true">
      <key foreign-key="FK_Schedules_Reminders">
        <column name="ReminderCode" />
      </key>
      <one-to-many class="ReminderSchedule" />
    </set>
</class>

This is the mapping for the entity ReminderSchedule:

  <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="ReminderSchedule" table="ReminderSchedules">
    <id name="Code" type="System.Guid">
      <column name="ReminderScheduleCode" />
      <generator class="guid.comb" />
    </id>
    <property name="NextSchedule" type="System.DateTime">
      <column name="NextSchedule" index="ReminderScheduleK01" not-null="true" />
    </property>
    <many-to-one class="Reminder" foreign-key="FK_ReminderScheduleToReminder" name="Reminder">
      <column name="ReminderCode" index="ReminderScheduleK02" not-null="true" />
    </many-to-one>
  </class>

ReminderSchedules table contains a record for each new scedule associated with a reminder. I can close a schedule (Closed = 1) and reschedule a new one.
In this situation I would have a new records with Closed = 0 and I the field NextSchedule would contain the date/time of the next schedule.

I am already using CreateCriteria to filter the reminders and it works pretty very well. Now I would like to fetch the reminders which do not have any ReminderSchedule open.

I've figured out how to do it with a query:

SELECT * FROM Reminders
WHERE ReminderCode 
    NOT IN (
        SELECT LastReschedule.ReminderCode FROM (
        SELECT ReminderCode, MAX(NextSchedule) MaxSchedule
        FROM ReminderSchedules
        WHERE Closed = 1 
        GROUP BY ReminderCode) LastReschedule
        )
ORDER BY Reminders.ReminderCode

but I don't know how to translate it in a criteria valid for nhibernate.

Is there anybody out there who can help me? It would be really appreciated.


回答1:


Since I haven't been able to use 2 subqueries (nested) I've tried to simplify using just one subquery.
I've fetched the id of the reminder (Schedules.Reminder.Code) for the schedules closed. Luckily for me I shouldn't have another schedule if the previous one hasn't been closed. If it happens, too bad, I won't be able to manage it.

This is the best I could do:

ICriteria FiltersCriteria = Session.CreateCriteria<Domain.Reminder>("Reminders");

DetachedCriteria dCriteria = DetachedCriteria.For<Domain.ReminderSchedule>("Schedules")
    .SetProjection(Projections.ProjectionList()
    .Add(Projections.GroupProperty("Schedules.Reminder.Code")))
    // .Add(Projections.Max("Schedules.NextSchedule").As("MaxSchedule")))
    .Add(Restrictions.Eq("Schedules.Closed", true));

FiltersCriteria.Add(Subqueries.PropertyIn("Reminders.Code", dCriteria));  

I guess this sample code is self explanatory.
I've used a DetachedCriteria for the subquery and I use it as a parameter for my Criteria.




回答2:


you can achieve that by using a Subquery.
in HQL, it should be something along the lines of (untested):

from Reminders as R
where R.ReminderCode not in
   ( select RS.ReminderCode
     from ReminderSchedules as RS
     where RS.NextSchedule = (select max(NextSchedule) from ReminderSchedules as Inner where RS.Closed = 1)
   )

even though I don't believe I hit exactly what you need, the link provided should help you get in the right direction.



来源:https://stackoverflow.com/questions/6061649/nhibernate-and-not-in-in-a-aggregate-subquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!