Kat*_*ate 1 nhibernate join fetch
我已经阅读了一些关于fetch = join的帖子 - http://nhforge.org/blogs/nhibernate/archive/2009/04/09/nhibernate-mapping-lt-many-to-one-gt.aspx(ser4ik.livejournal. com/2505.html)所以我有一些问题,例如我上课了
<class name="AttributesInf" table="attr_inf">      
 <id name="Id">
   <generator class="identity"/>
 </id>
 <property name="Name" />
    <property name="Desc"/>
</class> 
和
<class name="AttributeValue" table="attr_val">      
 <id name="Id">
   <generator class="identity"/>
 </id>
 <property name="Value" />
 <many-to-one name="AttrName" column="attrId"/>
</class> 
如果我在没有set fetch ="join"的情况下使用这个映射,我会得到sql:
Select av.Id, av.Value, av.attrId From attr_val av where av.Id=...()
之后单独的sql查询如:
Select * From attr_inf where Id = av.attrId
所以我的结果是:
class AttrinuteInf 
{ 
int Id; 
string Name; 
string Desc; 
} 
class AttributeValue 
{ 
int Id; 
string  Value; 
AttributeInf AttrName;
}
如果我设置fetch ="join",那么我得到一个查询:
Select u.Id, u.UserName, u.BlogId, b.Id, b.BlogName, b.BlogAuthor, b.BlogMsg 
from Users u
Left outer join Blogs b 
On u.BlogId=b.Id
Where u.Id = ...
所以我希望得到一个班级:
class AttributeValue
{
int Id;
string  Value;
string Name;
string Desc;
}
但是我有相同的结果,好像我没有设置fetch到"join".
这样可以吗?有没有办法<many-to-one>直接从类maped获取属性?(不是AttrName.Name,而是名称)
说明:
上面的映射集的一部分不显示我的真正问题.我想将一些实体映射为IDictionary<string,AttributeValue>.我将其映射为
<map name="Attributes" table="attr_val" lazy="true" cascade="all-delete-orphan" inverse="true">
<key column="item_id"></key>
<index column="name"></index> //I don't have that item in class AttributeValue, that is why I try to get it from other table
<one-to-many class="AttributeValue"/>
</map>
这不是你认为它正在做的事情.使用fetch = join就是急切加载关系的多方面.在这两种情况下,您最终都会返回相同的对象.默认情况下,NHibernate将延迟加载相关实体(这就是为什么你得到第二个查询).通过使用fetch = join,您可以立即请求整个对象关系,但它仍然会在没有fetch = join的情况下填充对象.
| 归档时间: | 
 | 
| 查看次数: | 3307 次 | 
| 最近记录: |