Question

I've omitted some code(package declarations, imports, other fields) for shortness. I have here simple One-to-Many relation. It worked fine till this moment.

@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
}

Now i need to get(select) all the Restaurants from DB:

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()
   }
 }

But there are on problem - query gives me 12 restaurants(as in DB) but every Restaurant has 0 Address but in Datastore every Restaurant has minimum 2 addresses.

Have anyone the same problem or knows the solution ?

Was it helpful?

Solution 2

If someone will have the same problem:

Replace

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

with

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

Another method is that you have to "touch" addresses property for every Restaurant in the retrieved list before closing PersistentManager. After PersistenManager being closed you cannot retrieve anything from datastore and Restaurant keeps null.

Solution found with help of google-appengine-java users.

OTHER TIPS

are you sure the Addresses are not lazy loaded? Just a guess... is there some way to force an "eager" loading of the objects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top