Hello everyone,
I'm working on a .NET C# project on which I had to use NHibernate Mapping Attributes to map my objects to my tables in my database.
Now let's explain what my problem is.
I have two mapped classes, for example ClassA and ClassB. In my database, table A contains a foreign key referencing the primary key of table B. Hence, I have added to ClassA an instance of ClassB mapped in many-to-one:
private ClassB b;
[ManyToOne(0, Name = "B", Column = "ID_TABLE_B_TABLE_A", Class = "ClassB", Update = false, Insert = false)]
public virtual ClassB B
{
get { return b; }
set { b= value; }
}
Now, I want to check the value of a field of ClassB when I'm accessing ClassA. I write the query in HQL:
Session.CreateQuery("select a.Id from ClassA a where a.ClassB.Name = 'xxx' ");
Here's the generated SQL:
select tablea0_.ID_TABLE_A as col_0_0_
from TABLE_A tablea0_, TABLE_B tableb1_
where tablea0_.ID_TABLE_B_TABLE_A = tableb1_.ID_TABLE_B
and tableb1_.NAME_TABLE_B='xxx'
I thought this kind of HQL query was supposed to generated a join statement rather than a where statement, as I have defined a many-to-one association between the two classes. Something like this would've been better:
select tablea0_.ID_TABLE_A as col_0_0_
from TABLE_A tablea0_
left join TABLE_B tableb1_ on tableb1_.ID_TABLE_B = tablea0_.ID_TABLE_B_TABLE_A
where tableb1_.NAME_TABLE_B='xxx'
In my opinion, join looks cleaner to where. I would like to know if there is a way to set up the behaviour of NHibernate accordingly, without specifying the join statement explicitly in the HQL query.
Any help would be appreciated !