tags:

views:

190

answers:

1

Is it possible to access the relationship table when doing HQL statement?

As an example, I have 3 tables: account, commitment, account_commitment. It was generated using these domains:

class Account {
   static hasMany = [ commits : Commitment ]

   String name
} 

class Commitment {
   static hasMany = [ actors : Account ]

   String description
}

My final and actual SQL query is something like this:

SELECT 
    b.id,
    account_name,
    d.nid,
    d.title
FROM
    account_commitment a, // is this available in HQL?
    account b,
    commitment c,
    content_type_act d
where
    d.nid = 3332
    and a.account_id = b.id
    and a.act_id = c.id
    and c.act_id = d.nid

I believe HQL only allows valid class domains. Since the relationship table is automatically generated, is this possible in HQL?

Thanks.

+1  A: 

No, HQL only works with mapped classes. If you want to run SQL queries just use groovy.sql.Sql. But if you only want to access the intermediate table to join the other two, that's unnecessary since HQL knows about the relationships between tables already.

Burt Beckwith
yes, it seems so. i just changed the architecture and was able to have a much better query. thanks for the reply.
firnnauriel