在WHERE子句中具有集合的HQL [英] HQL with a collection in the WHERE clause

查看:99
本文介绍了在WHERE子句中具有集合的HQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为整个查询进行尝试,这些查询正式使我成为噩梦.该系统是用户和联系人管理.所以我有UserAccountContactPhone.

I've been trying for the this whole a query who is officially giving me nightmares. The system is a user and contact management. So I have UserAccount, Contact and Phone.

UserAccountContact具有双向一对多关系,并且在电话上具有由Set映射的单向关系:

UserAccount has a bidirectional one-to-many relationship with Contact and an unidirectional one on phone all mapped by a 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>();

联系人现在可以与手机进行一对多的单向操作

Contact now has a one-to-many unidirectional with phones

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

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

I'm writing a method to check the existence of the same number for the same contact of a particular user by the email unique field.

我知道我本可以覆盖equalshashcode,但是由于电话是按set映射的实体中的,所以我现在不知道该怎么做.因此,我想提供一种方法,在联系页面上的每个条目

I know I could have overridden the equals and hashcode for that but since phone in a entity mapped by set I don't know at this moment how to do that. So I wanted to provide a method to rather check for that uniqueness for me before each entry on the contact page

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;
}

我继续遇到此错误:

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]. 

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

I can't really figure out what happens and first i don't know how to treat collections in HSQ.thanks for reading

推荐答案

HQL查询可能符合以下要求:

HQL query would be probably something along these lines:


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

此外,您可能希望处理这样的查询结果. list()方法始终返回一个列表,从不返回空值.

Also you may want to handle results of query like this. The list() method returns always a list, never a null.

 return !result.isEmpty();

这篇关于在WHERE子句中具有集合的HQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