2009-09-30 71 views
1

爲了簡化,我省略了一些代碼(包聲明,導入,其他字段) 。 我這裏有簡單的一對多關係。 它工作得很好,直到這一刻。一對多關係。從數據存儲中選擇對象

@PersistenceCapable(identityType = IdentityType.APPLICATION, 
detachable="true") 
class Restaurant implements Serializable { 

@PrimaryKey 
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
Key id 

@Persistent(mappedBy = "restaurant") 
List<RestaurantAddress> addresses = new ArrayList<RestaurantAddress>() 
} 

// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =

@PersistenceCapable(identityType = IdentityType.APPLICATION, 
detachable="true") 
class RestaurantAddress implements Serializable { 

@PrimaryKey 
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
Key id 

@Persistent 
Restaurant restaurant 
} 

現在我需要獲得(選擇)從數據庫中的所有的餐館:

def getRestaurantsToExport(final String dst, final int count) { 
    String field = restaurantExportFields[dst] 
    return transactionExecute() { PersistenceManager pm -> 
    Query q = pm.newQuery(Restaurant.class) 
    q.filter = "$field == null" 
    q.setRange(0, count) 
    return q.execute() 
    } 
} 

但也有問題 - 查詢給我12家餐館(如數據庫),但 每家餐廳都有0地址,但在數據存儲每家餐館都有 最低2添加resses。

有沒有人有同樣的問題或知道解決方案?

回答

1

的「渴望」加載如果有人有同樣的問題:

更換

@Persistent(mappedBy = "restaurant") 
List<RestaurantAddress> addresses = new 
ArrayList<RestaurantAddress> 

@Persistent(mappedBy = "restaurant",defaultFetchGroup = "true") 
List<RestaurantAddress> addresses = new 
ArrayList<RestaurantAddress> 

另一種方法是否必須在關閉 PersistentManager前檢索列表中的每個餐館「touch」地址屬性 。在PersistenManager關閉後,您不能從數據存儲中檢索任何東西,並且Restaurant不會爲空。

解決方案在google-appengine-java用戶的幫助下找到。

+0

您是否有鏈接到與谷歌相關的此討論? – pkaeding 2010-10-01 21:25:30

1

你確定地址沒有被延遲加載嗎?只是一個猜測...是有一些方法來強制對象