NHibernate ICriteria and composite-id with key-many-to-one
Suppose you have a legacy database, and an entity called ViewOfBaseData that have this id.
|
|
Now suppose you need to retrieve all object ViewOfBaseData linked to a LinkResult object with a property KeyList equal to a certain value. In HQL is really simple
|
|
Resulting SQL query is a simple join between the two tables, but now suppose you cannot use HQL and instead you are forced to use ICriteria, you can try this one.
|
|
But this failed with the error: * no such column: linkresult2_.keyList *, if you look at the SQL query issued to the database, you can verify that the query does not contain the join with the table for LinkResult entity. If you change the constant blabla with the integer 10 you will obtain the error * Type mismatch in NHibernate.Criterion.SimpleExpression: KeyList expected type System.String, actual type System.Int32 *thus confirming me that the path is specified correctly.
If you examine the inner metadata of the session you can find that the composite id ViewOfBaseDataId is a ComponentType and probably internally, when the engine that build sql query verify that Id property of ViewOfBaseData is a componentType determines that each column is contained in the base table. I do not know if it is a bug of nhibernate or if there is another way to specify the criteria, but if you absolutely need not to use HQL you can use DetatchedCriteria
|
|
This generates the following query
|
|
From the point of view of performance, probably this query is worst than a direct join, but it works ;)
alk.
Tags: NHibernate ICriteria