0
我有一個基本類型,其中「業務ID」必須是唯一的給定的子類,但它可能有不同的子類具有相同的業務ID。polymorphic hql
如果存在具有所請求的ID但具有錯誤的子類的基類型,我想使用命名查詢返回空值。下面的代碼是這樣做的,但我想知道是否可以通過更好的HQL避免try/catch。我可以嗎?
乾杯,
Berryl
電流HQL
<query name="FindActivitySubjectByBusinessId">
<![CDATA[
from ActivitySubject act
where act.BusinessId = :businessId
]]>
</query>
電流取代碼
public ActivitySubject FindByBusinessId<T>(string businessId) where T : ActivitySubject
{
Check.RequireStringValue(businessId, "businessId");
try {
return _session.GetNamedQuery("FindActivitySubjectByBusinessId")
.SetString("businessId", businessId)
.UniqueResult<T>();
}
catch (InvalidCastException e) {
// an Activity Subject was found with the requested id but the wrong type
return null;
}
}