WSO2和JPA配置(JNDI)

时间:2018-07-18 14:57:52

标签: java hibernate jpa wso2 osgi

我正在尝试通过WSO2 ESB使用JPA。我的模块B中捆绑了我的实体,因为模块A会生成JAR,并且应该在WSO2 OSGi模块中使用。 在模块A中,我得到了:

  • 实体包以及所有实体
  • 在/ resources / META-INF文件夹下的
  • persistence.xml

    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <non-jta-data-source>jdbc/PathToJNDI</non-jta-data-source>
    <class>com.moduleA.entities.Locks</class>
    <class>com.moduleA.entities.Logs</class>
    <class>com.moduleA.entities.MessageContent</class>
    <class>com.moduleA.entities.Messages</class>
    <class>com.moduleA.entities.Parts</class>
    <class>com.moduleA.entities.Recipients</class>
    <class>com.moduleA.entities.Test</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.persistence-context.flush-mode" value="commit" />
    </properties>
    

要获取持久性属性,我正在使用JNDI。它在WSO2面板中配置。

这些元素通过maven-bundle-plugin打包到模块B。

<plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>2.3.4</version>
        <extensions>true</extensions>
        <configuration>
          <instructions>
          <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
          <Bundle-Name>${project.artifactId}</Bundle-Name>
            <Export-Package>com.coig,com.coig.*,pl.coig.*, com.moduleA.entities.*</Export-Package>
            <DynamicImport-Package>*</DynamicImport-Package>
            <Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
            <JPA-PersistenceUnits>JPAPU</JPA-PersistenceUnits>
            <Import-Package>
                *, org.hibernate*,org.hibernate.proxy, javassist.util.proxy,
                javax.persistence;version="1.1.0";jpa="2.0",
                com.microsoft.sqlserver.jdbc, ${extra.packages},
                javax.transaction.*,
                *;resolution:=optional,
                org.osgi.framework
            </Import-Package>
            <Embed-Dependency>entities</Embed-Dependency>
          </instructions>
        </configuration>
      </plugin>

构建Maven之后,我可以看到模块B清单包含配置中的所有内容。

然后,我尝试以mediate()方法从WSO2 Mediator项目中的数据库中获取所有记录的列表。

Map<String, Object> properties = new HashMap<>();
properties.put("openjpa.ConnectionDriverName","com.microsoft.sqlserver.jdbc.SQLServerDriver");
properties.put("openjpa.ConnectionFactoryName","jdbc/PathToJNDI");

EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPU",properties);
try{
    EntityManager entityManager = emf.createEntityManager();
    try{
        log.severe("EntityManagerFactory and Entity manager created");
        entityManager.getTransaction().begin();
        try{
            log.severe("Transaction started");
            Query q = entityManager.createNamedQuery("Test.findAll", Test.class);
            List<Test> list = q.getResultList();
            for ( Test s : list ) {
                log.severe(s.getName());
            }
            entityManager.getTransaction().commit();
        } catch (Exception e1){
            if(entityManager.getTransaction().isActive()){
                entityManager.getTransaction().rollback();
            }
            log.log(Level.INFO, e1.getMessage(), e1);
        }
    }catch (Throwable e){
        log.severe("EntityManager exception: " + e.getMessage());
    }finally {
        entityManager.close();
    }
}catch(Exception e2){
    log.severe("EntityManagerFactory exception: " + e2.getMessage());
}finally {
    emf.close();
}

是否需要在属性中提供驱动程序和工厂名称以进行映射,然后使用它来创建EntityManagerFactory?没有这些属性,我会得到警告,OpenJPA找不到数据源驱动程序。

我收到消息,尽管Maven依赖项中提供了该应用程序,但该应用程序未找到Test.class的类路径,并且带有实体的包已导入到MediatorProject pom下的maven-bundle-plugin中。我不想添加带有实体的MediatorProject Path文件夹,但是我想通过maven-bundle或其他方式(如果您知道如何使其起作用:)来导入它们

为满足JPA交易,我应该对配置进行哪些更改?

0 个答案:

没有答案