3

我有两个不同的数据源,我需要两个不同的PersistenceManagerFactory. 我总是可以通过写一个persistence.xml文件来得到这个。但我希望以编程方式表示这一点。尽管第二个数据源保持相对不变,但第一个数据源可能会通过插件对其进行添加。这些插件可以带有一个或多个 JDO 注释类。Apersitance.xml在这里不是一个好主意,因为我希望它们在运行时加载。

在 Hibernate(和 JPA)中,这可以通过创建配置对象并向其添加所有带注释的类来实现。每当我看到一个新插件被加载时,我总是可以SessionFactory通过查看@Entity注释来关闭并使用插件中的额外类重新加载它。

在 DataNucleus/JDO 中有类似的方法吗?

我尝试在 Google 上搜索它,但我最终只能在DataNucleus 网站上解释如何编写persitence.xml文件。

4

1 回答 1

0

下面的 Scala 代码演示了如何PersistenceManager动态创建一个。您只需填充地图并将其传递给JDOHelper#getPersistenceManagerFactory.

private val pu  = props(db.driver, db.url, db.username, db.password)
private val pmf = JDOHelper.getPersistenceManagerFactory(pu.asJava)
private val pm  = pmf.getPersistenceManager.asInstanceOf[JDOPersistenceManager]

下面您可以看到一个示例,说明在和的情况H2下您可能愿意如何填充此类地图:MongoDBPostgreSQL

  def props(driver: String, url: String, username: String, password: String): Map[String, Any] =
    driver match {
      case "org.h2.Driver" =>
        Map[String, Any](
          "javax.jdo.option.Mapping"                 -> "h2",
          "datanucleus.schema.autoCreateAll"         -> "true",
          "javax.jdo.PersistenceManagerFactoryClass" -> "org.datanucleus.api.jdo.JDOPersistenceManagerFactory",
          "javax.jdo.option.ConnectionDriverName"    -> driver,
          "javax.jdo.option.ConnectionURL"           -> url,
          "javax.jdo.option.ConnectionUserName"      -> username,
          "javax.jdo.option.ConnectionPassword"      -> password
        )

      case "org.postgresql.Driver" =>
        Map[String, Any](
          "datanucleus.schema.autoCreateAll"         -> "true",
          "javax.jdo.PersistenceManagerFactoryClass" -> "org.datanucleus.api.jdo.JDOPersistenceManagerFactory",
          "javax.jdo.option.ConnectionDriverName"    -> driver,
          "javax.jdo.option.ConnectionURL"           -> url,
          "javax.jdo.option.ConnectionUserName"      -> username,
          "javax.jdo.option.ConnectionPassword"      -> password,
          "javax.jdo.option.RetainValues"            -> "true",
          "javax.jdo.option.RestoreValues"           -> "true",
          "javax.jdo.option.Optimistic"              -> "true",
          "javax.jdo.option.NontransactionalWrite"   -> "false",
          "javax.jdo.option.NontransactionalRead"    -> "true",
          "javax.jdo.option.Multithreaded"           -> "true",
          "javax.jdo.option.IgnoreCache"             -> "false"
        )

      case "mongodb.jdbc.MongoDriver" =>
        Map[String, Any](
          "javax.jdo.option.Mapping"                 -> "mongo",
          "datanucleus.schema.autoCreateAll"         -> "true",
          "javax.jdo.PersistenceManagerFactoryClass" -> "org.datanucleus.api.jdo.JDOPersistenceManagerFactory",
          "javax.jdo.option.ConnectionDriverName"    -> driver,
          "javax.jdo.option.ConnectionURL"           -> url,
          "javax.jdo.option.ConnectionUserName"      -> username,
          "javax.jdo.option.ConnectionPassword"      -> password
        )

      case _ => throw new IllegalArgumentException(s"unknown driver %s".format(driver))
    }
于 2015-06-05T22:27:45.687 回答