带有WHERE子句中的集合的HQL

bla*_*sei 9 java hibernate hql java-ee

我一直在尝试这个正式给我做恶梦的查询.该系统是用户和联系人管理.所以我有UserAccount,Contact而且Phone.

UserAccountContact手机上的单向一对多关系以及手机上的单向关系全部映射为Set:

//UserAccount mapping 
@OneToMany(targetEntity=PhoneImpl.class, cascade= {CascadeType.ALL})
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Phone> phones = new HashSet<Phone>();

@OneToMany(targetEntity=ContactImpl.class, cascade={CascadeType.ALL}, mappedBy="userAccount")
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Contact> contacts = new HashSet<Contact>();
Run Code Online (Sandbox Code Playgroud)

现在联系人有一对多的单向电话

@OneToMany(targetEntity=PhoneImpl.class, cascade={CascadeType.ALL})
private Set<Phone> phones = new HashSet<Phone>();
Run Code Online (Sandbox Code Playgroud)

我正在编写一种方法,通过电子邮件唯一字段检查特定用户的相同联系人是否存在相同的号码.

我知道我可以覆盖equals并且hashcode为此而且因为在集合映射的实体中的电话我现在不知道如何做到这一点.所以我想提供一种方法,在联系页面上的每个条目之前为我检查该唯一性

public boolean checkForExistingPhone(String userEmail, String formatedNumber) {
    List<Contact> result = null;
    Session sess = getDBSession().getSession();

    String query = "select Contact ,cphones.formatedNumber from Contact c inner join    Contact.phones cphones where c.UserAccount.email = :email and cphones.formatedNumber= :number";
//        try {
        result = (List<Contact>) sess.createQuery(query)
                .setParameter("email", userEmail)
                .setParameter("number", formatedNumber).list();
//        } catch (HibernateException hibernateException) {
//            logger.error("Error while fetching contacts of email " + userEmail + " Details:"     + hibernateException.getMessage());
//        }
        if(result == null)
            return false;
        else
            return true;
}
Run Code Online (Sandbox Code Playgroud)

我一直有这个错误:

org.hibernate.hql.ast.QuerySyntaxException: Contact is not mapped [select
cphones.formatedNumber from Contact c inner join Contact.phones cphones where
c.UserAccount.email = :email and cphones.formatedNumber= :number]. 
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚会发生什么,首先我不知道如何处理HSQ.thanks中的集合进行阅读

Juh*_*älä 16

HQL查询可能是这样的:

select c
from Contact c
join c.phones cphones
where c.userAccount.email = :email
  and cphones.formatedNumber = :number

您也可以像这样处理查询结果.该列表()方法总是返回一个列表,从来没有空.

 return !result.isEmpty();
Run Code Online (Sandbox Code Playgroud)