NHibernate 左外连接子类

发布于 2024-08-03 22:48:35 字数 1462 浏览 4 评论 0原文

我有 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
}

I have 2 entities products and images. Not all images are product images, and images are a sub class of a file below are my entities. I need to find all of the images that are not associated to a product. I'm new to retrieval of entities and have tried numerous approaches. Any ideas or links would be greatly appreciated.

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵强ㄟ 2024-08-10 22:48:35

您可以使用 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);

You can use a Critiria not exists subquery...

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

Where ProductImageLink is the association table.

Should result in a query like...

select ... from image img where not exists(select 1 from productimagelink pil where img.image_id = pil.image_id);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文