Hibernate version: 1.0.1
I have two entities, which I'll call Foo and Bar, which are related in a many-to-many fashion. The "join" table is exposed as an entity in itself, which I'll call FooBar.
Mapping documents:
Code:
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.0"
default-cascade="none"
default-access="field.camelcase-underscore"
assembly="Entities"
namespace="MyCompany.Entities">
<class name="Foo" table="foo" lazy="true" dynamic-update="true" select-before-update="true">
<id name="Id" column="foo_id" type="String" length="10" unsaved-value="null" access="field.camelcase-underscore">
<generator class="MyCompany.Entities.DataAccess.NHibernate.MyIdentifierGenerator, EntityAccess"/>
</id>
<property name="Name" column="name" type="String" length="50" access="field.camelcase-underscore"/>
<bag name="Bars" inverse="true" lazy="true" cascade="delete" access="field.camelcase-underscore">
<key column="foo_id" />
<one-to-many class="FooBar"/>
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.0"
default-cascade="none"
default-access="field.camelcase-underscore"
assembly="Entities"
namespace="MyCompany.Entities">
<class name="FooBar" table="foo_bar" lazy="true" dynamic-update="true" select-before-update="true">
<id name="Id" column="foo_bar_id" type="String" length="10" unsaved-value="null" access="field.camelcase-underscore">
<generator class="MyCompany.Entities.DataAccess.NHibernate.MyIdentifierGenerator, EntityAccess"/>
</id>
<property name="IsPrimary" column="is_primary" type="Boolean" access="field.camelcase-underscore"/>
<many-to-one name="Foo" class="Foo" cascade="none" column="foo_id" access="field.camelcase-underscore"/>
<many-to-one name="Bar" class="Bar" cascade="delete" column="bar_id" access="field.camelcase-underscore"/>
</class>
</hibernate-mapping>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.0"
default-cascade="none"
default-access="field.camelcase-underscore"
assembly="Entities"
namespace="MyCompany.Entities"
>
<class name="Bar" table="bar" lazy="true" dynamic-update="true" select-before-update="true">
<id name="Id" column="bar_id" type="String" length="10" unsaved-value="null" access="field.camelcase-underscore">
<generator class="MyCompany.Entities.DataAccess.NHibernate.MyIdentifierGenerator, EntityAccess"/>
</id>
<property name="Name" column="name" type="String" length="50" access="field.camelcase-underscore"/>
<bag name="Foos" inverse="true" lazy="true" cascade="delete" access="field.camelcase-underscore">
<key column="bar_id" />
<one-to-many class="FooBar"/>
</bag>
</class>
</hibernate-mapping>
If I execute this query:
Code:
SELECT Foo, FooBar, PrimaryBar
FROM Foo AS Foo
LEFT JOIN Foo.Bars AS FooBar
JOIN FooBar.Bar AS PrimaryBar
WHERE FooBar.IsPrimary = true
I get this exception:
Code:
QueryException: undefined alias or unknown mapping: MyCompany [SELECT Foo, MyCompany.Entities.FooBar, PrimaryBar
FROM MyCompany.Entities.Foo AS Foo
LEFT JOIN Foo.Bars AS FooBar
JOIN FooBar.Bar AS PrimaryBar
WHERE FooBar.IsPrimary = true]
at NHibernate.Hql.PathExpressionParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ParserHelper.Parse(IParser p, String text, String seperators, QueryTranslator q)
at NHibernate.Hql.SelectParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ClauseParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ClauseParser.End(QueryTranslator q)
at NHibernate.Hql.PreprocessingParser.End(QueryTranslator q)
at NHibernate.Hql.ParserHelper.Parse(IParser p, String text, String seperators, QueryTranslator q)
at NHibernate.Hql.QueryTranslator.Compile()
at NHibernate.Hql.QueryTranslator.Compile(ISessionFactoryImplementor factory, IDictionary replacements, Boolean scalar)
at NHibernate.Impl.SessionFactoryImpl.GetQuery(String queryString, Boolean shallow)
at NHibernate.Impl.SessionImpl.GetQueries(String query, Boolean scalar)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
at NHibernate.Impl.QueryImpl.List()
If I don't specify the select list, or alias the entities differently than their class names, it works fine. This might not sound like a big deal, but we need specify the select list explicitly and want to use entity aliases that match the class names where possible, for other reasons ...