本文介绍了Nhibernate生成代理,尽管HQL获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 从TaskEntity中选择不同的t作为
内部连接读取t.Case作为c
内部连接获取c.Client作为客户端
内部连接获取c.Matter作为问题

然而,尽管Matter有一个FETCH对它,它仍然作为代理返回。

这个对象的映射在

 引用(x => x.Matter).Columns(new [] {c_client,c_matter}); 

我试过使用JOIN,但是我的问题是从1列到2列,所以它不会接受映射。



有什么想法吗?

谢谢,

解决方案

我解决了导致这个问题的问题。



复合ID!

在项目的早期版本中,Nhibernate警告说我并不是重写Equals和GetHashCode,为了规避很多代码更改,并且促进代码重用一个CompositeBaseEntity类:

  using System; 
using System.Collections.Generic;
使用System.Linq;
使用System.Text;

名称空间Case.Infrastructure
{
public class BaseCompositeEntity:BaseEntity
{
public override int GetHashCode()
{
return base.GetHashCode();


public override bool Equals(object obj)
{
return base.Equals(obj);





这个班, Nhibernate告诉我要避免的地方!由于有两个关键字来比较平等,我们必须覆盖等于& GetHashCode()方法变成这样:

  public override bool Equals(object obj)
{
if(obj == null)
return false;
var t = obj作为ClientMatterEntity;
if(t == null)
return false;
if(AccountNumber == t.ClientAcconuntNumber&& CaseNumber == t.CaseNumber)
return true;
返回false;





$ b这样,Nhibernate完全知道如何进行比较,然后知道是否它有一个第一级缓存中的对象(它会,因为我们指定的抓取)。

更多信息可以在这里找到:


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

However, despite Matter having a FETCH against it, it's still returning as a proxy.

My mapping for this object is below

References(x => x.Matter).Columns(new[] {"c_client","c_matter" });

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.

Any thoughts?

Thanks,

解决方案

I worked out the problem that was causing this issue.

It also resolves to Composite IDs!

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);
        }
    }
}

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;
        }

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).

More information can be found here:http://nhforge.org/blogs/nhibernate/archive/2010/07/01/nhibernate-and-composite-keys.aspx

这篇关于Nhibernate生成代理,尽管HQL获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:38