Trouble formulating inner joins using NHibernate Linq query - nhibernate

Using NHibernate 3.3.1.400, I'm having problems expressing what is a simple SQL statment using NHibernate's Linq provider.
My domain model looks like this:
public class Site
{
public virtual Guid Id {get; set;}
public virtual string Name {get; set;}
}
// a site has many filers
public class Filer
{
public virtual Guid Id {get set;}
public virtual Site Site {get; set;}
public virtual string Name {get; set;}
}
// a filer has many filings
public class Filing
{
public virtual Guid Id {get set;}
public virtual Filer Filer {get; set;}
public virtual DateTime FilingDate {get; set;}
}
//a filing has many items
public class Item
{
public virtual Guid Id {get set;}
public virtual Filing Filing {get; set;}
public virtual DateTime Date {get; set;}
public virtual decimal Amount {get; set;}
}
public class SearchName
{
public virtual Guid Id {get set;}
public virtual string Name {get; set;}
}
// there are potentially many NameLink objects tied to a single search name
public abstract class NameLink
{
public virtual Guid Id {get set;}
public virtual SearchName SearchName {get; set;}
}
public class NameLinkToFiler: NameLink
{
public virtual Filer Filer {get; set;}
}
public class NameLinkToItem: NameLink
{
public virtual Item Item {get; set;}
}
My query is supposed to return a list of matching Item elements:
var query = session.Query<Item>()
.Where(x => x.Filing.Filer.Site == mySite);
The joins connecting Site -> Filer -> Filing -> Item are working great through my mappings, but the problems start when I try to join the NameLinkToFiler or NameLinkToItem classes based on user input.
If the user wants to filter the Query results with a filer name, I want to join the results of the Item query with the results of this query:
var filerNameQuery = session.Query<NameLinkToFiler>()
.Where(q=>q.SearchName.Contains('some name'));
I want the results of the NameLinkToFiler.Filer property to join the Item.Filing.Filer property, so my list of returned items is reduced.
Note: The 'Contains' keyword above is a full-text index search I'm using as described here. It's working fine, and let's just assume the filerNameQuery is an IQueryable<NameLinkToFiler>.
It's pretty easy to do this in straight SQL:
select filer.Name, filing.FilingDate, filer.Name, item.Date, item.Amount
from Search_Name searchNameForFiler, Search_Name searchNameForItem, Name_Link_Filer nameLinkFiler,
Name_Link_Item nameLinkItem, Item item, Filing filing, Filer filer, Site s
where
contains(searchNameForFiler.Name, :filerName) and searchNameForFiler.Id = nameLinkFiler.SearchNameId and nameLinkFiler.FilerId = filer.Id and
contains(searchNameForItem.Name, :itemName) and searchNameForItem.Id = nameLinkItem.SearchNameId and nameLinkItem.ItemId = item.Id
and item.FilingId = filing.Id
and filing.FilerId = filer.Id
and filing.SiteId = :site
...but I don't want to lose the compile-time checks for this sort of query.
Thanks.

Apparently, the answer is to not use lambda syntax.
This works fine:
query = from t in parentItemQuery
join l in Session.Query<NameLinkToFiler>() on t.Filing.Filer.Id equals l.Filer.Id
join n in Session.Query<SearchName>() on l.SearchName.Id equals n.Id
where sn.Contains(request.FilerName)
select t;

Related

Database structure with Entity Framework Core - Discrimination

I have the following problem:
I'd like to be able sell a few kinds of products in my store.
I have created following infrastructure:
public class Product
{
public int Id {get; set;}
public string Name {get; set;}
...
public int? ParentId {get; set;}
public Product Parent {get; set;}
public ICollection<Product> Children {get; set;}
}
public class AudioFile : Product
{
public string SomeValue {get; set;}
}
public class DocummentFile : Product
{
public string SomeString {get; set;}
}
In fluent mapping I am using discriminator.
So, to Select the amount of documment files without a parent, I am doing something like this:
DbContext.DocummentFiles.Count(x=> !x.ParentId.HasValue)
It works with Audio files too.
I'd like to select sample dictionary of all children for some audio file with SomeValue:
DbContext.AudioFiles
.Where(x => x.ParentId == 222)
.Select(x => x.Children
.OfType<AudioFile>()
.ToDictionary(t => t.Name, t => t.SomeValue));
Unfortunately, it does not work. I have high CPU usage by the long time, but I have not any result.

