【问题标题】:HQL querying columns in a setHQL 查询集合中的列
【发布时间】:2010-09-09 16:02:37
【问题描述】:

是否可以通过这样的配置使用 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>

【问题讨论】:

    标签: java hibernate hql


    【解决方案1】:

    它们只是 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
    

    这是hibernate doc的相关部分。

    【讨论】:

      【解决方案2】:

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

      select t1.table2.x from table1 as t1
      

      可以,但是

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

      不会。

      【讨论】:

        【解决方案3】:

        假设 table2 有一个列“color varchar(128)”,并且该列正确映射到 Hibernate。

        你应该可以做这样的事情:

        from table1 where table2.color = 'red'
        

        这将返回链接到table2 行的所有table1 行,其color 列是“红色”。请注意,在您的 Hibernate 映射中,您的 set 与其引用的表具有相同的名称。上面的查询使用了set的名字,不是表的名字。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-15
          • 1970-01-01
          • 1970-01-01
          • 2011-05-18
          相关资源
          最近更新 更多