NHibernate.Mapping.Attributes.Filter - nhibernate

I'm mapping my database tables using NHibernate with NHibernate.Mapping.Attributes library and I got stuck to get the Filter attributes to work.
Suppose a class A that has a set of objects of class B. So, I have, the following:
[NHibernate.Mapping.Attributes.Set(0, Inverse = true, Lazy = NHibernate.Mapping.Attributes.CollectionLazy.False)]
[NHibernate.Mapping.Attributes.Key(1, Column = "ClassAId")]
[NHibernate.Mapping.Attributes.OneToMany(2, Class = "ClassB, Assembly")]
public virtual ISet<ClassB> ClassBs { get; set; }
I want to create a filter on this collection to bring only class B objects that satisfy a given criteria, such as Status = 1.
How can I create such Filter?

The where parameter of the Set mapping should be able help you out. Per the documentation the where parameter:
where: (optional) specify an arbitrary
SQL WHERE condition to be used when
retrieving or removing the collection
(useful if the collection should
contain only a subset of the available
data)
So to filter on Status (assuming Status is a SQL column in the table mapped for ClassB - though this column does not have to be mapped in the NHibernate mapping).
[NHibernate.Mapping.Attributes.Set(0,...., Where = "Status = 1", .....)]
...
public virtual ISet<ClassB> ClassBs { get; set; }

Related

nHibernate Criteria, trying to order by enum name (alphabetical rather than value)

Lets say I have a class that contains a status type, which is defined as an enum like so:
public class MyObject
{
public virtual int Id { get; set; }
public virtual SomeEntity Data { get; set; }
public virtual MyStatusEnum Status { get; set; }
}
public enum MyStatusEnum
{
Active = 1,
Paused = 2,
Completed = 3
}
My mapping done via Fluent nHibernate looks like:
public class MyObjectMap: ClassMap<MyObject>
{
public MyObjectMap()
{
this.Table("my_object_table");
...
this.References(x => x.SomeEntity).Column("some_entity_id").Not.Nullable();
this.Map(x => x.Status).Column("status_type").CustomType<MyStatusEnum>().Not.Nullable();
}
}
Now that the setup is out of the way, my dilemma:
In my repository class, I want to sort all of the MyObject entities by the Status property, which nHibernate persists as an int. However, due to powers beyond my control, I cannot reorder MyStatusEnum so that the enum values are ordered alphabetically. When I create my criteria to select the list of MyObjects, and try to sort it by the Status property, it sorts by the int value of Status.
ICriteria criteria = this.Session.CreateCriteria<MyObject>("obj")
.AddOrder(Order.Asc("Status"))
.List()
I'd really like to be able to order by the enum name. Any ideas would be greatly appreciated.
If you want to sort it in the database, you'd have to sort by a projection with a case statement, but this won't be able to use an index, and might not work depending on your version of NHibernate (there are bugs when sorting by projections that aren't in the select).
Something like this might work:
.AddOrder(Order.Asc(
Projections.SqlProjection(
"CASE {alias}.Status "
+ "WHEN 1 THEN 0 "
+ "WHEN 2 THEN 3 "
+ "WHEN 3 THEN 2 END",
new string[0], new IType[0])))
Another option (better for performance) is to add a property to your class such as StatusOrder which you set equal to the relative position of the current status in your enum and just sort by that field (which you can index).
Yet another option is to define a formula property on your class (ie. specify a formula in the mapping) where the formula specifies a value to sort by depending on the status.
The easy way is to simply refactor your enum so that values and names have the same ordering:
public enum MyStatusEnum
{
Active = 1,
Paused = 3,
Completed = 2
}
But you can always use the List.Sort method to do the job:
enum MyEnum
{
Alpha,
Beta,
Gama
}
static void Main(string[] args)
{
List<MyEnum> list = new List<MyEnum>()
{
MyEnum.Gama,
MyEnum.Beta,
MyEnum.Alpha
};
list.Sort((x, y) => x.ToString().CompareTo(y.ToString()));
}
NHibernate sorts result sets based on how they are stored in the database. From what you said I'm guessing your enums are being stored as integers, hence you won't be able to ask SQL Server to order them by their names because these names are not known by SQL Server.
Unless you store your enums as strings, like discussed in this SO Question, your only option will be to perform the ordering "in memory" and not in the database.

EF4 and intentionally returning only *some* of the properties of an entity

Folks, I know I didn't phrase that title very well, but here's the scenario.
I have a WinForm UI tier, and a WCF middle tier, serving up my EF4 entity objects, which are (of course) mapped to my database tables. Everything works fine.
One of my objects is the Client - and in the Client db table are three varbinary(max) fields for PDF documents. So my entity object has three Byte() properties, one for each document.
But when I load up an initial grid listing the Clients, it's going to drag ALL that PDF data from the MT - making a much bigger payload than I generally need.
With DataSets, I'd write my SQL to not include the PDF binary - but I'd include a Boolean flag field for each to indicate whether there IS one to download if the user wants it. Then I'd load the PDFs via a separate call as needed.
With EF4 - what's the best pattern for this?
First, I'm thinking to put the documents into a child-table/child-objects, so I don't pull it across the tier with the Client. One problem solved.
Second, I suppose I could use partial classes to extend my Client entity object to have the three Boolean properties I want.
Am I on the right track?
I think you have three options:
1) Create a custom class which you project the properties you want into:
public class MySpecialSelection
{
public int ID { get; set; }
public string Name { get; set; }
// more
public bool HasPDFDoc1 { get; set; }
public bool HasPDFDoc2 { get; set; }
public bool HasPDFDoc3 { get; set; }
}
using (var context = new MyContext())
{
var mySpecialSelectionList = context.MyEntities.Where(...some predicate...)
.Select(e => new MySpecialSelection
{
ID = e.ID,
Name = e.Name,
// ...
HasPdfDoc1 = (e.PdfDoc1 != null),
HasPdfDoc2 = (e.PdfDoc2 != null),
HasPdfDoc3 = (e.PdfDoc3 != null),
}).ToList();
// ...
}
Instead of a "named" object you can also project into anonymous types.
Note: This doesn't attach any full model entity to the context, so you won't have any change tracking of entities.
2) Table splitting: It means that you split your single entity into two separate classes which are related by a navigation property. You can map then both entities to a single table in the database. It allows you to load the navigation properties (for instance the binary fields) on request (by lazy, eager or explicite loading). Details about this for EF4.0 are here and for EF4.1 here.
3) Your own proposal: Create separate tables and separate entities which are linked by navigation properties and FK constraints.

