简体   繁体   English

HQL 查询集合中的列

[英]HQL querying columns in a set

Is it possible to reach the individual columns of table2 using HQL with a configuration like this?是否可以通过这样的配置使用 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>

They're just properties of table1's table2 property.它们只是 table1 的 table2 属性的属性。

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 .这是hibernate doc的相关部分。

You can query on them, but you can't make it part of the where clause.您可以查询它们,但不能将其作为 where 子句的一部分。 Eg,例如,

select t1.table2.x from table1 as t1

would work, but会工作,但是

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

would not.不会。

Let's say table2 has a column " color varchar(128) " and this column is properly mapped to Hibernate.假设 table2 有一列“ color varchar(128) ”,并且该列已正确映射到 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'.这将返回链接到color table2 “红色”的table2行的所有table1行。 Note that in your Hibernate mapping, your set has the same name as the table it references.请注意,在您的 Hibernate 映射中,您的set与它引用的表具有相同的名称。 The above query uses the name of the set , not the name of the table.上面的查询使用集合的名称,而不是表的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM