NHibernate 左外连接子类

标签 nhibernate subclass outer-join

我有 2 个实体产品和图像。并非所有图像都是产品图像,图像是以下文件的子类,是我的实体。我需要找到与产品无关的所有图像。我对实体检索不熟悉,并且尝试了多种方法。任何想法或链接将不胜感激。

public class File
    {
        #region Feilds
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Enumerations.File.FileType Type { get; set; }
        public virtual string Extension { get; set; }
        public virtual string Path { get; set; }
        public virtual DateTime DateCreated { get; set; }
        public virtual DateTime DateModified { get; set; }
        #endregion
    }

public class Image : File
{
    #region Fields
    public virtual string ImageName { get; set; }
    public virtual string Description { get; set; }
    public virtual bool Active { get; set; }
    public virtual DateTime DateTaken { get; set; }
    #endregion
}

public class Product
{
    #region Properties
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual decimal Price { get; set; }
    public virtual decimal Weight { get; set; }
    public virtual bool IsDigital { get; set; }
    public virtual DateTime DateCreated { get; set; }
    public virtual IList<Category> ProductCategories { get; set; }
    public virtual IList<ProductAttribute> ProductAttributes { get; set; }
    public virtual IList<Image> ProductImages { get; set; }
    #endregion
}

最佳答案

您可以使用 Critiria 不存在子查询...

IList<Image> images = session.CreateCriteria<Image>("img")
        .Add(Expression.Not(Subqueries.Exists(DetachedCriteria.For<ProductImageLink>("pil")
                    .SetProjection(Projections.Constant(1))
                    .Add(Expression.EqProperty("img.image_id", "pil.image_id")))))
        .List<Image>();

其中 ProductImageLink 是关联表。

应该会产生如下查询:

select ... from image img where not exists(select 1 from productimagelink pil where img.image_id = pil.image_id);

关于NHibernate 左外连接子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1360410/

相关文章:

ios - 需要有关为 iOS 应用程序实现多个(相似)场景的建议

c++ - C++ 中模板化子类专门化的限制

css - css 类可以自己上课吗?如何完成?

sql - 如何在 MySQL 中执行 FULL OUTER JOIN?

c# - Nhibernate 删除对象映射和更新数据库

c# - 通过针对接口(interface)编程来保留数据

c# - NHibernate 在 WHERE IN 中使用 QueryOver

c# - 软删除 - ActiveRecord with Listeners

mysql - 慢查询 LEFT OUTER JOIN Mysql

c# - LINQ to SQL 右外连接