views:

1822

answers:

3

Is it possible to reach the individual columns of table2 using HQL with a configuration like this?

<hibernate-mapping>
  <class table="table1">
    <set name="table2" table="table2" lazy="true" cascade="all">
      <key column="result_id"/>
      <many-to-many column="group_id"/>
    </set>
  </class>
</hibernate-mapping>
+1  A: 

They're just properties of table1's table2 property.

select t1.table2.property1, t1.table2.property2, ... from table1 as t1

You might have to join, like so

select t2.property1, t2.property2, ... 
    from table1 as t1
    inner join t1.table2 as t2

Here's the relevant part of the hibernate doc.

sblundy
+1  A: 

You can query on them, but you can't make it part of the where clause. E.g.,

select t1.table2.x from table1 as t1

would work, but

select t1 from table1 as t1 where t1.table2.x = foo

would not.

Mike Desjardins
A: 

Let's say table2 has a column "color varchar(128)" and this column is properly mapped to Hibernate.

You should be able to do something like this:

from table1 where table2.color = 'red'

This will return all table1 rows that are linked to a table2 row whose color column is 'red'. Note that in your Hibernate mapping, your set has the same name as the table it references. The above query uses the name of the set, not the name of the table.

Michael Angstadt