2011-05-28 19 views
1

我無法獲取位於另一個對象內的對象中的字段。我可以得到一些領域,但其他人沒有。無法從Siena的另一個對象中的對象獲取字符串字段

這是我爲重現此錯誤而創建的測試。

public void commentTest(){ 
    try { 
     new MyUser("[email protected]","Maurizio Pozzobon","01","facebook","hash").insert(); 
    } catch (Exception e) {} 
    MyUser user = MyUser.findByEmail("[email protected]"); 
    Place place = new Place(user,"posto","bel posto",null,null); 
    place.insert(); 
    assertNotNull(user); 
    Event e =new Event(user,place, "Festa","Questa è una gran bella festa",null,new Date(),(long) 10,false,null); 
    e.insert(); 
    assertNotNull(user.nome); 
    EventComment ec = new EventComment(user, e, "TestComment", new Date()); 
    ec.insert(); 
    List<EventComment> ecs = e.comments.fetch(); 
    for (EventComment comment : ecs) { 
     assertNotNull(comment.user.id); 
     MyUser us= MyUser.findById(comment.user.id); 
     assertNotNull(us.nome); 
     assertNotNull(comment.user.nome); 
    } 
} 

它未能在該行

assertNotNull(comment.user.nome); 

這不是一個大忌,因爲我仍然可以到現場做其他調用數據庫,但它似乎不可思議,我可以訪問某些領域而別人無法

在MYUSER我嘗試都宣告「諾姆」字段使用和不使用以下注釋

@Column("nome") 
@Max(200) @NotNull 
public String nome; 

回答

0

沒有基本的,這是正常的。
您使用GAE,對嗎?
在GAE中,請記住,在SQL DB中沒有JOIN。
當您獲取評論時,鏈接的用戶不會被完全提取,但只填寫user.id字段。這就是爲什麼assertNotNull(comment.user.id)是好的。
因此,默認情況下,如果您希望用戶關聯到評論,則需要手動抓取。

這個限制應該很快就會改變,因爲我們將很快提供實體分組,並且會有一個新的註釋@Join自動獲取鏈接的實體。

您可以嘗試此註釋,但尚未最終確定。 在您的評論類中,添加@Join:

@Join 
@Column("user") 
User user; 

然後,當你取一個評論,它也將與它獲取用戶。

Comment comment = Comment.findById("id", value); 
assertNotNull(comment.user.nome); // will be OK. 

但它不應該在的情況下工作:List<EventComment> ecs = e.comments.fetch();
此連接更爲複雜,直到我們有實體分組,它會消耗幕後太多資源。

+0

非常感謝你總是清理乾淨! – 2011-05-29 13:40:31

相關問題