2009-11-18 17 views
9

Ho cercato per tutta la query un'interrogazione che mi dà ufficialmente degli incubi. Il sistema è un utente e gestione dei contatti. Quindi ho UserAccount, Contact e Phone.HQL con una raccolta nella clausola WHERE

UserAccount ha un rapporto bidirezionale uno-a-molti con Contact e uno unidirezionale sul telefono tutto mappato da un 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>(); 

Contatto ora ha uno-a-molti unidirezionale con i telefoni

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

Sto scrivendo un metodo per verificare l'esistenza dello stesso numero per lo stesso contatto di un particolare utente dal campo unico di posta elettronica.

So che avrei potuto ignorare il equals e hashcode per quello ma dal momento che il telefono in un'entità mappata dal set non so in questo momento come farlo. Così ho voluto fornire un metodo per controllare piuttosto per quella unicità per me prima di ogni entrata nella pagina di contatto

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

io continuo a avere questo errore:

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

non posso davvero capire cosa succede e prima non so come trattare le collezioni in HSQ.thanks per la lettura

interrogazione

risposta

16

HQL sarebbe probabilmente qualcosa in queste righe:

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

Inoltre, potresti voler gestire i risultati di query come questa. Il metodo () restituisce sempre un elenco, mai un null.

return !result.isEmpty(); 
+0

o 'tornare result.isEmpty();!' ;) @ –

+0

framer8, fisso –