HQL查询集中的列

时间:2020-03-05 18:56:43  来源:igfitidea点击:

是否可以使用HQL这样的配置访问table2的各个列?

<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>

解决方案

回答

它们只是table1的table2属性的属性。

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

我们可能需要加入,像这样

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

这是休眠文档的相关部分。

回答

我们可以查询它们,但不能使其成为where子句的一部分。例如。,

select t1.table2.x from table1 as t1

会工作,但是

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

不会。

回答

假设table2有一列" color varchar(128)",并且此列已正确映射到Hibernate。

我们应该能够执行以下操作:

from table1 where table2.color = 'red'

这将返回链接到其"颜色"列为"红色"的" table2"行的所有" table1"行。注意,在Hibernate映射中,set具有与其引用的表相同的名称。上面的查询使用集合的名称,而不是表的名称。