내 응용 프로그램에 Canine 및 CanineHandler 개체가 있습니다. CanineHandler 개체는 완전히 다른 데이터베이스를 참조하는 PersonID, EffectiveDate (송곳니로 처리기를 시작한시기를 지정) 및 Canine (CanineID)에 대한 FK 참조를가집니다.nhibernate에서 최신 그룹 선택
특정 PersonID가 주어지면 현재 모든 책임을 맡고 있습니다.
Select Canine.*
from Canine
inner join CanineHandler on(CanineHandler.CanineID=Canine.CanineID)
inner join
(select CanineID,Max(EffectiveDate) MaxEffectiveDate
from caninehandler
group by CanineID) as CurrentHandler
on(CurrentHandler.CanineID=CanineHandler.CanineID
and CurrentHandler.MaxEffectiveDate=CanineHandler.EffectiveDate)
where [email protected]
편집 : 추가 매핑 파일 아래 : 나는 SQL에서 사용하려는 (간체) 쿼리는 것
<class name="CanineHandler" table="CanineHandler" schema="dbo">
<id name="CanineHandlerID" type="Int32">
<generator class="identity" />
</id>
<property name="EffectiveDate" type="DateTime" precision="16" not-null="true" />
<property name="HandlerPersonID" type="Int64" precision="19" not-null="true" />
<many-to-one name="Canine" class="Canine" column="CanineID" not-null="true" access="field.camelcase-underscore" />
</class>
<class name="Canine" table="Canine">
<id name="CanineID" type="Int32">
<generator class="identity" />
</id>
<property name="Name" type="String" length="64" not-null="true" />
...
<set name="CanineHandlers" table="CanineHandler" inverse="true" order-by="EffectiveDate desc" cascade="save-update" access="field.camelcase-underscore">
<key column="CanineID" />
<one-to-many class="CanineHandler" />
</set>
<property name="IsDeleted" type="Boolean" not-null="true" />
</class>
내가 아직 시도하지 않은,하지만 난 내가 할 수있는 것 같은데요 HQL에서 이것. 아직 HQL에 어떤 것을 써야 할 필요가 없으므로 궁극적으로 그 문제를 해결해야 할 것입니다.하지만 제 질문은 criterion/subqueries 객체로이 서브 쿼리를 수행 할 수 있는지 여부입니다.
나는 지금까지 다음과 같은 분리 된 기준을 만드는 등 가지고 :
DetachedCriteria effectiveHandlers = DetachedCriteria.For<Canine>()
.SetProjection(Projections.ProjectionList()
.Add(Projections.Max("EffectiveDate"),"MaxEffectiveDate")
.Add(Projections.GroupProperty("CanineID"),"handledCanineID")
);
하지만 난 내부 조인하는 방법을 알아낼 수 없습니다. 나는이 작업을 수행 할 경우
Session.CreateCriteria<Canine>()
.CreateCriteria("CanineHandler", "handler", NHibernate.SqlCommand.JoinType.InnerJoin)
.List<Canine>();
내가 오류 "속성을 확인할 수 없습니다 : CanineHandler의 : OPS.CanineApp.Model.Canine". 분명히 나는 뭔가를 놓치고있다. 그러나 문서에서 핸들러를 가진 Canines리스트를 반환해야한다는 인상을 받았다. 이 작업을 할 수있을 때까지 하위 쿼리를 추가해도 작동하지 않습니다 ...
Only get latest results using nHibernate과 같은 비슷한 질문을 발견했지만 실제로는 직접적인 결과의 종류와 함께 적용되는 것 같지 않습니다. 내가 찾고있어.
도움이나 의견을 크게 주시면 감사하겠습니다.
여기에 매핑 된 저장 프로 시저의 모습입니다 매핑없이 올바르게 대답하기가 힘들다. 하위 쿼리를 사용하여 유사한 문제를 해결했다. 14.8을 참조한다. nhibernate 참조 – bernhardrusch
의 분리 된 쿼리와 하위 쿼리는 첫 번째 쿼리가 제대로 보이지 않습니다. 현재 처리중인 모든 개는 가져 오지 않지만 가장 최근에 처리 한 개는 가져 오지 않습니다. 같은 날짜에 개가 둘 이상 처리 된 경우에만 여러 결과가 반환됩니다. – mdma
SQL 쿼리가 작동합니다. CanineID 목록과 최신 핸들러가 개에 할당 된 날짜를 반환합니다 (예 : {(1,2010.01.01), (2,2010.03.05), ...} 두 번째 쿼리는 핸들러 레코드와 일치합니다 by Date and Dog. 실제로, EffectiveDate와 CanineID는 CanineHandler의 행을 고유하게 식별한다는 것을 언급해야합니다. 이 테이블을 대체하기 위해 대체 키 제약 조건을 적용하지 않았지만 CanineID & Date 쌍, 쿼리는 여전히 작동 할 것입니다. – Kendrick