How to create a NHibernate proxy object with some initiliazed fields (other than Id)?

I want to create an object proxy, similar to what ISession.Load is returning, but with some fields initialized. For other properties, when accessed, the proxy will fetch the entire object from database.
Consider the following example:
public class User
{
protected User() {
}
public User(int id, string username, string email) {
// ...
}
// initialize the following fields from other datasources
public virtual int Id { get; set; }
public virtual string UserName { get; set; }
public virtual string Email { get; set; }
// the rest of fields when accessed will trigger a select by id in the database
public virtual string Field1 { get; set; }
public virtual string Field2 { get; set; }
public virtual DateTime Field3 { get; set; }
public virtual ISet<Comment> Comments { get; set; }
}
The Id, UserName, Email are well-known in my case, so I could create an object proxy containing these fields, and for the others leave the default proxy behavior. In addition to throwing an exception if this id is not found in the database, i could throw an exception if preinitialized fields do not match or overwrite them silently. I am using NHibernate.ByteCode.Castle for proxy factories.
Edit:
The purpose for this is to be able to have some projection properties from an entity which can be queried elsewhere (say. a lucene index) and to avoid database calls. Then instead of wrapping these fields in a custom component class containing only these subset of properties, I want to use the proxy object directly so that I am able to load the rest of fields if needed. In the best case scenario I wouldn't hit the database at all, but in some corner cases I'd like to access other fields, too. The SELECT N+1 problem's impact can be greatly reduced by using batching.
An hypothetical version of code I want to use would be:
// create User object proxy with some fields initialized
var user = Session.Load<User>(5, new { UserName = "admin", Email = "admin#company.com" });
Console.WriteLine(user.Id); // doesn't hit the database
Console.WriteLine(user.UserName); // doesn't hit the database
Console.WriteLine(user.FullName); // doesn't hit the database
if (somecondition) {
Console.WriteLine(user.Field1); // fetches all other fields
}
You can specify an eager fetch inside the query to actually retrieve the needed associations. This could be done in different ways depending on what query style ( Criteria,Hql or LINQto NH ) you are using. But the key is changing the fetch mode.
for non-collection properties, I wouldn't do that;
the cost of prefetching them from the DB when you load your entity is (usually) so small that I wouldn't even bother.
for collection properties, just mark the collection fetch strategy as 'lazy=true'.
The only case where I would think about doing something like that is when I have a very large number of properties which I don't need (in your example- say Field1..Field20).
In that case I would either:
1. Define those properties together as a component, or
2. create a DTO for fetching only a subset of your entity's properties.
specifying lazy = "true" (or Not.LazyLoad() for Fluent NHib) on properties Field1, Field2, Field3, Comments mappings may help, though not sure about Select N+1 issue.
another way to go is specifying lazy = "false" for UserName, Email

