JDO app引擎类型安全祖先查询

时间:2014-12-16 00:04:27

标签: jdo google-cloud-datastore datanucleus

我正在使用App Engine和DataNucleus JDO类型安全查询。是否可以使用DataNucleus构建类型安全的祖先查询?例如来自此应用引擎link的代码将组织称为祖先,Java中的类型安全查询是什么?

class Organization(db.Model):
    name = db.StringProperty()
class Person(db.Model):
    given_name = db.StringProperty()
    surname = db.StringProperty()
class MainPage(webapp2.RequestHandler):
    def post(self):
        org = Organization.get_or_insert('ateam', name='ATeam')
        person = Person(parent=org)
        person.given_name='GI'
        person.surname='Joe'
        person.put()
        q = db.GqlQuery("SELECT * FROM Person WHERE ANCESTOR IS :1 ", org)
        people = []
        for p in q.run():
            people.append({'given_name': p.given_name,
                        'surname': p.surname})

1 个答案:

答案 0 :(得分:0)

是的,Datanucleus有一个类型安全的东西,受到QueryDSL的启发。 您可以在Scala中看到下面的示例:

def querySingleRecords(jdbcDriver: String, jdbcUrl: String, username: String, password: String): Unit = {
  import javax.jdo.JDOHelper
  import scala.collection.JavaConverters._
  import org.datanucleus.api.jdo.JDOPersistenceManager

  val pu = props(jdbcDriver, jdbcUrl, username, password)

  val pmf = JDOHelper.getPersistenceManagerFactory(pu.asJava)
  val pm  = pmf.getPersistenceManager.asInstanceOf[JDOPersistenceManager]

  import model.{Identifier, QIdentifier}
  import org.datanucleus.query.typesafe.TypesafeQuery

  val tq: TypesafeQuery[Identifier] = pm.newTypesafeQuery(classOf[Identifier])
  val q: QIdentifier = QIdentifier.candidate()

  def find(symbol: String): Unit = {
    val i: Identifier =
      tq
        .filter(q.symbol.eq(symbol))
        .executeUnique()
        .asInstanceOf[Identifier]
    assert(symbol == i.symbol, s""""%s" not equal to "%s"""".format(symbol, i.symbol))

    if(debug) {
      println("%s -> %d".format(i.symbol, i.fk))
    }
  }

  find("PETR4.SA")
  find("ACIM")
  find("ZVV")
  find("AAIT")
  find("ZUMZ")
  find("AAAIF.OB")
  find("ZYXI.OB")
  find("AALC34.SA")
  find("ZURI9.SA")
}

对于一个使用它的完整示例,包含一个大的build.sbt文件,该文件具有生成metaclassea并运行字节码增强器的所有魔力,请查看

http://github.com/frgomes/poc-scala-datanucleus