Can I set NHibernate's default "OrderBy" to "CreatedDate" and not "Id"?
This is a strange question that I find.
Can I get NHibernate to ask SQL to sort data by CreateDate by default if I haven't set OrderBy in my HQL or criteria? I am curious to see if this kind can be done at the DB level to prevent LINQ.
The reason is because I am using GUIDs for IDs and when I do something like this:
Sheet sheet = sheetRepository.Get(_someGUID);
IList<SheetLineItems> lineItems = sheet.LineItems;
to get all the lineItems, they are returned in any arbitrary way that sorts the SQL which retrieves what I believe is a GUID. I will add ordinals to my positions at some point, but for now I just want to use CreateDate as the sort criterion. I don't want to be forced to do:
IList<SheetLineItem> lineItems = sheetLineItemRepository.GetAll(_sheetGUID);
and then writing this method to sort by CreateDate. I believe that if everything is sorted by CreateDate by default, this will be fine unless specifically stated otherwise.
a source to share
No, you cannot. The best solution (as you noted in the comment) is to set the order attribute on the collection mapping . Note that the value must be set to the name of the database column, not the name of the property.
a source to share
You don't need to write a method to sort, just use the LINQ OrderBy extension method:
sheetLineItemRepository.GetAll(_sheetGUID).OrderBy(x => x.CreatedDate);
You can put a clustered index on CreatedDate
in the database and then you will probably get records in that order, but you definitely shouldn't rely on it.
a source to share