persistence.xml配置到纯java jdo配置

r55awzrz  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(242)

我有这个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/>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题