NHibernate Query w/ criteria against collection items - nhibernate

I have the following configuration in the database.
Database Schema
I want to be able to query all the Individuals where they are either an employee or vendor. Of the examples I've seen I can't get any of them to work. The code doesn't throw an error, it just doesn't bring back any records.
Here are the DTO's
public class Individual
{
public virtual int Sid { get; set;}
public virtual string Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string MiddleInitial { get; set; }
public virtual ISet<Company> Companies { get; set; }
}
public class Company
{
private bool _inactive;
public virtual int Sid { get; set; }
public virtual string Name { get; set; }
public virtual IList<Individual> Individuals { get; set; }
public virtual bool Active
{
get { return !_inactive; }
set { _inactive = value; }
}
public virtual bool IsVendor { get; set; }
}
public class IndividualCompany
{
public virtual Individual Individual { get; set; }
public virtual Company Company { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
IndividualCompany key = obj as IndividualCompany;
if (key == null)
return false;
if (Individual.Sid == key.Individual.Sid && Company.Sid == key.Company.Sid)
return true;
return false;
}
public override int GetHashCode()
{
int hash = 13;
hash = 7 * hash * Individual.Sid.GetHashCode();
hash = 7 * hash * Company.Sid.GetHashCode();
return hash;
}
}
I have another function that uses these 3 tables and all my Individuals and the collection of Companies gets populated just fine so I know the mapping is working. I just don't know how to add criteria on the child record.
string sql = "from Individual i" +
" inner join fetch i.Companies";
return _session.CreateQuery(sql).List<Individual>();
Here's the query I'd like it to produce.
select i.*
from individual i inner join individual_company_assoc ica
on i.individual_sid = ica.individual_sid
inner join company c
on ica.company_sid = c.company_sid
where c.is_vendor = 0
Here's what I've tried:
public IList<Individual> Get(bool vendorsOnly)
{
try
{
return _session.CreateCriteria<Individual>()
.CreateAlias("Company", "c")
.Add(Restrictions.Eq("c.IsVendor", vendorsOnly))
.List<Individual>();
}
catch (NHibernate.HibernateException)
{
throw;
}
}
I'm not using FluentNHibernate. Any help would be greatly appreciated.

An nHibernate query will not automatically join with other tables, even if you reference columns of a joining table in the query.
You must explicitly state that the query should join with the Company table.
Here's an example which will work. It uses the QueryOver syntax, which is a type-safe wrapper around the Criteria API.
return _session
.QueryOver<Individual>()
.JoinQueryOver<Company>(i => i.Companies>())
.Where(c => c.IsVendor == vendorsOnly)
.TransformUsing(NHibernate.Transform.Transformers.DistinctRootEntity)
.List();

Related

.net Core Many to Many relationship

I am trying to determine what would be the smartest way to accomplish this. I may be way way overthinking what I am trying to do, but here goes.
I have the following entities, simplified
public class Meet
{
public int Id { get; set; }
//various properties
public List<MeetComp> Competitors { get; set; }
}
public class Competitor
{
public int Id { get; set; }
// various properties
public List<MeetComp> Meets { get; set; }
[ForeignKey("GymManager")]
public int GymManagerId { get; set; }
public GymManager GymManager { get; set; }
}
public class GymManager
{
public int Id { get; set; }
//various properties
public List<Competitor> Competitors { get; set; }
}
public class MeetComp
{
public int Id { get; set; }
[ForeignKey("Competitor")]
public int CompetitorId { get; set; }
public Competitor Competitor { get; set; }
[ForeignKey("Meet")]
public int MeetId { get; set; }
public Meet Meet { get; set; }
}
So I am creating a razor page where I get a specific Gymmanager and load all the related competitors to display in a list, which I have working just fine.
However I need another list (on the same page) of the related competitors of the Gymmanager but also who have an entry in the "MeetComp" table by a specific meetid. So List #1 is all of my Competitors and List #2 is all of my Comptetitors that are registered for that Meet.
Would it be smarter to have EF pull the data I get the data the first time with a ThenInclude()? Then I write some logic to determine if they get added to list #2? or should I make another trip to the Database? Then if I do make another trip to the database is there an easy to way to query for the List of CompId's I already have?
So here's what I ended up doing is making another trip to the DB.
public async Task<IActionResult> GetRegisteredComps(List<int> Comps, int meetid)
{
if(Comps.Count == 0)
{
return Ok();
}
if(meetid == 0)
{
return BadRequest();
}
var query = _context.MeetsComps.Include(c => c.Competitor)
.AsQueryable();
query = query.Where(c => c.MeetId == meetid);
query = query.Where(c => Comps.Contains(c.CompetitorId));
var results = await query.ToListAsync();
return Ok(results);
}

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();

