gpt4 book ai didi

java - 在 WHERE 子句中带有集合的 HQL

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:11:34 25 4
gpt4 key购买 nike

我一直在努力寻找一个正式让我做噩梦的查询。该系统是一个用户和联系人管理。所以我有 UserAccountContactPhone

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>();

Contact 现在与电话有一对多的单向

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

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

我知道我可以覆盖 equalshashcode 但是因为 phone 在一个由 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;
}

我一直有这个错误:

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 中的集合。感谢阅读

最佳答案

HQL 查询可能是这样的:

select cfrom Contact cjoin c.phones cphoneswhere 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();

关于java - 在 WHERE 子句中带有集合的 HQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1759610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com