persistence.xml 配置到纯 Java JDO 配置

问题描述

我有这个 persistence.xml 配置文件

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">
    <persistence-unit name="transactions-optional">
        <class>myapp.Room</class>
        <exclude-unlisted-classes/>
        <properties>
            <property name="javax.jdo.PersistenceManagerFactoryClass"
                      value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" />
            <property name="datanucleus.ConnectionURL" value="mongodb:localhost:27017/admin" />
            <property name="datanucleus.ConnectionUserName" value="admin" />
            <property name="datanucleus.ConnectionPassword" value="adminadmin" />
            <property name="datanucleus.storeManagerType" value="mongodb" />
            <property name="datanucleus.autocreateSchema" value="true" />
        </properties>
    </persistence-unit>
</persistence>

我需要将其转换为纯 Java 代码实现,如下所示:

protected PersistenceManager getPersistenceManager() {
  if (pm == null) {
    synchronized(lock) {
      if (pm == null) {
        Properties newProperties = new Properties();
        newProperties.put("javax.jdo.PersistenceManagerFactoryClass","org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
        newProperties.put("datanucleus.ConnectionURL","mongodb:localhost:27017/admin");
        newProperties.put("datanucleus.ConnectionUserName","admin");
        newProperties.put("datanucleus.ConnectionPassword","adminadmin");
        newProperties.put("datanucleus.storeManagerType","mongodb");
        newProperties.put("datanucleus.autocreateSchema","true");
        pmf = JDOHelper.getPersistenceManagerFactory(newProperties,"transactions-optional");
        pm = pmf.getPersistenceManager();;
      }
    }
  }
  return pm;
}

如何将这些属性添加到 JDOHelper 初始化中?

<class>myapp.Room</class>
<exclude-unlisted-classes/>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)