Retrieve Big data using EF

I have two tables "Kelime"
public class Kelime
{
public int ID { get; set; }
public string Word { get; set; }
public DateTime Date { get; set; }
}
and "Anlam"
public class Anlam
{
public int ID { get; set; }
public string Meaning { get; set; }
public DateTime Date { get; set; }
public int Kelimesi_ID { get; set; }
[ForeignKey("Kelimesi_ID")]
public virtual Kelime kelime { get; set; }
}
Both tables contains more than 80k datas. I don't think they are very big but I am having problem on this query:
Repository<Kelime> _rk = new Repository<Kelime>();
Repository<Anlam> _ra = new Repository<Anlam>();
IEnumerable<int> kelimeIdler = _ra.All().Select(s => s.Kelimesi_ID).Distinct();
int _kelimecik= _rk.Find(w => !kelimeIdler.Contains(w.ID)).ID;
or Kelime _kelimecim = _rk.All().Where(w => !kelimeIdler.Contains(w.ID)).FirstOrDefault();
I am trying to take the "Kelime", "Kelime List" or its "id" it doesn't matter which are not in my "Anlam" table. There is a timeout when it comes to "contains" part. I tried to write non clustered index but it doesn't accept subquery. What should I do to achieve what I want? Thank you very much.
private static DataContext _context;
public static DataContext ContextOlustur()
{
if (_context == null)
{
_context = new DataContext();
}
return _context;
}
Adding this pattern to my data context class solved my problem. Because my queries used two different context that's why it was getting problem to connect database and got time out. This pattern prevents creating another context.

RavenDB TransformResults

