2009-11-18 6 views
9

私は正式に私に悪夢を与えているこの全体のクエリを試みてきました。システムは、ユーザーと連絡先の管理です。だから私はUserAccountContactPhoneです。WHERE句のコレクションを持つHQL

UserAccountContactとの双方向の1対多の関係とSetによってマッピングされた携帯電話上の一方向の1のすべてがあります。今

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

連絡先を電話との1対多の単方向を持っている

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

私は電子メール固有のフィールドで特定のユーザーの同じ連絡先に同じ番号の存在を確認する方法を書いています。

equalshashcodeをオーバーライドできましたが、セットでマップされたエンティティの電話は、この時点でどのように行うのかわかりません。

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

私は本当に理解することはできません。だから、私はこのエラーを持つに保つコンタクトページ上の各エントリの前に

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は、これらの線に沿って、おそらくものになるだろう

答えて

16

HQLクエリを読み取るためHSQ.thanksでコレクションを治療するための方法がわからない:

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

また、このようなクエリの結果を処理することもできます。 list()メソッドは常にリストを返し、決してnullを返しません。

return !result.isEmpty(); 
+0

または '返しresult.isEmpty();!' ;)framer8 @ –

+0

、固定 –