NHibernate left join select count in one-to-many relationship - nhibernate

I've been looking for a week after a correct synthax whithout success.
I have 2 classes :
public class ArtworkData
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<CommentData> Comments { get; set; }
}
public class CommentData
{
public virtual Guid Id { get; set; }
public virtual string Text { get; set; }
public virtual ProfileData Profile { get; set; }
public virtual ArtworkData Artwork { get; set; }
public virtual DateTime Created { get; set; }
}
I want to do this query :
SELECT this_.ArtworkId as ArtworkId3_3_,
this_.Name as Name3_3_,
this_.Description as Descript3_3_3_,
FROM Artwork this_
LEFT outer JOIN
(SELECT c.ArtworkIdFk, count(1) Cnt
FROM Comment c
GROUP BY c.ArtworkIdFk) as com
on com.ArtworkIdFk = this_.ArtworkId
ORDER BY 1 desc
But I don't find the way to.
At this moment I just have something like this :
ICriteria c = this.Session.CreateCriteria(typeof(ArtworkData));
if(filter.Category !=null)
{
c.CreateAlias("Categories", "cat")
.Add(Restrictions.Eq("cat.Id", filter.Category.Id));
}
DetachedCriteria crit = DetachedCriteria.For(typeof(CommentData), "comment")
.SetProjection(Projections.ProjectionList()
.Add(Projections.Count("comment.Id").As("cnt"))
.Add(Projections.GroupProperty("comment.Artwork.Id")));
c.Add(Expression.Gt(Projections.SubQuery(crit), 0));
c.AddOrder(Order.Desc(Projections.SubQuery(crit)));
But it's not what I want.
I want to get all Artworks order by the number of comments (but I don't need to get this number).
Please help me! I'm going crazy!

I don't understand what are you trying to do with this weird SQL but if you need to get all Artworks with number of comments you can try this query:
<query name="ArtworkWithCommentsCount">
SELECT artwork.Name, artwork.Comments.size
FROM Artwork artwork
</query>

If you use NHibernate 3 you could use this code:
var artworks = Session.Query<Artwork>().OrderBy(a => Comments.Count);
Or you could use HQL:
Session.CreateQuery("from Artwork a order by size(a.Comments)")

Try Detached Criteria. Take a look at this blogpost.

Related

Fluent Nhibernate generating incorrect query resulting in Cartesian product instead of single row

After trying numerous things, I am still unable to figure out the right way to query the following relationship using Fluent NHibernate.
This would otherwise have been an easier task if I had to write SQL queries. Hope to get some better advice to avoid N+1 issue and unoptimized auto-generated SQL queries.
I have the following relationship which goes something as below :
Tags can have media content (Images of various predefined sizes, videos, documents etc.) associated with it.
- 1 Tags can have multiple Media Items mapped to it (Lets's say images with dimension 32x32, 64x64, 600x100, 0 or more Videos)
- Every media item is mapped to a media description which helps in identifying the size and type of the media
- The same media item can be used by a different tag. Example, having a generic image for all tags which do not have any icons.
Entities:
Media
public class Media:IEntity
{
private ICollection<TagMedia> _tagMedia;
public virtual int Id { get; set; }
public virtual string FilePath { get; set; }
public virtual MediaType MediaType { get; set; }
public virtual ICollection<TagMedia> TagMedia
{
get { return _tagMedia?? (_tagMedia= new List<TagMedia>()); }
protected set { _tagMedia= value; }
}
}
Tag
public class Tag:IEntity
{
private ICollection<TagMedia> _tagMedia;
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<TagMedia> TagMedia
{
get { return _tagMedia?? (_tagMedia= new List<TagMedia>()); }
set { _tagMedia= value; }
}
}
TagMedia
public class TagMedia :IEntity
{
public virtual int Id { get; set; }
public virtual Media Media { get; set; }
public virtual Tag Tag { get; set; }
public virtual DateTime AddedOn { get; set; }
}
MediaType
public class MediaType:IEntity
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
}
Mappings
MediaMapping
public class MediaMapping : IAutoMappingOverride<Media>
{
public void Override(AutoMapping<Media> mapping)
{
mapping.Map(c => c.FileName).CustomSqlType("varchar(60)").Not.Nullable();
}
}
TagMapping
public class TagMapping : IAutoMappingOverride<Tag>
{
public void Override(AutoMapping<Tag> mapping)
{
mapping.HasMany<TagMedia>(c => c.TagMedia)
.KeyColumn("TagId")
.Cascade.SaveUpdate()
.BatchSize(25);
mapping.BatchSize(25);
mapping.DynamicUpdate();
mapping.DynamicInsert();
}
}
TagMediaMapping
public class TagMediaMapping : IAutoMappingOverride<TagMedia>
{
public void Override(AutoMapping<TagMedia> mapping)
{
mapping.Map(c=>c.AddedOn);
}
}
Query:
The following query gets the cartesian product of all media mapped to the tag and does not eliminate records other than "Icon-16x16". I expect the ORM to return no more than one row.
Any help would be highly appreciated.
_session.Query<Tag>()
.FetchMany(x => x.TagMedia)
.ThenFetch(x => x.Media)
.ThenFetch(x=>x.MediaType)
.Where(c => c.Id == id
&& c.TaxonomyMedia.Any(x=>x.Media.MediaType.Description== "Icon-16x16"))
.SingleOrDefault();
Generated SQL:
exec sp_executesql N'select *
from [Tag] Tag0_ left outer join [TagMedia] Tagme1_ on Tag0_.TagId=Tagme1_.TagId
left outer join [Media] media2_ on Tagme1_.MediaId=media2_.MediaId
left outer join [MediaType] mediatype3_ on media2_.MediaTypeId=mediatype3_.MediaTypeId
where Tag0_.TagId=#p0
and (exists (select Tagme4_.TagMediaId
from [TagMedia] Tagme4_ inner join [Media] media5_ on Tagme4_.MediaId=media5_.MediaId inner join [MediaType] mediatype6_ on media5_.MediaTypeId=mediatype6_.MediaTypeId where Tag0_.TagId=Tagme4_.TagId and mediatype6_.MediaTypeDescription=#p1))',N'#p0 int,#p1 nvarchar(4000)',#p0=102,#p1=N'Icon-16x16'
go
the SingleOrDefault function is a LINQ function and not implemented by NHibernate. Use .Take(1)
I think the solution is:
_session.Query<Tag>()
.FetchMany(x => x.TagMedia)
.ThenFetch(x => x.Media)
.ThenFetch(x=>x.MediaType)
.Where(c => c.Id == id
&& c.TaxonomyMedia.Any(x=>x.Media.MediaType.Description== "Icon-16x16"))
.Take(1)
.SingleOrDefault();

Linq - How to to create a list of categories which are included in another table

I am trying to select from a list of categories where it matches the category type of a list of items using linq. IE, from a list of all the FIGstationeryCategories, select only the ones where the FiGStationeryType has a matching category from an already filtered list. The models are listed below.
public class FIGstationeryType
{
public int Id { get; set; }
public virtual FIGstationeryCategory Category { get; set; }
public virtual FIGcompany Company { get; set; }
public decimal Height { get; set; }
public decimal Width { get; set; }
public virtual FIGstationeryType Template { get; set; }
public bool DoubleSided { get; set; }
}
public class FIGstationeryCategory
{
public int Id { get; set; }
public string Name { get; set; }
public decimal MaxZoom { get; set; }
public ICollection<FIGstationeryType> StationeryItems { get; set; }
}
I have been going around in circles with this, and any help will be much appreciated. I haven't got very far! The first line of code works fine, it is the second one I am struggling with.
var listOfItems = db.StationeryTypes
.Where(C => C.Company.Users.Any(u => u.UserId == WebSecurity.CurrentUserId))
.ToList();
var categoryList = db.StationeryCategories
.Where(listOfItems
Any help would be much appreciated.
var listOfCategories =
(from o in listOfItems select o.Category.Name).Distinct().ToList();
When I thought about it (After watching 3 hours of linq videos last night), I realised that the listOfItems already held all the categories which where in use, so I didn't need to query and compare the two tables, just draw the relevant values from the list I already had.
I am not entirely sure how you want to select your categories but this probably goes a little way:
var categoryList = db.StationeryCategories
.*Select*(x => listOfItems.Where(y => y.Category == x)
.FirstOrDefault());
Can you clarify if this is the criteria you are after?

Join nested query NHibernate

How do you do this query in NHibernate, with criteria or QueryOver?
select r.relationshipidentifier, r.publicrelationshipid
from relationship r
inner join (
select max(asofdate) as asofdate, s.relationshipidentifier
from relationship s
group by s.relationshipidentifier
) m
on r.asofdate = m.asofdate and r.relationshipidentifier = m.relationshipidentifier
Assume
public class Relationship {
public virtual Guid Id { get; set; }
public virtual DateTime AsOfDate { get; set; }
public virtual Guid RelationshipIdentifier { get; set; }
public virtual Guid PublicRelationshpId { get; set; }
}
the inner join can be described by a subquery.
something to the effect of -
QueryOver.Of<Relationship>()
.SelectList(list =>
list.SelectMax(r => r.AsOfDate),
list.GroupProperty(r=> r.RelationshipIdentifier)
)
and you can use the WithSubquery method of QueryOver to inner join to that subquery.
see this article for further details on QueryOver API.

Workaround for selectmany in ravendb using client api

I have a ravendb class like such:
public class Student
{
public string Id { get; set; }
public string TopLevelProperty { get; set; }
public Dictionary&ltstring, string&gt Attributes { get; set; }
public Dictionary&ltstring,List&ltDictionary&ltstring, string&gt&gt&gt CategoryAttributes { get; set; }
}
and a document like so:
The following linq will not work due to a selectmany:
test = (from student in session.Query()
from eduhistory in student.CategoryAttributes["EducationHistory"]
where eduhistory["StartYear"] == "2009"
select student).ToList();
How can I get all students where StartYear == 2009?
This does it :
test = session.Advanced.LuceneQuery()
.Where("CategoryAttributes.EducationHistory,StartYear:2009")
.ToList();
Notice the comma rather than a dot after EducationHistory. This indicates that we are looking at the list to find a property in one of the items named StartYear.
If I wanted greater than :
test = session.Advanced.LuceneQuery()
.Where("CategoryAttributes.EducationHistory,StartYear:[2009 TO null]")
.ToList();
etc etc.
This should work:
test = (from student in session.Query()
where student.CategoryAttributes["EducationHistory"].Any(edu => edu["StartYear"]== "2009" )
select student).ToList();

Criteria API and left join on a dictionary/map

I have the following classes:
public class Item
{
public int Id { get; set; }
public IDictionary<int, ItemLocal> { get; protected set; }
public ICollection<string> Tags { get; set; }
public int DefaultLanguageId { get; set; }
public DateTime Start { get; set; }
}
public class ItemLocal
{
public virtual Item Parent { get; set; }
public virtual int LanguageId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
These map to the tables Item, ItemTag and ItemLocal. I want to make the following query via the criteria api:
select
i.Id,
i.Start,
l.Title
from
Item i
left join ItemLocal l on i.Id = l.ParentId and i.DefaultLangaugeId = l.LanguageId
order by
l.Title,
i.Id
But I dont know how to perform the left join with the nhibernate criteria api. Especially with the usage of the default language selection.
Any help is very appreciated.
I have found a solution:
Session
.CreateCriteria<Item>("i")
.CreateCriteria("Localization", "l", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Add(Restrictions.Disjunction()
.Add(Restrictions.EqProperty("i.DefaultLanguageId", "l.LanguageId"))
.Add(Restrictions.IsNull("l.LanguageId"))
)
.AddOrder(Order.Asc("l.Title"))
.AddOrder(Order.Asc("w.Id"));
This seems to work but results in a query with a workaround WHERE clause. Would rather see that it could render a SQL query where the restrictions defined in the disjunction could be part of the OUTER LEFT JOIN ... ON ... statement.