惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
H
Heimdal Security Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
AWS News Blog
AWS News Blog
C
Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Hacker News
The Hacker News
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
S
Securelist
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
博客园 - 叶小钗
J
Java Code Geeks
V
V2EX
博客园 - Franky
Spread Privacy
Spread Privacy
K
Kaspersky official blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Simon Willison's Weblog
Simon Willison's Weblog
Project Zero
Project Zero
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
C
CERT Recently Published Vulnerability Notes
Latest news
Latest news
NISL@THU
NISL@THU
罗磊的独立博客
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
V
Visual Studio Blog

博客园 - 张志敏

System.Net.Http for Silverlight 通过网页进行 iOS 应用内部分发 使用 Intel HAXM 为 Android 模拟器加速,媲美真机 Mono for Android 实现高效的导航 Activity 生命周期及其栈管理方式 我的第一个 Mono for Android 应用 谈谈 INotifyPropertyChanged 的实现 在 Silverlight 5 项目中使用 async/await Task 编程中的异常处理 开源一个 Sliverlight 导航框架 SQLite 常用 SQL 让 VS 编译 MonoTouch 项目源文件不再出错 MonoTouch绑定CocoaTouch类库 升级 Xcode 4.3 后找不到 xcodebuild 的解决方法 iOS 静态类库项目的建立与使用 从源代码编译里程碑的 ICS ROM IIS 7.5 下应用程序池的预定义账户 Web API 依赖注入与扩展 ASP.NET Web API 简介
NHibernate 关系映射中的 Formula
张志敏 · 2012-06-20 · via 博客园 - 张志敏

在 Nhibernate 的实体类映射中, 如果实体类的属性需要通过 SQL 计算才能得到, 则可以使用 Formula 选项解决。

Nhibernate 对 Formula 的要求如下:

formula (optional): an SQL expression that defines the value for a computed property. Computed properties do not have a column mapping of their own.

场景1: 映射需要计算的属性

以下面的 Category 映射为例:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Northwind" namespace="Northwind">
   <class name="Category" table="[Categories]" schema="[dbo]">
      <id name="CategoryID" column="[CategoryID]" type="int" />
      <property name="CategoryName" column="[CategoryName]" type="string"/>
      <property name="Description" column="[Description]" type="string"/>
      <property name="Picture" column="[Picture]" type="binary"/>

      <set name="Products" lazy="true">
         <key column="CategoryID" />
         <one-to-many class="Product" not-found="ignore"/>
      </set>

   </class>
</hibernate-mapping>

如果要增加一个属性 NameAndDesc , 把 CategoryName 和 Description 两个字段连接起来, 用 Formula 可以这样做:

<property name="NameAndDesc" formula="[CategoryName] + ' ' + [Description]" type="string" />

场景2: 映射复杂的 SQL 类型

SQL 2008 支持空间数据类型 geography 和 geometry , 映射空间数据类型可以通过 Nhibernate 的空间扩展解决, 操作起来比较麻烦, 在客户端不需要空间数据类型或者不能处理空间数据类型的情况下, 可以用 Formula 处理。 示例表结构定义如下:

CREATE TABLE SpatialTable (
   id int IDENTITY (1,1),
   GeogCol1 geography,
);

使用 Formula 的映射文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Northwind" namespace="Northwind">
   <class name="Category" table="[Categories]" schema="[dbo]">
      <id name="Id" column="[id]" type="int" />
      <property name="GeoCol1" formula="[GeogCol1].STAsText()" type="string" />
   </class>
</hibernate-mapping>