NHibernate aggregate query for one-to-many relation

I have next entities:
class Topic
{
public virtual int Id {get; private set;}
public virtual ICollection<Vote> Votes {get; private set; }
}
class Vote
{
public virtual Topic Topic {get; private set;}
public virtual VoteType VotedTo {get; private set;} // enum VotedUp, VotedDown
}
I need to load from the db the next info - all topics (IDs, actually Names, but in this demo case it does not matter) and two more fields CountOfVotedUp, CountOfVotedDown (aggregates). So as I understand in SQL world we need joins, group by, case and count.
Is it posible to get this info with LINQ with less operations with db? I mean N+1, additional selects, connections etc.
All that I tried - is to use NH's LINQ, but it's Query aggregates only on Topic.Id and I could not count any of Votes collection.
Provided you have a summary class to store your result :
public class SummaryDTO
{
public int TopicId { get; set; }
public VoteType TypeOfVote { get; set; }
public int VoteCount { get; set; }
}
then
Vote voteAlias = null;
SummaryDTO result = null;
youNHSession.QueryOver<Topic>()
.JoinAlias(x=> x.Votes, ()=>voteAlias)
.SelectList(
list=>list
.SelectGroup(topic=>topic.Id).WithAlias(()=>result.TopicId)
.SelectGroup(()=>voteAlias.VotedTo).WithAlias(()=>result.TypeOfVote)
.SelectCount(()=>voteAlias.VotedTo).WithAlias(()=>result.VoteCount ))
.TransformUsing(Transformers.AliasToBean<SummaryDTO>())
.List<SummaryDTO>();
I guess that's not exactly what you are looking for, but hope this will set you on a good track.

NHibernate - reference without (always) doing SQL join

I've got a question that may reek of code/design stink but I'm going to ask it anyway.
Say I've got the two trivial classes below:
public class PostOffice
{
public virtual string ZipCode {get; set;}
public virtual string StreetAddress {get; set;}
}
public class Person
{
public virtual in ID {get; protected set;}
public virtual string FirstName {get; set;}
public virtual string LastName {get; set;}
public virtual PostOffice ZipCode {get; set;}
}
and I've used FluentNHibernate to set PostOffice.ZipCode as the ID and provided the mapping override below:
model.Override<Person>(p=> p.References( z=> z.ZipCode, "ZipCode"));
Person.ZipCode in the database is just a VARCHAR and I would normally write some SQL like
SELECT *
FROM Person
WHERE ZipCode = '90210'
but if I have an NHibernate session and do something like the following:
session.Query<Person>().Where(p=>p.ZipCode == '90210')
the resulting SQL (pretty-printed) looks like
SELECT p.*
FROM Person p INNER JOIN PostOffice o
ON p.ZipCode = o.ZipCode
WHERE o.ZipCode = '90210'
My question - if entities are lazy-load by default, is it possible to specify some mapping properties such that NHibernate will generate the SQL without the join? The example is trivial but in my actual situation, the table being joined to has millions of rows so I'd like to avoid the join if possible.
Thanks in advance!
If the fields ZipCode and StreetAddress are actually in the Person table then you need to map PostOffice as a Component and not a Reference.
http://wiki.fluentnhibernate.org/Auto_mapping#Components
Edit:
You may be able to map a separate property to just that ZipCode field.
public class Person
{
public virtual in ID {get; protected set;}
public virtual string FirstName {get; set;}
public virtual string LastName {get; set;}
public virtual string ZipCode { get; set; }
public virtual PostOffice PostOffice {get; set;}
}
model.Override<Person>(p=>
p.References( z=> z.PostOffice, "ZipCode")
.Not.Update()
.Not.Insert()
);

