2009-08-13 3 views
1

가 TemporaryAccessOpenCommand 및 TemporaryAccessCloseCommand 모두가 기본 클래스 명령에서 상속 다음 매핑 파일을 고려NHibernate에 쿼리 문제

<class name="Command" table="xxx_Commands" lazy="false"> 
<id name="Id" type="Int32" unsaved-value="0"> 
    <generator class="identity"/> 
</id> 
<property name="BeginDate" /> 
<property name="EndDate" /> 
<component name="Result" class="CommandResult"> 
    <property name="Status" column="ResultStatus"/> 
    <property name="Details" column="ResultDetails" /> 
</component> 

<many-to-one name="Requestor" class="xxx.Domain.SessionInfo, xxx.Domain" column="SessionInfoId" lazy="false" /> 

<joined-subclass name="xxx.TemporaryAccess.Commands.TemporaryAccessCloseCommand, xxx.TemporaryAccess" table="xxx_Commands_TemporaryAccess_Close"> 
    <key column="CommandId"/> 
</joined-subclass> 

<joined-subclass name="xxx.TemporaryAccess.Commands.TemporaryAccessOpenCommand, xxx.TemporaryAccess" table="xxx_Commands_TemporaryAccess_Open"> 
    <key column="CommandId"/> 
    <many-to-one name="EndUser" ...... /> 
    <property name="Reason"/> 
    <property name="AccessRight"/> 
    <property name="AccessType"/> 
    <bag name="CloseAccessCommands" cascade="all" lazy="false"> 
    <key column="OpenCommandId"/> 
    <one-to-many class="xxx.TemporaryAccess.Commands.TemporaryAccessCloseCommand, xxx.TemporaryAccess"/> 
    </bag> 
</joined-subclass> 

무슨이 CloseAccessCommand 성공이없는 모든 OpenAccessCommand를 검색 할 NHibernate에 쿼리를 것입니까?

나는이 시도 :

public IList<TemporaryAccessOpenCommand> FindOpenCommandsWithoutSucceededCloseCommand() 
    { 
     return this.HibernateTemplate.ExecuteFind<TemporaryAccessOpenCommand>(delegate(ISession session) 
     { 
      return session.CreateCriteria(typeof(TemporaryAccessOpenCommand)) 
       .CreateCriteria("CloseAccessCommands") 
       .Add(Expression.Not(Expression.Eq("Result.Status", CommandStatus.Succeeded))) 
       .List<TemporaryAccessOpenCommand>(); 
     }); 
    } 

을하지만 빈 목록을 반환해야 할 때 그것은 견인 CloseCommand을 (하나 실패 하나가 성공)이있는 OpenAccessCommand를 반환합니다.

답변

0

도와

감사합니다 (그리고 불쌍한 내 영어 변명은) 나는이 방법 (그것이 최선의 방법이 아니라면 내가 제안을 열려있어)

public IList<TemporaryAccessOpenCommand> FindOpenCommandsWithoutSucceededCloseCommand() 
    { 
     return this.HibernateTemplate.ExecuteFind<TemporaryAccessOpenCommand>(delegate(ISession session) 
     { 
      string sql = string.Format(@" 
             (
              SELECT temp_c.OpenCommandId 
              FROM VSA2_Commands_TemporaryAccess_Close temp_c 
                INNER JOIN VSA2_Commands c 
                 ON temp_c.CommandId = c.Id 
              WHERE c.ResultStatus = {0} 
             ) AS OpenCommandId" 

             , (int)CommandStatus.Succeeded); 

      var subCriteria = DetachedCriteria.For<TemporaryAccessCloseCommand>(); 
      subCriteria = subCriteria.SetProjection(Projections.SqlProjection(sql, new string[] { "OpenCommandID" }, new IType[] { NHibernateUtil.Int32 })); 


      return session.CreateCriteria(typeof(TemporaryAccessOpenCommand)) 
       .Add(Expression.Eq("Result.Status", CommandStatus.Succeeded)) 
       .Add(Subqueries.PropertyNotIn("Id", subCriteria)) 
       .List<TemporaryAccessOpenCommand>(); 
     }); 
    } 
작업 뭔가를 얻을 수 있었다