尽管 HQL 获取 Nhibernate 生产代理 [英] Nhibernate producing proxy despite HQL fetch
问题描述
我有以下 HQL 语句:
I have the following HQL statement:
select distinct t from TaskEntity as
inner join fetch t.Case as c
inner join fetch c.Client as client
inner join fetch c.Matter as matter
然而,尽管 Matter 有 FETCH 反对它,它仍然作为代理返回.
However, despite Matter having a FETCH against it, it's still returning as a proxy.
我对此对象的映射如下
References(x => x.Matter).Columns(new[] {"c_client","c_matter" });
我试过在这个问题上使用 JOIN,但我的问题是从 1 列到 2 列,所以它不接受映射.
I've tried using JOIN on this, but my issue I'm going from 1 to 2 columns, so it wouldn't accept the mapping.
有什么想法吗?
谢谢,
推荐答案
我解决了导致此问题的问题.
I worked out the problem that was causing this issue.
它还解析为复合 ID!
It also resolves to Composite IDs!
在项目早期 Nhibernate 警告我没有覆盖 Equals 和 GetHashCode,为了规避大量代码更改,并促进代码重用,我创建了一个 CompositeBaseEntity 类:
Earlier in the project Nhibernate was warning that I was not overriding Equals and GetHashCode, to circumvent lots of code changes, and to promote code re-use I made a CompositeBaseEntity class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Case.Infrastructure
{
public class BaseCompositeEntity : BaseEntity
{
public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
}
}
这个类,把 Nhibernate 告诉我要避免的东西放回原处!由于有两个键可以比较相等性,我们必须覆盖 Equals &GetHashCode() 方法变成类似:
This class, puts back in place what Nhibernate was telling me to avoid! As there are two keys to compare equality against, we MUST override the Equals & GetHashCode() methods to become something like:
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = obj as ClientMatterEntity;
if (t == null)
return false;
if (AccountNumber== t.ClientAcconuntNumber && CaseNumber == t.CaseNumber)
return true;
return false;
}
通过这种方式,Nhibernate 确切地知道应该如何进行比较,然后知道它是否在一级缓存中具有该对象(它将按照我们指定的 fetch 进行).
This way, Nhibernate knows exactly how comparison should be done, and then knows if it has that object in a first-level cache (which it will, as we specified fetch).
可以在此处找到更多信息:http://nhforge.org/blogs/nhibernate/archive/2010/07/01/nhibernate-and-composite-keys.aspx
More information can be found here: http://nhforge.org/blogs/nhibernate/archive/2010/07/01/nhibernate-and-composite-keys.aspx
这篇关于尽管 HQL 获取 Nhibernate 生产代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!