Is there something analogous on NHibernate regarding Entity Framework's navigation property?

Is there something analogous on NHibernate regarding Entity Framework's navigation property? For example, instead of:
s.Save(new Product { Category = s.Get<Category>("FD"), Name = "Pizza" });
I wish I could write:
s.Save(new Product { CategoryId = "FD", Name = "Pizza" });
Can I inform NHibernate not to use the Product's Category property as a mechanism to save the Product's category? I want to use CategoryId instead(Read: I don't want to use DTO). Entity Framework seems able to facilitate avoiding DTO patterns altogether, while at the same time offering the full benefit of ORM(can avoid joins using navigation properties). I want the EF's offering the best of both worlds(lean mechanism for saving objects, i.e. no need to retrieve the property's object) and navigation mechanism for querying stuff
Sample from EF: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx
public class Category
{
public virtual string CategoryId { get; set; }
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; set; }
}
public class Product
{
public virtual int ProductId { get; set; }
public virtual string Name { get; set; }
public virtual string CategoryId { get; set; }
public virtual Category Category { get; set; }
}
[UPDATE]
Regarding James answer, I tried seeing the NHibernate's actions in SQL Server Profiler.
// this act didn't hit the Category table from the database
var c = s.Load<Category>("FD");
// neither this hit the Category table from the database
var px = new Product { Category = c, Name = "Pizza" };
// this too, neither hit the Category table from the database
s.Save(px);
Only when you actually access the Category object that NHibernate will hit the database
Console.WriteLine("{0} {1}", c.CategoryId, c.Name);
If I understand your question, you want to save a Product with a Category without hitting the database to load the Category object. NHibernate absolutely supports this and you almost have the right code. Here is how you do it in NHibernate:
s.Save(new Product { Category = s.Load<Category>("FD"), Name = "Pizza" });
This will not hit the database to fetch the actual Category, but it will simply save a Product with the correct Category.Id. Note that you don't need (and I would recommend getting rid of Product.CategoryId).
Now why does this work with session.Load(), but not session.Get()... With session.Get(), NHibernate has to return the object or null. In .NET, there is no way for an object to replace itself with null after the fact. So NHibernate is forced to go to the database (or L1 cache) to verify that the "FD" Category actually exists. If it exists, it returns an object. If not, it must return null.
Let's look at session.Load(). If the object is not present in the database, it throws an exception. So NHibernate can return a proxy object from session.Load() and delay actually hitting the database. When you actually access the object, NHibernate will check the database and can throw an exception at that point if the object doesn't exist. In this case, we're saving a Product to the database. All NHibernate needs is the Category's PK, which it has in the proxy. So it doesn't have to query the database for the Category object. NHibernate never actually needs to hydrate an actual Category object to satisfy the save request.

Why do I need to set an alias to my projection if it's optional?

I'm reading the documentation about DetachedCriteria. The documentation clearly shows that setting an alias for your projection is optional. However, whenever I omit the alias my model properties contain no data. Here are my two test models.
[ActiveRecord("INCIDENT")]
public class Incident : ActiveRecordBase<Incident>
{
[PrimaryKey(PrimaryKeyType.Native, "INCIDENT_ID", ColumnType = "Int32")]
public virtual int IncidentId { get; set; }
[Property("CREATION_DATETIME", ColumnType = "DateTime")]
public virtual DateTime? CreationDatetime { get; set; }
[BelongsTo("CAUSE_CD")]
public virtual Cause Cause { get; set; }
}
[ActiveRecord("CAUSE")]
public class Cause : ActiveRecordBase<Cause>
{
[PrimaryKey(PrimaryKeyType.Native, "CAUSE_CD", ColumnType = "String")]
public virtual string CauseCd { get; set; }
[Property("CAUSE_DESC", ColumnType = "String", NotNull = true)]
public virtual string CauseDesc { get; set; }
}
Here is what I use to query the database.
DetachedCriteria incidentCriteria = DetachedCriteria.For<Incident>("i")
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("i.IncidentId"))
.Add(Projections.Property("i.CreationDatetime"))
)
.SetResultTransformer(Transformers.AliasToBean<Incident>());
IList<Incident> incidents = Incident.FindAll(incidentCriteria);
Both projections properties do not get populated unless I set an alias. So my question is, why is the alias optional? I'm sure I'm simply missing something else. But if I put a random alias (e.g. abc) it'll return an error saying that it could not find property "abc" in the Incident class. Fair enough, I add the appropriate alias to match my property names. And voila! My properties are now property being populated.
Now comes the issue of when I want to query a lookup table. I add
.Add(Projections.Property("c.CauseDesc"), "CauseDesc")
to my ProjectionList and append
.CreateCriteria("i.Cause", "c")
But now it complains that it can't find "CauseDesc" from my Incident model.
What am I missing from this whole criteria ordeal?
Update:
The following code
IList<Incident> results = sess.CreateCriteria<Incident>("i")
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("i.IncidentId"), "IncidentId")
.Add(Projections.Property("i.CreationDatetime"), "CreationDatetime")
.Add(Projections.Property("c.CauseDesc"), "CauseDesc")
)
.Add(Expression.Gt("i.IncidentId", 1234567))
.CreateAlias("Cause", "c")
.List<Incident>();
This does create a valid query (I checked it with a profiler) but it seems to be having issues populating my generic list. It gives me error "The value \"System.Object[]\" is not of type \"oms_dal.Models.Incident\" and cannot be used in this generic collection.\r\nParameter name: value". However, all works fine if I don't use a projection but then it selects 50+ fields which I don't want. Does that mean I'm forced to use a DTO in this circumstance?
You need to specify the projection property name like...
.Add(Projections.Property("i.IncidentId"), "IncidentId")
also, in general you do not project into the same domain object. You should create an incident dto like...
public class IncidentDTO
{
public int IncidentID { get; set; }
public DateTime CreationDatetime { get; set; }
}
and then...
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("i.IncidentId"), "IncidentId")
.Add(Projections.Property("i.CreationDatetime"), "CreationDatetime")
)
.SetResultTransformer(Transformers.AliasToBean<IncidentDTO>());
If you want Incidents matching some criteria (not a DTO), then don't set projections/resulttransformer. Instead you simply do something like this...
IList<Incident> incidents = session.CreateCriteria<Incident>()
.CreateAlias("Cause", "c") //now you can access Cause properties via `c.`
.Add(Restrictions.Eq("c.CauseDesc", "some cause"))
.List<Incident>();
See how the root criteria object doesn't need an alias. If it helps, I only use CreateCriteria for the initial object. If I need to reference child objects, I use CreateAlias.