NHibernate Criteria

public class A
{
public string aname {get; set;}
public string aId {get; set;}
public string bId {get; set;}
}
public class B
{
public string bId {get; set;}
public string bname {get; set;}
public string cId {get; set;}
}
public class C
{
public string cId {get; set;}
public string cfirstname {get; set;}
public string clastname {get; set;}
}
public class abcDTO
{
public string aname {get; set;}
public string bname {get; set;}
public string clastname {get; set;}
}
Evetually the query which I am looking is
SELECT a.aid, b.bname, c.clastname FROM A thisa
inner join B thisb on thisa.bid=thisb.bid
inner join C thisc on thisb.cid=thisc.cid and this_.POLICY_SEARCH_NBR like '%-996654%'
The criteria which I am trying is, Please let me know the best possible way to write a criteria so that I can get the abcdto object as result
var policyInsuranceBusiness = DetachedCriteria.For<A>()
.SetProjection(Projections.Property("a.aid"))
.Add(Restrictions.Like("a.aid", "1-SAP-3-996654", MatchMode.Anywhere))
.CreateCriteria("b.bid", "b", JoinType.InnerJoin)
.SetProjection(Projections.Property("b.bname")) // ERROR OUT - COULD NOT RESOLVE PROPERTY
.CreateCriteria("c.cid", "c", JoinType.InnerJoin)
.SetProjection(Projections.Property("c.clastname")); // ERROR - COULD NOT RESOLVE PROPERTY
IList<abcDTO> plo = policyInsuranceBusiness.GetExecutableCriteria(_session).SetResultTransformer(NHibernate.Transform.Transformers
.AliasToBean<abcDTO).List<abcDTO>();
You can't join objects in NHibernate without having a relationship between them defined in the classes and in the mappings.
So taking your example I would estimate the following class definitions:
public class A {
public virtual string Name {get; set;}
public virtual string PolicySearchNumber {get; set;}
public virtual int Id {get; set;}
public virtual B B {get; set;}
}
public class B {
public virtual int Id {get; set;}
public virtual string Name {get; set;}
public virtual C C {get; set;}
}
public class C {
public virtual int Id {get; set;}
public virtual string Firstname {get; set;}
public virtual string Lastname {get; set;}
}
public class abcDTO {
public string int aid {get; set;}
public string aname {get; set;}
public string bname {get; set;}
public string clastname {get; set;}
}
And your Criteria Query would be:
var results = session.CreateCriteria<A>()
.CreateCriteria("B", "b")
.CreateCriteria("b.C", "c")
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Id"), "aid")
.Add(Projections.Property("Name"), "aname")
.Add(Projections.Property("b.Name"), "bname")
.Add(Projections.Property("c.Lastname"), "clastname")
)
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(abcDTO)))
.List<abcDTO>();
You can optionally choose Inner or Left Joins depending on how you handle null references in the Criteria Query.

NHibernate: Discriminators without inheritance

I have the following model:
public class SomeObject1 {
public virtual Guid Id {get; set; }
public string Property1 {get; set; }
}
public class SomeObject2 {
public virtual Guid Id {get; set; }
public string Property2 {get; set;}
}
and the table
SOME_OBJECTS
PK_SOME_OBJECTS Guid
WHICH_OBJECT Integer
PROPERTY1 varchar2
PROPERTY2 varchar2
when the WHICH_OBJECT column = 1 the row contains information for SomeObject1, when the WHICH_OBJECT column = 2 the row contains information for SomeObject2.
How would I go about doing these mappings? I've found the discriminator feature but it seems to only apply when you have subclasses in an inheritance hierarchy.
I'm pretty sure it's not possible to map two unrelated entities to the same table; however, you may be able to map them to two different views that reference the same table.