【问题标题】:query on Set<> with HQL使用 HQL 查询 Set<>
【发布时间】:2014-01-26 16:28:19
【问题描述】:

我有这样的映射类:

@Entity
@Table(name = "contact", catalog = "pagesjaunes")
public class Contact implements java.io.Serializable {

    private Integer id;
    private String name;
    private Set<Phone> phones = new HashSet<Phone>(0);

    [...]

}



@Entity
@Table(name = "telephone", catalog = "pagesjaunes")
public class Phone implements java.io.Serializable {

    private Integer id;
    private Contact contact;
    private String phoneNumber;

    [...]
}

我可以在Contact 上用HQL 进行类似的查询吗:

from Contact where phones.phoneNumber = 06487954

【问题讨论】:

    标签: hibernate jpa


    【解决方案1】:

    不,语法不正确。 phones 是一个 Set 的路径,而 Set 没有 phoneNumber 属性。你需要加入才能做你想做的事:

    select c from Contact c
    inner join c.phones [as] phone
    where phone.phineNumber = '06487954'
    

    这当然在the documentation中有解释。

    注意:[as] 表示可以使用as,也可以省略。

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      相关资源
      最近更新 更多