NHibernate aggregate query for one-to-many relation - nhibernate

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.

Related

Trouble formulating inner joins using NHibernate Linq query

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;

Fluent NHibernate one-to-many with intervening join table?

I'm having trouble getting the Fluent Nhibernate Automapper to create what I want. I have two entities, with a one-to-many relationship between them.
class Person
{
public string name;
IList<departments> worksIn;
}
class Department
{
public string name;
}
The above is obviously bare bones, but I would be expecting to generate the fleshed out schema of:
Person{id, name}
Department{id, name}
PersonDepartment{id(FK person), id(Fk Department)}
Unfortunately, I am instead getting:
Person{id, name}
Department{id, name, personid(FK)}
I don't want the FK for Person included on the department table, I want a separate join/lookup table (PersonDepartment above) which contains the primarykeys of both tables as a composite PK and also Fks.
I'm not sure if I am drawing up my initial classes wrong (perhaps should just be LIst workIn - representing ids, rather than List worksIn), or if I need to manually map this?
Can this be done?
The way the classes have been structured suggests a one-to-many relationship (and indeed that's how you describe it in your question), so it should not be a surprise that FNH opts to model the database relationship in that way.
It would be possible, as you suggest, to manually create a many-to-many table mapping. But, is this definitely what you want?
I tend to find that pure many-to-many relationships are quite rare, and there is usually a good case for introducing an intermediate entity and using two one-to-many relationships. This leaves open the possibility of adding extra information to the link (e.g. a person's "primary" department, or perhaps details of their office within each of their departments).
Some example "bare-bones" classes illustrating this kind of structure:
public class Person
{
public int Id { get; set;}
public string Name { get; set;}
public IList<PersonDepartment> Departments { get; set; }
}
public class PersonDepartment
{
public int Id { get; set; }
public Person Person { get; set; }
public Department Department { get; set; }
public bool IsPrimary { get; set; }
public string Office { get; set; }
}
public class Department
{
public int Id { get; set; }
public IList<PersonDepartment> Personnel { get; set; }
public string Name { get; set; }
}

How to use components in fluent nhibernate

I'm working on a legacy MySql database and have the following entities:
public class Company
{
public int Id { get; set;}
public string Address { get; set; }
public string City { get; set; }
}
public class CompanyDepartment
{
public int Id { get; set;}
public string Address { get; set; }
public string City { get; set; }
}
The idea is that a company only use the department class if it has more than one department.
Right now I'm trying to make a company/department search, this means I need a list of all departments and therefore I need to "create" departments of all the companies that only has one department, and therefore don't have a entry in CompaynyDepartment.
To do this I was thinking of use components in fluent NHibernate, but I'm not sure I can join the real departments with the fake ones?
Is there a better approach to this problem? It's not an option to change the database structure.
I ended up changing the database structure

Custom Fill Collection in NHibernate

I'm using NHibernate in my web app and it is mapped with my database. I have a model, somthing like this:
public class Company {
public virtual string Name { get; set; }
public virtual IList<Employee> Employeers { get; set; }
}
public class Employee {
public virtual string Name { get; set; }
public virtual DateTime Birthday { get; set; }
/* other properties */
public virtual Company Company { get; set; }
}
PS: it's not real model but it works for my samples/doubts...
I'm using HQL to get my objects and I'd like to know if is there any way to:
1) Get a Company object and fill the Employeers Colletion with Top 10 Employeers Ordered by Birthday Desc ?
2) Is there any way to, when collection is filled, fill it with only some fields like Name and Birthday? I have a lot of properties that I won't use in my view. I can create a DTO for this but I don't know how to do!
Thanks
Persistent collections and entities represent the current state; they can't have just a part of that (think about it: if they did, how would NH track changes?)
So, in both cases, the answer is queries and DTOs. You can easily retrieve the data you need with HQL:
class EmployeeNameAndBirthDay
{
public string Name { get; set; }
public DateTime Birthday { get; set; }
}
public IList<EmployeeNameAndBirthDay> GetTopEmployees(Company company)
{
return session.CreateQuery(#"
select Name as Name,
Birthday as Birthday
from Employee
where Company = :company
order by Birthday desc
")
.SetParameter("company", company)
.SetMaxResults(10)
.SetResultTransformer(
Transformers.AliasToBean<EmployeeNameAndBirthDay>())
.List<EmployeeNameAndBirthDay>();
}

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.