一对多关系。从数据存储中选择对象

发布于 2024-08-07 03:42:38 字数 1257 浏览 5 评论 0原文

我省略了一些代码(包声明、导入、其他字段) 为了简短。 我这里有简单的一对多关系。 直到这一刻为止一切都很好。

@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 家餐厅(如 DB 中所示)但是 每个餐厅都有 0 个地址,但在数据存储中每个餐厅都有 至少 2 个地址。

有没有人遇到同样的问题或知道解决方案?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦上情书 2024-08-14 03:42:38

您确定地址没有延迟加载吗?只是猜测......是否有某种方法可以强制“急切”加载对象

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

时光与爱终年不遇 2024-08-14 03:42:38

如果有人遇到同样的问题:

替换

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

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

另一种方法是你必须“触摸”地址属性
关闭前检索到的列表中的每个餐厅
持久管理器。 PersistenManager 关闭后,您不能
从数据存储中检索任何内容,而 Restaurant 保持为空。

google-appengine-java 用户的帮助下找到了解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文