没有左连接到派生类的NHibernate查询基类 [英] NHibernate query baseclass without left joins to derived classes

查看:54
本文介绍了没有左连接到派生类的NHibernate查询基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,其中一个继承自另一个.我试图使用 NHibernate 来映射这些类,以便我可以尽可能有效地查询这两个类.基类抽象,需要能够以自己的方式进行查询.基类看起来像这样.

I have two classes one of which inherits from the other. Im trying to use NHibernate to map these classes such that I can query both as efficiently as possible. The base class is not abstract and needs to be able to be queried in its own right. The base class looks somthing like this.

public class Cat
{
    public int Id { get; set; }
    public string Name { get; set; }
}

派生类看起来像这样:

public class CustomerCat : Cat
{
    public int CustomerId { get; set; }
}

派生的简单扩展了基础并为其添加了更多属性.我的映射文件看起来像这样

the derived simple extends the base and adds more properties to it. My Mapping file looks like this

<?xml version="1.0" encoding="utf-8" ?>  
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"  
                   assembly="Foo"  
                   namespace="Foo">  
  <class name="Cat" table="Cat">  
    <id name="Id" column="CatId" type="Int32" unsaved-value="0">  
      <generator class="native" />  
    </id>  
    <property name="Name" column="CatName" type="string" />  
    <joined-subclass name="CustomerCat" table="CustomerCat">  
      <key column="CatId" />  
      <property name="CustomerId " column="CustomerId" />  
    </joined-subclass>  
  </class>  
</hibernate-mapping>

我遇到的问题是,如果我通过这样的方式查询 Cats:

The problem Im having is if I query for Cats via something like this:

IList<Cat> cats = session.CreateCriteria<Cat>().List<Cat>();

生成的sql将通过joined-subclass表离开join.我可以在不引用查询中任何派生类表的情况下查询我的基类吗?

The sql generated will left join to by joined-subclass table. Can I query my base class without any refernce to any derived classes' tables in the query?

推荐答案

我认为为了以您希望的方式工作(即,不总是进行左连接)您必须创建一个抽象基类.问题是所有 CustomerCats 都是 Cats,因此它必须加入该表才能获得所有 Cats(即 Cats 和 CustomerCats).

I think in order to work in the way that you want it to (ie, not always do a left join) you have to create an abstract base class. The problem is that all CustomerCats are Cats and so it will have to join onto that table to get all the Cats (that is, Cats and CustomerCats).

如果您决定创建抽象基类,请参阅 此处 概述了不同的选项及其产生的 SQL.

If you do decide to create an abstract base class, see here for an overview of the different options and what SQL they result in.

这篇关于没有左连接到派生类的NHibernate查询基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