NHibernate is generally rather smart and omits joins on many-to-one if fields from related entity are not used in query. However, I want to do a formula discriminator on other table without subselect, so I did a custom persister with
protected override string DiscriminatorFormulaTemplate
{
get { return this.DiscriminatorFormula; }
}
and specified just a column name in formula (but this column name is from a related table)
<discriminator formula="TypeID" />
I understand this is generally prone to name conflicts, however in this specific case this is not a problem.
So now I want to force the related (many-to-o开发者_Python百科ne
) table to be always joined to the main one. What is the best way to do this? It is joined some times, when HN detects that some fields are used, however I do not know how to force it to do it all the time.
<many-to-one ... fetch="join"/>
This covers Get, Criteria and lazy load, but not HQL.
Another option is using custom SQL for all operations, and select from a subquery instead of a table (with the join inside that query)
Unfortunately, anything you do at this point will be a hack. The way NH is designed, the discriminator must be in the same table.
精彩评论