2011-04-05 84 views
0

時,使用標準API時,我總是得到相同的錯誤無效鍵錯誤:JPA:使用標準API

Local Exception Stack: 
Exception [EclipseLink-6075] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.QueryException 
Exception Description: Object comparisons can only use the equal() or notEqual() operators. Other comparisons must be done through query keys or direct attribute level comparisons. 
Expression: [ 
Relation operator LIKE 
    Query Key rootId 
     Base de.uni.entities.Diary 
    Constant Testaccount] 
Query: ReportQuery(referenceClass=Diary) 

與標準API的代碼如下:

 .... 
    CriteriaBuilder cb = getEm().getCriteriaBuilder(); 
    CriteriaQuery<User> cq = cb.createQuery(User.class); 

    // Restrictions 
    Predicate[] predicate = new Predicate[1]; 

    // From-clause 
    Root<User> root2R = cq.from(User.class); 
    predicate[0] = cb.like(root2R.<String> get("rootId"), id); 

    Join<Clazz, Diary> friends2R; 
    friends2R = root2R.join("diaries"); 

    cq.where(predicate); 

    // Select-clause 
    cq.select((Selection<? extends Clazz>) friends2R); 

    Query query = getEm().createQuery(cq); 

    List<Object> data = query.getResultList(); 

    return data; 

} 

由於以下兩行存在問題: 「加入好友2R; friends2R = root2R.join(」diaries「);」

如果沒有映射(使用調整後的select-clause),我會使用擬合rootId得到搜索的用戶,這樣就不會出現錯誤。但是現在我想將用戶映射到日誌並顯示用戶實體的所有日記。但它總是以前面的錯誤結束。無論我使用「喜歡」還是「平等」,它都不起作用。

generell代碼不應該有錯誤,因爲我已經使用相同的代碼來獲取所有映射的用戶用戶(多對多關係)=>沒有問題。

我只是不明白,爲什麼會出現這個錯誤,特別是爲什麼它提到日記爲基礎,雖然用戶應該被提及爲基類......並且該ID也是正確的,並且也出現在數據庫...

我真的希望你能幫助我,在此先感謝!

回答

1

rootId是如何映射的,使用Basic還是OneToOne? 似乎是一個錯誤,請在EclipseLink上記錄這個錯誤。

您的代碼似乎並不正確雖然,

CriteriaQuery中CQ = cb.createQuery(User.class);

應,

CriteriaQuery中CQ = cb.createQuery(Diary.class);

不應該嗎?

不日記也與用戶有關係?您可以只查詢其用戶擁有該ID的日記。