我们有一个共同的DB模式:(N)休眠找到架构内容:对象< - 的AttributeValue - >属性
对象< - 的AttributeValue - >属性
可以说,我们谈论的房子。房子可以有很多很多属性(门的形状,屋顶的颜色等)。未知混凝土房屋的属性数量。
直截了当映射对象使用:
- 府 - 包含HouseAttributeValue对象的集合,
- HouseAttributeValue - 包含属性的对象和它的字符串值
- HouseAttribute - 包含属性名称
现在简单得到按预期工作:
ICriteria criteria = this.Repository.CreateCriteria(typeof(House))
IList<House> searchResult = this.Repository.GetList<House>();
返回属性值集合(带属性对象)的房屋。
我们需要找到特定的房子(带有大窗户和玻璃门)并获得它的所有属性。
我想象(不介意性能)没有神奇的select语句:
SELECT
this_.*,
att_val_fetch.*,
att_fetch.*
FROM House this_
INNER JOIN attribute_value att_val_fetch ON this_.versionedobjectid = att_val_fetch.versionedobjectid
INNER JOIN attribute att_fetch ON att_val_fetch.attributeid = att_fetch.attributeid
-- only for filter, no need to fetch
INNER JOIN attribute_value att_val_1 ON this_.versionedobjectid = att_val_1.versionedobjectid
INNER JOIN attribute att_1 ON att_val_1.attributeid = att_1.attributeid
INNER JOIN attribute_value att_val_2 ON this_.versionedobjectid = att_val_2.versionedobjectid
INNER JOIN attribute att_2 ON att_val_2.attributeid = att_2.attributeid
WHERE((att_1.attributename = 'window' AND att_val_1.valuestring IN('big'))
and (att_2.attributename = 'door' AND att_val_2.valuestring IN('glass')));
这怎么能使用NHibernate(或休眠)做些什么呢?希望在代码中看到没有直接SQL语句的解决方案,但实际上任何解决方案都是受欢迎的。