I need to restrict a one-to-many collection by a date column with a value specified on the parent element. The 'where' attribute on set or bag looks perfect for this.
NHibernate documentation describes the 'where' attribute as being for arbitrary SQL, so am I correct in assuming that I cannot use values from the parent class here as I would in HQL and must implement my own IUserCollection instead?
IMHO using a filter would be better because you could write the restriction in database agnostic HQL.
Related
Is it possible to somehow restrict a one-to-one mapping with a constraint other than property ref? Ideally, something along the lines of the class definition, for example, where you can add where="Condition=Value".
Thanks
You can use nhibernate filters for this: http://nhibernate.info/blogs/nhibernate/archive/2009/05/04/nhibernate-filters.aspx
I've got two objects a parent and a child list. In my fluent nhibernate mapping for the parent I want to load the list of the children.
However I want this to be conditional, a column in the child table is called "IsDeleted" and I only want to return the children where "IsDeleted" is false.
Is it possible to set up a mapping to do this? If not is it possible to do it in just standard nhibernate?
Thanks
Yes, you can use a Where constraint in Fluent NHibernate to map this. Somehting like:
HasMany(x => x.Children).Where("IsDeleted = 0");
The Where constraint should use SQL syntax not HQL. For tables that allow soft deletes it's probably easier to map a view that filters the deleted records out.
i have a complex entity, very very heavy.
i want to select only the id and the name of this entity from the db for a better performance.
how do i do that with nhibernate (or fluent nhibernate).
There are a few different possibilities.
Create a New Entity
One possible solution is to create a new entity and map it to the same table, but only map the columns that you want (id and name). This is quite flexible and lets you use that entity as any other entity. The problem is that you introduce some duplication.
Using HQL
Another solution is to use projections. With HQL you can use the select clause to specify which columns to retrieve. If you want a proper entity instance as the result from the query and not an array of objects, you can create a custom constructor for your class and use that in the HQL query.
session.CreateQuery("select new Foo(f.Id, f.Name) from Foo f").List<Foo>();
Using the Criteria API
If you want to use the Criteria API instead of HQL, you can use the SetProjection method. If you want a proper entity from the query and not an array of objects, you can use the AliasToBean result transformer.
session.CreateCriteria(typeof(Foo))
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Name"), "Name")
.Add(Projections.Property("Id"), "Id"))
.SetResultTransformer(Transformers.AliasToBean(typeof(Foo)))
.List();
The criteria example code is borrowed from the following question, which might be of interest:
NHibernate - Only retrieve specific columns when using Critera queries?
You have 2 choices :
when using ICriteria - use (syntax might not be correct but you get the picture)
SetProjections(Projections.ProjectionList.Add(Projections.Property("prop"))
.Add(Projections.Property("prop1")))
when using hql
select c.Col1,c.Col2 from table c
Both of them will return a list of array lists - or something like that - you then have to traverse it and build your .. dictionary (or whatever you have).
I have a table that needs relations to 2 tables, according to ObjectType column.
For example if ObjectType=1 then column Object should point to TABLE1, and if ObjectType=2 then point to TABLE2.
Can I accomplish this in NHibernate mappings or as Fluent NHibernate?
If not will you suggest me using same Interfaces for both Table classes? (Note: table schemas are totally different)
Why not reference both tables, and use one or the other according to your needs in the class code?
Use a property that returns a common interface for both tables and gives one table or the other according to the object type.
In my domain model I have an abstract class CommunicationChannelSpecification, which has child classes like FTPChannelSpecification, EMailChannelSpecification and WebserviceChannelSpecification. Now I want to create an HQL query which contains a where clause that narrows down the result to certain types of channel specifications. E.g. (in plain English) select all CommunicationChannelSpecifications that whose types occur in the set {FTPChannelSpecification, WebserviceChannelSpecification}.
How can this be achieved in HQL? I'm using NHibernate 2.0.1 and a table per subclass inheritance mapping strategy...
Thanks!
Pascal
Not positive in NHibernate, but in Hibernate, there are two special properties that are always referenced id and class. So, for your particular case, I'd do
from CommunicationChannelSpecifications spec where spec.class in (?)
NHibernate supports the same syntax as Hibernate in this case. See here for an example.