2009-11-18 31 views
9

Tôi đã cố gắng cho toàn bộ câu hỏi này, người đã chính thức cho tôi những cơn ác mộng. Hệ thống này là quản lý người dùng và liên hệ. Vì vậy, tôi có UserAccount, ContactPhone.HQL với một bộ sưu tập trong mệnh đề WHERE

UserAccount có một-nhiều mối quan hệ hai chiều với Contact và là một một chiều trên điện thoại tất cả các ánh xạ bởi một 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>(); 

Liên bây giờ có một one-to-many một chiều với điện thoại

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

Tôi đang viết một phương pháp để kiểm tra sự tồn tại của cùng một số cho cùng một liên hệ của một người dùng cụ thể theo trường duy nhất của email.

Tôi biết tôi có thể đã ghi đè số equalshashcode cho điều đó nhưng vì điện thoại trong thực thể được ánh xạ theo bộ tôi không biết tại thời điểm này cách thực hiện điều đó. Vì vậy, tôi muốn cung cấp một phương pháp để thay kiểm tra tính độc đáo mà đối với tôi trước mỗi mục trên trang liên hệ

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

tôi tiếp tục có lỗi này:

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

tôi không thể thực sự tìm ra những gì xảy ra và lần đầu tiên tôi không biết làm thế nào để điều trị các bộ sưu tập trong HSQ.thanks cho việc đọc

Trả lời

16

HQL truy vấn sẽ là một cái gì đó có lẽ cùng những dòng này:

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

Ngoài ra, bạn có thể muốn xử lý kết quả truy vấn như thế này. Danh sách () luôn trả về một danh sách, không bao giờ là rỗng.

return !result.isEmpty(); 
+0

hoặc 'trở result.isEmpty();! ' ;) –

+0

@ framer8, cố định –

Các vấn đề liên quan