views:

940

answers:

1

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
}
+2  A: 

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);
dotjoe
That worked like a charm had to refactor it a bit to:IList<APP.File.Image> images = session.CreateCriteria(typeof(APP.File.Image), "img") .Add(Expression.Not(Subqueries.Exists(DetachedCriteria.For<APP.ECommerce.ProductImage>("pi") .SetProjection(Projections.Constant(1)) .Add(Expression.EqProperty("img.Id", "pi.Image"))))).List<APP.File.Image>();
John Delate
Also what is a good resource/documentation for Fluent NHibernate Criteria?
John Delate
oh yea, i forgot the List at the end. Not sure about Fluent, I'm only a month in to regular hibernate. I thought Fluent was about overcoming the hbm.xml mapping files.
dotjoe
+1 Once again you got an accepted answer without an upvote. I still think it should automatically be done, but I am apparently the only one so I'll just keep upvoting 0 vote accepted answers as I find them. :)
Dusty
I couldnt upvote.. didnt have the reputation...
John Delate