如何使用 Scala 中的 JDO 在 GAE 上存储子对象

发布于 2024-08-22 11:17:43 字数 1583 浏览 8 评论 0原文

我在两个类之间有父子关系,但子对象永远不会存储。我确实收到警告:

“org.datanucleus.store.appengine.MetaDataValidator checkForIllegalChildField:无法验证关系 net.vermaas.kivanotify.model.UserCriteria.internalCriteria”,

但我不清楚为什么会发生这种情况。已经尝试了几种替代方案但没有运气。

父类是“UserCriteria”,它有一个“Criteria”列表作为子类。

这些类定义如下(Scala):

class UserCriteria(tu: String, crit: Map[String, String]) extends LogHelper {
  @PrimaryKey
  @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
  var id = KeyFactory.createKey("UserCriteria", System.nanoTime)

  @Persistent
  var twitterUser = tu

  @Persistent
  var internalCriteria: java.util.List[Criteria] = flatten(crit)

  def flatten(crits: Map[String, String]) : java.util.List[Criteria] = {
    val list = new java.util.ArrayList[Criteria]
    for (key <- crits.keySet) {
      list.add(new Criteria(this, key, crits(key)))
    }
    list
  }

  def criteria: Map[String, String] = {
    val crits = mutable.Map.empty[String, String]

    for (i <- 0 to internalCriteria.size-1) {
      crits(internalCriteria.get(i).name) = internalCriteria.get(i).value
    }

    Map.empty ++ crits
  }

  // Stripped the equals, canEquals, hashCode, toString code to keep the code snippet short... 
}

@PersistenceCapable
@EmbeddedOnly
class Criteria(uc: UserCriteria, nm: String, vl: String) {

  @Persistent
  var userCriteria = uc

  @Persistent
  var name = nm

  @Persistent
  var value = vl

  override def toString = {
    "Criteria name: " + name + " value: " + value
  }
}

有什么想法为什么不存储子类吗?或者为什么我会收到错误消息?

谢谢, 下吕

I'm have a parent-child relation between 2 classes, but the child objects are never stored. I do get an warning:

"org.datanucleus.store.appengine.MetaDataValidator checkForIllegalChildField: Unable to validate relation net.vermaas.kivanotify.model.UserCriteria.internalCriteria"

but it is unclear to me why this occurs. Already tried several alternatives without luck.

The parent class is "UserCriteria" which has a List of "Criteria" as children.

The classes are defined as follows (Scala):

class UserCriteria(tu: String, crit: Map[String, String]) extends LogHelper {
  @PrimaryKey
  @Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
  var id = KeyFactory.createKey("UserCriteria", System.nanoTime)

  @Persistent
  var twitterUser = tu

  @Persistent
  var internalCriteria: java.util.List[Criteria] = flatten(crit)

  def flatten(crits: Map[String, String]) : java.util.List[Criteria] = {
    val list = new java.util.ArrayList[Criteria]
    for (key <- crits.keySet) {
      list.add(new Criteria(this, key, crits(key)))
    }
    list
  }

  def criteria: Map[String, String] = {
    val crits = mutable.Map.empty[String, String]

    for (i <- 0 to internalCriteria.size-1) {
      crits(internalCriteria.get(i).name) = internalCriteria.get(i).value
    }

    Map.empty ++ crits
  }

  // Stripped the equals, canEquals, hashCode, toString code to keep the code snippet short... 
}

@PersistenceCapable
@EmbeddedOnly
class Criteria(uc: UserCriteria, nm: String, vl: String) {

  @Persistent
  var userCriteria = uc

  @Persistent
  var name = nm

  @Persistent
  var value = vl

  override def toString = {
    "Criteria name: " + name + " value: " + value
  }
}

Any ideas why the childs are not stored? Or why I get the error message?

Thanks,
Gero

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

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

发布评论

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

评论(1

趴在窗边数星星i 2024-08-29 11:17:43

在我看来,您似乎正在尝试实现一个拥有的一-一对多关系。另外,您似乎忘记了 UserCriteria 的 internalCriteria 字段的 @Embedded 注释,但我认为它可能仍然无法工作,因为该字段包含一个列表而不是嵌入类本身。

It looks to me like you are trying to implement an owned one-to-many relationship. Also you seem to have forgotten the @Embedded annotation for the UserCriteria's internalCriteria field, though I think, that it might still not work since the field contains a list and not the embedded class itself.

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