Java 在 WHERE 子句中带有集合的 HQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1759610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:22:47  来源:igfitidea点击:

HQL with a collection in the WHERE clause

javahibernatejakarta-eehql

提问by black sensei

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, Contactand Phone.

我一直在尝试查询谁正式给我做噩梦。该系统是一个用户和联系人管理。所以我有UserAccountContactPhone

UserAccounthas a bidirectional one-to-many relationship with Contactand an unidirectional one on phone all mapped by a Set:

UserAccount与电话上的双向一对多关系Contact和单向关系都由 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

Contact 现在有一对多单向与手机

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

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

I know I could have overridden the equalsand hashcodefor 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

我知道我可以为此覆盖equalshashcode,但是由于电话在由 set 映射的实体中,我现在不知道该怎么做。所以我想提供一种方法,而不是在联系页面上的每个条目之前检查我的唯一性

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

I keep on having this error:

我一直有这个错误:

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

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

我真的不知道会发生什么,首先我不知道如何处理 HSQ 中的集合。感谢阅读

采纳答案by Juha Syrj?l?

HQL query would be probably something along these lines:

HQL 查询可能是这样的:

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

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

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

 return !result.isEmpty();