doglin
doglin

Reputation: 1741

NHibernate.QueryException: not an association: Id

I wrote a joint query using NHiberNate, but I am getting a NHibernate.QueryException:not an association: Id

This is what it looks like with NHibernate library

TicketRequest ticketAlias = null;
        Show showAlias = null;

        IList<TicketRequest> results = UnitOfWork.CurrentSession.QueryOver<TicketRequest>(() => ticketAlias)
                  .JoinAlias(() => ticketAlias.ShowId, () => showAlias.Id)
                  .Where(() => showAlias.ShowDate >=DateTime.Now)
                  .List();

        return results;

I just want a simple joint statement, and this is what it would have been in SQL

select * from TicketRequest as a join Show as b
on a.Show_id = b.Id
where ShowDate >=GETDATE()

Can someone help and let me know why I am getting a "not an association:Id" error. I have id in the "Show" table, it is a primary key.

Please advise. All helps are greatly appreciated.

Upvotes: 4

Views: 4725

Answers (2)

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

You don't have to specify the foreign keys / primary keys when querying with NHibernate. It's an ORM. You write object oriented queries. The keys and relations are specified in the mapping file.

A Join in an NHibernate query is simply specified by the property name with which you navigate to the other property.

That's what the error message means. Id is not an association.

Upvotes: 0

cremor
cremor

Reputation: 6865

You need to specify a many-to-one relation in joins. In your case that is the Show property.

IList<TicketRequest> results = UnitOfWork.CurrentSession.QueryOver<TicketRequest>(() => ticketAlias)
    .JoinAlias(() => ticketAlias.Show, () => showAlias)
    .Where(() => showAlias.ShowDate >= DateTime.Now)
    .List();

PS: You shouldn't map both a many-to-one relation (Show) and an foreign key property (ShowID). Usually you only work with object relations when using an ORM. Only map the plain ID if you really need it for something, but even then only map it as read-only.

Upvotes: 1

Related Questions