私は正式に私に悪夢を与えているこの全体のクエリを試みてきました。システムは、ユーザーと連絡先の管理です。だから私はUserAccount
、Contact
とPhone
です。WHERE句のコレクションを持つHQL
UserAccount
はContact
との双方向の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>();
私は電子メール固有のフィールドで特定のユーザーの同じ連絡先に同じ番号の存在を確認する方法を書いています。
equals
とhashcode
をオーバーライドできましたが、セットでマップされたエンティティの電話は、この時点でどのように行うのかわかりません。
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は、これらの線に沿って、おそらくものになるだろう
または '返しresult.isEmpty();!' ;)framer8 @ –
、固定 –