2013-07-12 20 views
0

我試圖用Hibernate/JPA使用RequestFactory調用數據庫,並且我想要檢索返回嵌入實體的實體列表。我知道.with()方法適用於像.find()這樣的方法,但它似乎不適用於自定義查詢。你可以在命名查詢中使用RequestFactory的.with()方法嗎?

我做它目前的方法如下:

我在實體類中使用一個命名查詢的查詢。 (主要實體是名稱,嵌入式實體被稱爲nameSuffix後綴實體)

@NamedQueries({ @NamedQuery(name = "Query.name", query = "select * from NameTable") }) 

然後在服務類中,.LIST()方法,它是想我與RequestFactory打電話,如下所示。

public List<Name> list() { 
    return emp.get().createNamedQuery("Query.name").getResultList(); 
} 

最後,這是我打這個電話在我的客戶端代碼:

NameRequest context = requestFactory.createNameRequest(); 
context.list().with("nameSuffix").fire(new Receiver<List<NameProxy>>(){ 
    public void onSuccess(List<NameProxy> response) { 
     String suff = response.get(0).getNameSuffix().getText(); 

    } 
}); 

在上面的代碼,它說,getNameSuffix()返回null,這將意味着.with("nameSuffix")不與標準.find()方法一樣使用.list()調用。

有沒有辦法建立一個呼叫,使用.with()返回實體及其嵌入實體的列表,還是我需要以另一種方式做?如果我需要以另一種方式去做,有沒有人想出了一個很好的方法呢?

回答

0

我想你誤解了方法with()的想法,除非你有一個方法getNameSuffix它返回NameSuffix實體。這就是documentation說一下吧:

When querying the server, RequestFactory does not automatically populate relations in the object graph. To do this, use the with() method on a request and specify the related property name as a String

所以,你必須傳遞給方法是什麼要檢索子實體的名稱的列表。我希望這個例子可能會有所幫助:

class A { 
    String getS(){return "s-a"} 
    B getB(){return new B();} 
} 
class B { 
    String getS(){return "s-b";} 
    C getC(){return new C();} 
} 
class C { 
    String getS(){return "s-c";} 
} 

context.getA().fire(new Receiver<A>(){ 
    public void onSuccess(A response) { 
    // return 's-a' 
    response.getS(); 
    // trhows a NPE 
    response.getB().getS(); 
    } 
}); 
context.getA().with("b").fire(new Receiver<A>(){ 
    public void onSuccess(A response) { 
    // return 's-a' 
    response.getS(); 
    // return 's-b' 
    response.getB().getS(); 
    // trhows a NPE 
    response.getB().getC().getS(); 
    } 
}); 
context.getA().with("b.c").fire(new Receiver<A>(){ 
    public void onSuccess(A response) { 
    // return 's-a' 
    response.getS(); 
    // return 's-b' 
    response.getB().getS(); 
    // return 's-c' 
    response.getB().getC().getS(); 
    } 
}); 
+0

感謝您的回覆,但不,我明白我發送它的名字。我比較瞭解.with(),並且可以在.find()方法中使用它。我希望有一些幫助能夠在使用NamedQueries的調用中使用.with(),這可能是不可能的。 – GrandpaJake

+0

Gotcha。爲什麼不將您的查詢作爲遠程方法的參數發送? 'rq.list(「namedQuery」)' –

+0

我不確定那會有什麼幫助。命名的查詢被調用的很好,但我沒有將嵌入式實體返回到返回的實體中。 – GrandpaJake

相關問題