2015-02-05 25 views
0

我從Java/Objectify開始,遇到問題。GAE Java Objectify找不到父母

(A同事剛剛發佈我的問題在法國之前,因爲他不知道規則,我沒有一個帳戶。)

要解釋,我有3個班什麼工作,因爲這: 服務器有一個站點父母有什麼國家父母。

我在我的班級網站和國家工作沒有任何問題,我可以輕鬆地保存我的國家從一個網站的數據。例如:

Site site = ofy().load().type(Site.class).filter("name", "Angers").first().now(); 
System.out.println(ofy().load().key(site.getCountry()).now().getName()); 

什麼返回:「法國」

但是,當我儘量保持一個網站從服務器作爲相同的方式:

System.out.println("- Server->Site - "+ofy().load().key(a.getSite()).now().getName()); 

我得到了一個java.lang.NullPointerException

順便說一下,wh恩我做的只是一個getSite(),返回也爲空值:

Site x=ofy().load().key(a.getSite()).now(); 
if(x==null) 
System.out.println("null"); 
else 
System.out.println("NOT null"); 

結果:

不過,我做我的構造一些測試,以驗證密鑰保存好:

System.out.println(serverSite.toString()+"="+this.site.toString()); 

什麼返回密鑰(站點(4754288278503424))=鍵(站點(4754288278503424))

這是另一個考驗就是我鍵生成後做只是在程序

List<Site> yy=ofy().load().type(Site.class).filter("name","Dijon").list(); 
cleSitekey = Key.create(Site.class, yy.get(0).getId()); 
System.out.println("SITE KEY: "+cleSiteid.toString()); 
List<Site> l=ofy().load().type(Site.class).filter("name",yy.get(0).getName()).list();  
for(int i = 0; i < l.size(); i++){ 
Key<Site> keysiteserv= Key.create(Site.class, l.get(i).getId()); 
System.out.println("index " + i + " = " + l.get(i).getName()+" - "+ l.get(i).getId()); 
System.out.println(cleSitekey.toString()); 
System.out.println(cleSitekey.compareTo(keysiteserv)); 
} 

什麼秀雅0返回精確的按鍵具有相同的價值:

元素的L'指數0 =第戎 - 4754288278503424

密鑰(站點(4754288278503424))

只有在關係型數據庫方面的經驗,我不知道我是否沒有得到Keys概念,如果我誤解了它,或者我只是不知道該怎麼做。

我的類站點和服務器:

@Entity 
@Index 
public class Site { 
@Id Long idSite; 
private String name; 
@Parent Key<Country> country; 

private Site(){} 

public Site(String siteName, Key<Country> siteCountry){ 
if(siteCountry!=null){ 
List<Site> l=ofy().load().type(Site.class).filter("name",siteName).ancestor(siteCountry).list(); 

if(l.size()==0){ 
System.out.println("Object created."); 
this.name=siteName; 
this.country=siteCountry; 
ofy().save().entity(this).now(); 
} 
else 
{ 
System.out.println("Object creation stopped, it already exists."); 
} 
} 
} 
//getters+setters etc 
} 

@Entity 
@Index 
public class Server { 

@Id Long idServer; 
private String hostname; 
private String ip; 
private int status; 
private String statusDate; 
private String lastTimeUp; 
@Parent Key<Site> site; 

@SuppressWarnings("unused") 
private Server(){} 

public Server(String serverHostName, String serverIp, int serverStatus, String serverStatusDate, String serverLastTimeUp, Key<Site> serverSite){ 
if(serverSite!=null){ 

List<Server> l=ofy().load().type(Server.class).filter("hostname",serverHostName).filter("ip",serverIp).ancestor(serverSite).list();  
System.out.println("list ok."); 
if(l.size()==0){ 
this.hostname = serverHostName; 
this.ip = serverIp; 
this.status = serverStatus; 
this.statusDate = serverStatusDate; 
this.lastTimeUp = serverLastTimeUp; 
this.site = serverSite; 
System.out.println("Test start"); 
System.out.println(serverHostName+"="+this.hostname); 
System.out.println(serverIp+"="+this.ip); 
System.out.println(serverStatus+"="+this.status); 
System.out.println(serverLastTimeUp+"="+this.lastTimeUp); 
System.out.println(serverSite.toString()+"="+this.site.toString()); 
System.out.println("Test end"); 
try{ 
ofy().save().entity(this).now(); 
System.out.println("Object created."); 
} 
catch(Exception ex){ 
System.out.println("Saving interrupted."); 
System.out.println(ex.getLocalizedMessage()); 
} 
} 
else 
{ 
System.out.println("Object creation stopped, it already exists."); 
} 
} 
} 
//getters/setters/etc 
} 

感謝您的關注。

回答

0

你構建密鑰的方式對我來說並不合適。根據文檔,如果一個實體有一個父對象,那麼你必須使用Key.create(parent,class,id)方法重載,該對父對象有一個重載。 例如,創建服務器對象的關鍵它有網站作爲家長,你必須做到以下幾點:

//注:網站是父

Key<Server> serverKey = Key.create(site, Server.class, server.getId());

+0

哦,謝謝,我不明白那!我嘗試它的服務器和網站關鍵是 重點(國家(6020925673701376)/網站(4754288278503424)) 而不是 重點(站點(4754288278503424)) ,因爲我在做什麼。 非常感謝=) – Pauline 2015-02-06 10:13:49

+0

我很高興我能幫上忙。由於您是初學者,我建議您先閱讀Datastore上的Google App Engine文檔,然後閱讀完美的Objectify文檔,一切都會突然變得有意義。 :) – elcid 2015-02-06 12:02:22