【问题标题】:NHibernate and JoinAlias throw exceptionNHibernate 和 JoinAlias 抛出异常
【发布时间】:2012-01-31 19:08:33
【问题描述】:

我在HQL 有查询,效果很好:

var x =_session.CreateQuery("SELECT r FROM NHFolder f JOIN f.DocumentComputedRights r WHERE f.Id = " + rightsHolder.Id + " AND r.OrganisationalUnit.Id=" + person.Id);
            var right = x.UniqueResult<NHDocumentComputedRight>();

基本上我会收到 NHDocumentComputedRight 实例。

我尝试在 QueryOver 中实现相同的查询。我这样做了:

var right = _session.QueryOver<NHFolder>().JoinAlias(b => b.DocumentComputedRights, () => cp).Where(h => h.Id == rightsHolder.Id && cp.OrganisationalUnit.Id == person.Id)
            .Select(u => cp).List<NHDocumentComputedRight>();

但我得到空引用异常。

如何在QueryOver 中实现此查询?

更新(添加映射) - NHibernate 3.2:

public class FolderMapping: ClassMapping<NHFolder>
    {
        public FolderMapping()
        {
            Table("Folders");
            Id(x => x.Id, map =>
            {
                map.Generator(IdGeneratorSelector.CreateGenerator());
            });
//more not important properties...

            Set(x => x.DocumentComputedRights, v =>
            {
                v.Table("DocumentComputedRightsFolder");
                v.Cascade(Cascade.All | Cascade.DeleteOrphans);
                v.Fetch(CollectionFetchMode.Subselect);
                v.Lazy(CollectionLazy.Lazy);

            }, h => h.ManyToMany());


            Version(x => x.Version, map => map.Generated(VersionGeneration.Never));
            }
    }

public class DocumentComputedRightMapping : ClassMapping<NHDocumentComputedRight>
    {
        public DocumentComputedRightMapping()
        {
            Table("DocumentComputedRights");

            Id(x => x.Id, map =>
            {
                map.Generator(IdGeneratorSelector.CreateGenerator());
            });

//more not important properties...

            ManyToOne(x => x.OrganisationalUnit, map =>
            {
                map.Column("OrganisationalUnit");
                map.NotNullable(false);
                map.Cascade(Cascade.None);
            });

        }
    }

public class OrganisationUnitMapping : ClassMapping<NHOrganisationalUnit>
    {
        public OrganisationUnitMapping()
        {
            Table("OrganisationalUnits");
            Id(x => x.Id, map =>
                              {
                                  map.Generator(IdGeneratorSelector.CreateGenerator());
                              });

//more not important properties...

        }
    }

谢谢

【问题讨论】:

  • 它可以与cp.OrganisationalUnit == person 一起使用吗?
  • 否,因为问题出在 .Select(U=>cp) 部分。当我删除它(或更改为 Select(u=>cp.Id) 时,此查询有效。
  • NHDocumentComputedRight 中是否有对 NHFolder 的反向引用?
  • 没有对 NHFolder 的反向引用。这就是多对多关系。

标签: nhibernate queryover


【解决方案1】:

AFAIK 标准/queryOver 只能返回为它创建的实体(在您的示例中为 NHFolder)或设置为具有 aliastobean 的实体的列。你可以做一个相关的子查询。

var subquery = QueryOver.Of<NHFolder>()
    .JoinAlias(b => b.DocumentComputedRights, () => cp)
    .Where(h => h.Id == rightsHolder.Id && cp.OrganisationalUnit.Id == person.Id)
    .Select(u => cp.Id);

var right = _session.QueryOver<NHDocumentComputedRight>()
    .WithSubquery.Where(r => r.Id).Eq(subquery)
    .SingleOrDefault<NHDocumentComputedRight>();

【讨论】:

    【解决方案2】:

    我觉得你的select语句有问题,你有没有试过这样的:

    var right = _session.QueryOver<NHFolder>()
        .JoinAlias(b => b.DocumentComputedRights, () => cp)
        .Select(x => x.DocumentComputedRights)
        .Where(h => h.Id == rightsHolder.Id && cp.OrganisationalUnit.Id == person.Id)
        .List<NHDocumentComputedRight>();
    

    这对我有用,所以它也应该适用于你的情况。

    我猜想问题背后的主要原因是Select 方法缺乏适当的重载。实际上你想这样写:

    .JoinAlias(b => b.DocumentComputedRights, () => cp)
    .Select(() => cp)
    

    Expression&lt;Func&lt;object&gt;&gt; 不存在。希望它会包含在下一个版本中。

    【讨论】:

    • 我尝试了您的查询,但我得到了这个异常:[ SELECT this_.Id as y0_ FROM Folders this_ inner join DocumentComputedRightsFolder documentco3_ on this_.Id=documentco3_.nhfolder_key inner join DocumentComputedRights cp1_ on documentco3_.elt=cp1_ .Id WHERE(this_.Id = @p0 和 cp1_.OrganisationalUnit = @p1)] 名称:cp0 - 值:65536 名称:cp1 - 值:32769。但奇怪的是,这个查询(在 sql 中)仍然返回 NHFolder id (this_.Id) 而不是 NHDocumentComputedRights
    • 为了您的信息,这是 NHFolder 和 NHDocumentComputedRights 之间的多对多关系
    • 您也可以粘贴您的映射吗?生成的 sql 似乎不正确 - 您使用的是哪个版本的 NHibernate?
    • 为问题添加了映射。我跳过了不重要的属性。我使用 NH 3.2
    猜你喜欢
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多