nhibernate命名查询连接单条记录
这就是我所拥有的:
class Unit
{
public Position ManagerPosition;
}
<sql-query name="GetUnits" read-only="true">
<query-param name="UserGroupId" type="int"/>
<query-param name="RelevantToDate" type="datetime"/>
<query-param name="ParentUnitId" type="int"/>
<return class="Unit" />
...
</sql-query>
<sql-query name="GetPositions" read-only="true">
<query-param name="UserGroupId" type="int"/>
<query-param name="RelevantToDate" type="datetime"/>
<query-param name="UnitId" type="int"/>
<query-param name="ManagersOnly" type="bool"/>
<return class="Position" />
...
</sql-query>
单位和职位位于单独的表中,但均按UserGroupId和RelevantDate过滤(适当命名查询的参数)。由于其属性的复杂性,它们不会直接映射到表。
我想做的是获取一个 Units 列表,其中包含一个 Position,其标志 IsManager 设置为 True 。
我尝试通过 return-join 来做到这一点 - 没有成功。 我尝试通过
<return class="Unit">
<return-property name="ManagerialPosition" column="position"/>
</return>
and
<return alias="position" class="Position">
来做到这一点,但是 NHibernate 说它不能将 System.Object 数组转换为 Unit 数组。
有什么更好的方法来解决我的问题?提前致谢!
更好的方法是:
- 从单位查询中调用参数化查询来获取位置。
- 解析组合查询的结果集以获取 Unit 和 Position 属性
This is what i have:
class Unit
{
public Position ManagerPosition;
}
<sql-query name="GetUnits" read-only="true">
<query-param name="UserGroupId" type="int"/>
<query-param name="RelevantToDate" type="datetime"/>
<query-param name="ParentUnitId" type="int"/>
<return class="Unit" />
...
</sql-query>
<sql-query name="GetPositions" read-only="true">
<query-param name="UserGroupId" type="int"/>
<query-param name="RelevantToDate" type="datetime"/>
<query-param name="UnitId" type="int"/>
<query-param name="ManagersOnly" type="bool"/>
<return class="Position" />
...
</sql-query>
Units and Positions are in separate tables, but both filtered by UserGroupId and RelevantDate (the parameters of the appropriate named queries). they are not mapped to the tables directly because of the complexity of their properties.
What i'm trying to do is to get a list of Units with a single Position which has its flag IsManager set to True.
I've tried to do it by return-join - no success.
I've tried to do it by
<return class="Unit">
<return-property name="ManagerialPosition" column="position"/>
</return>
and
<return alias="position" class="Position">
but the NHibernate said that it can't cast an array of System.Object to an array of Unit.
what is better way to solve my problem? thanks in advance!
the preferable ways are:
- call a parameterized query for Position from Unit query.
- parse a resultset of a combined query to get Unit and Position properties
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论