使用 SQL 查询填充 nHibernate 映射文件中的属性 [英] Use SQL query to populate property in nHibernate mapping file

查看:42
本文介绍了使用 SQL 查询填充 nHibernate 映射文件中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,它包含一个作为 SQL 语句结果的属性.如何将 SQL 语句添加到我的 nHibernate 映射文件中?

I have an object which contains a property that is the result of an SQL statement. How do I add the SQL statement to my nHibernate mapping file?

示例对象:

public class Library{
    public int BookCount
    {
        get;
        set;
    }
}

示例映射文件:

<hibernate-mapping>
    <class name="Library" table="Libraries">
        <property name="BookCount" type="int">  <- This is where I want the SQL query to populate the value. ->
    </class>
</hibernate-mapping>

示例 SQL 查询:

SELECT COUNT(*) FROM BOOKS WHERE BOOKS.LIBRARY_ID = LIBRARIES.ID

推荐答案

这在 Ayende 的博文:

公式是一种指定我们想要与属性关联的任意 SQL 的方法.显然,这是一个只读值,我们在极少数情况下会使用它.尽管如此,它有时还是很有用的.

formula is a way to specify any arbitrary SQL that we want to associate with a property. Obviously, this is a read only value, and it is something that we would use on fairly rare occasions. Nevertheless, it can be pretty useful at times.

示例:

<property name="BookCount" type="int" formula="(SELECT COUNT(*) FROM BOOKS WHERE BOOKS.LIBRARY_ID = ID)" />

这篇关于使用 SQL 查询填充 nHibernate 映射文件中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