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.
Related
I have three tables that are represented here by objects and that convert to SQLite tables. The CategoryGroup table has multiple Categories and each Category has multiple Phrases.
public class CategoryGroup
{
[PrimaryKey, NotNull]
public int Id { get; set; }
public string Name { get; set; }
public bool Selected { get; set; }
}
public class Category : ICategory
{
[PrimaryKey, NotNull]
public int Id { get; set; }
public int CategoryGroupId { get; set; }
public string Name { get; set; }
public bool Selected { get; set; }
}
public class Phrase : IPhrase
{
public int CategoryId { get; set; }
[PrimaryKey, NotNull]
public string PhraseId { get; set; }
public string English { get; set; }
public string Romaji { get; set; }
public string Kana { get; set; }
public string Kanji { get; set; }
public int Modified { get; set; }
}
What I would like to do is to join these three tables and group them by CategoryGroup and then report on the Selected count.
I created this SQL but I am not sure about the three table join.
SELECT CG.Id, CG.Name, COUNT(*) AS PhraseCount
FROM CategoryGroup AS CG
FROM Category AS C
JOIN CG.Id = P.CategoryId
JOIN Phrase AS P ON C.Id = P.CategoryId
GROUP BY CG.Id
ORDER BY CG.Name
Can someone give me advice on how I can do the join?
You've got the syntax wrong. You'd select from one table, join another on some criteria so that records match, then join the third on some other criteria linking this table.
SELECT CG.Id, CG.Name, COUNT(*) AS PhraseCount
FROM CategoryGroup AS CG
JOIN Category AS C ON C.CategoryGroupId = CG.Id
JOIN Phrase AS P ON P.CategoryId = C.Id
GROUP BY CG.Id
ORDER BY CG.Name;
You may have to GROUP BY CG.Id, CG.Name instead. This is not necessary by the SQL Standard, because the category group name is functionally dependent on the category group ID, but some DBMS demand this though (because they find it too hard to determine functional dependence). I don't know about SQLite in this respect.
My fluent mappings are:
HasOne(x => x.User).ForeignKey("fOM_User_Id").Cascade.None();
My class is:
public class OrganizationMember
{
public virtual int MemberId
{
get;
set;
}
public virtual int OrganizationId
{
get;
set;
}
public virtual int UserId
{
get;
set;
}
public virtual User User { get; set; }
public virtual string Permissions
{
get;
set;
}
public virtual System.DateTime DateAdded
{
get;
set;
}
}
I run the query:
var assignableusers = _session.Query<OrganizationMember>().Where(x => x.OrganizationId == CurrentProject.OwnerID)
.Select(x => x.User.Login);
which generated:
select user1_.fU_Login as col_0_0_ from tOrganization_Members organizati0_
left outer join tUsers user1_ on **organizati0_.fOM_Id**=user1_.fU_UserID
where organizati0_.fOM_Organization_Id=7
but it should have been:
select user1_.fU_Login as col_0_0_ from tOrganization_Members organizati0_
left outer join tUsers user1_ on **organizati0_.fOM_User_Id** = user1_.fU_UserID
where organizati0_.fOM_Organization_Id = 7
How can I fix this in my mappings?
from organizati0_.fOM_Id to organizati0_.fOM_User_Id
I don't think your relationship is a true one to one.
http://www.jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/
You should probably use a References (many-to-one) instead.
References(x => x.User).Cascade.None();
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.
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.
I have an entitiy as
public class ClaimDetail
{
public virtual int Id { get; set; }
public virtual Claim Claim {get;set;}
public virtual string ServiceDateFrom { get; set; }
public virtual string DateCreated { get; set; }
public virtual ClaimDetail ParentClaimDetail { get; set; }
}
I want apply left outer join on on this table as
SELECT cd.ClaimDetailId ,
cd.ParentClaimDetail_id ,
cds.ClaimDetailId ,
cds.ParentClaimDetail_id ,
cds.DateCreated,
cd.DateCreated
FROM ClaimDetail AS cd left JOIN ClaimDetail AS cds ON
cd.ClaimDetailId = cds.ParentClaimDetail_id
I am implementing this query in nhibernate as
ICriteria query = session.CreateCriteria(typeof(ClaimDetail), "cd")
.CreateAlias("ParentClaimDetail", "cds", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
query.SetProjection
(
Projections.ProjectionList()
.Add(Projections.Property("cd.ParentClaimDetail.Id"), "sks")
.Add(Projections.Property("cd.Id"),"cdid")
.Add(Projections.Property("cds.DateCreated"), "dc1")
.Add(Projections.Property("cd.DateCreated"), "dc2")
.Add(Projections.Property("cds.ParentClaimDetail.Id"), "cds")
.Add(Projections.Property("cds.Id"),"cdsid")
);
but this create criteria creating query as
SELECT cd.ClaimDetailId ,
cd.ParentClaimDetail_id ,
cds.ClaimDetailId ,
cds.ParentClaimDetail_id ,
cds.DateCreated,
cd.DateCreated
FROM ClaimDetail AS cd left JOIN ClaimDetail AS cds ON
cd.ParentClaimDetail_id = cds.ClaimDetailId
can any one help me.
Thanks in advance
It looks like you want to use a named query instead. The named query goes in the mapping XML and you can feed input into it. In addition, you can select a custom class to represent the entities of the resulting data.