I'm trying to use the TransformResults feature, and I can't get it to work. I'm not totally sure I understand this feature, perhaps there is another way to solve this problem. What I want is just the Id from the Order and the email addesses from the Customer and the Entrepreneur. I am happy for all tips that can take me in the right direction. Here is my code.
Document
public class OrderDocument
public string Id {get; set }
public EntrepreneurInfo EntrepreneurInfo { get; set; }
public CustomerInfo CustomerInfo { get; set; }
public OrderStatus CurrentOrderStatus { get; set; }
}
Info classes
public class EntrepreneurInfo
{
public string EntrepreneurDocumentId { get; set; }
public string Number { get; set; }
public string Name { get; set; }
}
public class CustomerInfo
{
public string CustomerDocumentId { get; set; }
public string Number { get; set; }
public string Name { get; set; }
}
The info classes are just subsets of a Customer and Entrepreneur documents respectively.
The Customer and Entrepreneur documents inherits from a base class ( AbstractOrganizationDocument) that has the EmailAddress property.
My Index
public class OrdersApprovedBroadcastingData :
AbstractIndexCreationTask<OrderDocument, OrdersApprovedBroadcastingData.ReduceResult>
{
public OrdersApprovedBroadcastingData()
{
this.Map = docs => from d in docs
where d.CurrentOrderStatus == OrderStatus.Approved
select new
{
Id = d.Id,
CustomerId = d.CustomerInfo.CustomerDocumentId,
EntrepreneurId = d.EntrepreneurInfo.EntrepreneurDocumentId
};
this.TransformResults = (db, orders) => from o in orders
let customer = db.Load<CustomerDocument>(o.CustomerId)
let entrepreneur = db.Load<EntrepreneurDocument>(o.EntrepreneurId)
select
new
{
o.Id,
o.CustomerId,
CustomerEmail = customer.EmailAddress,
o.EntrepreneurId,
EntrepreneurEmail = entrepreneur.EmailAddress
};
}
public class ReduceResult
{
public string Id { get; set; }
public string CustomerId { get; set; }
public string CustomerEmail { get; set; }
public string EntrepreneurId { get; set; }
public string EntrepreneurEmail { get; set; }
}
}
If I look at the result of this Index in Raven Studio I get null values for all fields except the Id. And finally here is my query.
Query
var items =
this.documentSession.Query<OrdersApprovedBroadcastingData.ReduceResult, OrdersApprovedBroadcastingData>()
.Select(x => new OrdersToBroadcastListItem
{
Id = x.Id,
CustomerEmailAddress = x.CustomerEmail,
EntrepreneurEmailAddress = x.EntrepreneurEmail
}).ToList();
Change your index to:
public class OrdersApprovedBroadcastingData : AbstractIndexCreationTask<OrderDocument>
{
public OrdersApprovedBroadcastingData()
{
Map = docs => from d in docs
where d.CurrentOrderStatus == OrderStatus.Approved
select new
{
};
TransformResults = (db, orders) =>
from o in orders
let customer = db.Load<CustomerDocument>(o.CustomerInfo.CustomerDocumentId)
let entrepreneur = db.Load<EntrepreneurDocument>(o.EntrepreneurInfo.EntrepreneurDocumentId)
select new
{
o.Id,
CustomerEmailAddress = customer.EmailAddress,
EntrepreneurEmailAddress = entrepreneur.EmailAddress
};
}
}
Your result class can simply be the final form of the projection, you don't need the intermediate step:
public class Result
{
public string Id { get; set; }
public string CustomerEmailAddress { get; set; }
public string EntrepreneurEmailAddress { get; set; }
}
You don't have to nest this class in the index if you don't want to. It doesn't matter either way. You can query either with:
var items = session.Query<Result, OrdersApprovedBroadcastingData>();
Or with
var items = session.Query<OrderDocument, OrdersApprovedBroadcastingData>().As<Result>();
Though, with the first way, the convention tends to be to nest the result class, so really it would be
var items = session.Query<OrderDocument.Result, OrdersApprovedBroadcastingData>();
Note in the index map, I am not including any properties at all. None are required for what you asked. However, if you want to add a Where or OrderBy clause to your query, any fields you might want to filter or sort on should be put in there.
One last thing - the convention you're using of OrderDocument, CustomerDocument, EntrepreneurDocument, is a bit strange. The usual convention is just Order, Customer, Entrepreneur. Think of your documents as the persisted form of the entities themselves. The convention you are using will work, it's just not the one usually used.

Sorting with Mathematical Formula with nHibernate (C#)

public class Feedback
{
public virtual int Id { get; private set; }
public virtual string ContentText { get; set; }
public virtual DateTime FeedbackDate { get; set; }
public virtual Student student { get; set; }
}
My Feedback Class.
public class Student
{
public virtual int Id { get; private set; }
public virtual int NumberOfStars { get; set; }
public virtual IList<Feedback> Feedbacks { get; private set; }
public Student()
{
Feedback = new List<Feedbacks>();
}
}
My Student Class
public class Course
{
public virtual int Id { get; set; }
// bla bla bla
public virtual IList<Student> Students { get; private set; }
public Course()
{
Students = new List<Student>();
}
public IList<Student> SortBy(string type)
{
// some other sorting
else if (type.Equals("popular")){
sortedStudents = session.CreateCriteria(typeof(Student))
.CreateAlias("Student", "s")
.CreateAlias("s.Feedback", "f")
.AddOrder(Order.Desc( -------- ))
.List();
}
return (IList<Student>) sortedStudents;
}
}
My Course class
I want sort students in a Course with method SortBy :
if type is x i will sort with following rule
(Students.Feedback.Count)*5 + Student.NumberOfStars)
How ?
HQL:
List<Student> sortedStudents = session
.CreateQuery(
#"from Students student
where student.Course == :course
order by size(student.Feedbacks) * 3 + student.NumberOfStars")
.SetEntity("course", course)
.List<Student>();
size is a HQL function. See the chapter "Expressions" in the NH documentation.
You may also select it with Criteria and sort it with Linq.
Edit
Just saw that you use it in a property and you may have the students already in memory.
You don't need a query, just to sort it.
return students
.OrderBy(x => x.Feedback.Count * 5 + x.NumberOfStars)
.ToList();
Query with LINQ
IList sortedStudents = (from student in this.Students
where student.Course == this
orderby (student.Feedbacks.Count*3 + student.NumberOfStars)
select student).ToList();