이러한 (간체) 내 엔티티 :NHibernate에 쿼리 문제
public class IdentificationRequest
{
private int id;
private ICollection<IdentificationRequestStateHistoryItem> stateHistoryItems;
public virtual string Id
{
get { return id; }
private set { id = value; }
}
public virtual IEnumerable<IdentificationRequestStateHistoryItem> StateHistoryItems
{
get { return stateHistoryItems; }
private set { stateHistoryItems = new List<IdentificationRequestStateHistoryItem>(value); }
}
public void ChangeState(IdentificationRequestState state)
{
// state transition here
var stateHistoryItem = new IdentificationRequestStateHistoryItem(state, this);
stateHistoryItems.Add(stateHistoryItem);
}
}
public class IdentificationRequestStateHistoryItem
{
private int id;
private IdentificationRequestState state;
private EntityTimestamp timestamp;
public virtual string Id
{
get { return id; }
private set { id = value; }
}
public virtual IdentificationRequestState State
{
get { return state; }
private set { state = value; }
}
public virtual EntityTimestamp Timestamp
{
get { return timestamp; }
private set { timestamp = value; }
}
}
public class IdentificationRequestState
{
// possible states are
// Received, then id = 10
// Finished, then id = 20
private int id;
private string name;
public virtual string Name
{
get { return name; }
private set { name = value; }
}
public virtual string Id
{
get { return id; }
private set { id = value; }
}
}
public class EntityTimestamp
{
private DateTime createdOn;
public virtual DateTime CreatedOn
{
get { return createdOn; }
private set { createdOn = value; }
}
private IUser createdBy;
public virtual IUser CreatedBy
{
get { return createdBy; }
private set { createdBy = value; }
}
private DateTime changedOn;
public virtual DateTime ChangedOn
{
get { return changedOn; }
private set { changedOn = value; }
}
private IUser changedBy;
public virtual IUser ChangedBy
{
get { return changedBy; }
private set { changedBy = value; }
}
}
은 그래서 위의 다음 또한 데이터베이스에 그렇게 설계처럼 읽습니다.
IdentificationRequest
은 IdentificationRequestState
전환을 추적 할 수 있습니다. 따라서 새 IdentificationRequestState
이 할당 될 때마다 IdentificationRequestStateHistoryItem
이 새로 만들어지고 여러 개의 기록 항목 모음에 저장됩니다. 즉, 현재 IdentificationRequestState
이 마지막으로 추가 된 것입니다 (createdOn 날짜로 표시됨).
그래서 지금 내 질문은 쿼리의 모양입니다. 모든 IdentificationRequest
의 현재 상태가 Received 또는 Finished로 표시됩니다.
이
은 내가 지금까지 무엇을 가지고 있습니다 :private ICollection<IdentificationRequest> GetByCurrentState(IdentificationRequestState state)
{
var session = GetCurrentSession();
var subCriteria = DetachedCriteria.For(typeof(IdentificationRequestStateHistoryItem))
.SetProjection(Projections.Property("identificationRequest.Id"));
subCriteria.Add(Restrictions.Eq("State", state));
var criteria = session
.CreateCriteria(typeof (IdentificationRequest))
.Add(Subqueries.PropertyIn("Id", subCriteria))
.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer());
return criteria.List<IdentificationRequest>();
}
그러나이 쿼리를 사용하고 현재 상태로 식별 요청에 대한 검색을받은 경우 요청을했기 때문에 현재 상태가 완료로, 나는 또한 요청을받을 때 전에받은 상태 그래서 Max (CreatedOn) 등으로 뭔가를해야합니까?
NHibernate에 매핑 :
<class name="IdentificationRequest" table="IdentificationRequest">
<id name="Id" column="IdentificationRequestId" unsaved-value="-1">
<generator class="identity"/>
</id>
<bag name="stateHistoryItems" access="field" order-by="CreatedOn desc" fetch="join" cascade="all-delete-orphan" lazy="false">
<key column="IdentificationRequestId"/>
<one-to-many class="IdentificationRequestStateHistoryItem"/>
</bag>
</class>
<class name="IdentificationRequestStateHistoryItem" table="IdentificationRequestStateHistoryItem">
<id name="Id" column="IdentificationRequestStateHistoryItemId" unsaved-value="-1">
<generator class="identity"/>
</id>
<many-to-one name="identificationRequest" access="field" lazy="false" cascade="none" column="IdentificationRequestId" class="IdentificationRequest"></many-to-one>
<many-to-one name="State" lazy="false" fetch="join" cascade="none" column="IdentificationRequestStateId" class="IdentificationRequestState"></many-to-one>
<component name="Timestamp" class="EntityTimestamp">
<property name="CreatedOn"/>
<component name="CreatedBy" class="User">
<property name="Name" column="CreatedBy" />
</component>
<property name="ChangedOn"/>
<component name="ChangedBy" class="User">
<property name="Name" column="ChangedBy" />
</component>
</component>
</class>
<class name="IdentificationRequestState" table="IdentificationRequestState">
<id name="Id" column="IdentificationRequestStateId" unsaved-value="-1">
<generator class="assigned"/>
</id>
<property name="Name"/>
</class>