【问题标题】:Hibernate query internal listHibernate查询内部列表
【发布时间】:2012-03-22 21:11:52
【问题描述】:

我有以下 Hibernate 实体:

@Entity
class A{
    String name;
}

@Entity
class B{
   List<A> aList;
}

我想从数据库中获取所有B列表中的ALL名称类似于B的列表,例如,对于以下列表:

String lst[] = {"str1", "str2"};

我需要检索所有 B 条目,其中包含两个 A 的列表,其中的字符串与数组中的字符串相同。

我更喜欢使用 Criteria 查询,但 HQL 或 SQl 也可以。

【问题讨论】:

    标签: sql hibernate criteria


    【解决方案1】:

    如果您的 A 和 B 实体分别保存在 A_table 和 B_table 中,并且从 A 到 B 的一对多关系保存在 A_table 的 b_id 列中,则相应的 SQL 将类似于:

    select distinct * from B_table join A_table on A_table.b_id=B_table .id where A_table.name="str1" or A_table.name="str2"
    

    HQL 查询类似于:

    select distinct b from B b inner join B.aList as a where a.name='str1' or a.nama='str2'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2023-03-28
      • 2018-12-08
      相关资源
      最近更新 更多