【问题标题】:Integration Testing an OpenJPA Data Access Object or Service Facade?集成测试 OpenJPA 数据访问对象或服务外观?
【发布时间】:2012-03-26 16:47:30
【问题描述】:

我正在使用 struts2 和 openJPA 构建一个项目。我想做一些集成测试,但我似乎在让它工作时遇到了问题。

persistence.xml

<persistence-unit name="SalesCertIT" transaction-type="RESOURCE_LOCAL">
    <jta-data-source>jdbc/salesCertIT</jta-data-source>
    <class>com.ist.salesCert.entities.Certification</class>
    <properties>
        <property name="log4j" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />
        <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
        <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/sales_certification" />
        <property name="openjpa.ConnectionUserName" value="dev" />
        <property name="openjpa.ConnectionPassword" value="password" />
        <property name="openjpa.Id" value="SalesCertIT" />
        <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver" />
    </properties>
</persistence-unit>

类:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("SalesCertIT");
EntityManager em = emf.createEntityManager();

我得到错误:

必须在 ConnectionDriverName 属性。

我已将 persistence.xml 和 mysql-connector-java-*-stable-bin-jar 添加到类路径中,(Eclipse->debug Configuration->Classpath->Bootstrap 条目)

如果我尝试在运行时对其进行配置,它确实可以工作,但在尝试执行操作时出现另一个错误:

HashMap<String, String> conf = new HashMap<String, String>();
conf.put("openjpa.ConnectionDriverName", "com.mysql.jdbc.Driver");
conf.put("openjpa.ConnectionURL", "jdbc:mysql://localhost:3306/sales_certification");
conf.put("openjpa.ConnectionUserName", "dev");
conf.put("openjpa.ConnectionPassword", "password");
conf.put("openjpa.TransactionMode", "local");
conf.put("openjpa.Id", "SalesCertIT");      
EntityManagerFactory emf = Persistence.createEntityManagerFactory("SalesCertIT", conf);
EntityManager em = emf.createEntityManager();

类型“com.ist.salesCert.entities.Certification”尚未 增强。

我尝试添加一个 javaagent 参数:(Eclipse->Debug Configurations->Arguments->VM arguments)

-javaagent:C:/Progra~1/IBM/WebSphere/AppServer/plugins/com.ibm.ws.jpa.jar

在这一点上,我不知道还能尝试什么。有什么想法吗?

【问题讨论】:

  • 你想在 JSE 或 JEE 中运行它吗?在您的第一个配置部分,为什么您有多个 openjpa.ConnectionDriverName 属性?当你的测试运行时,除了 xyz 之外,是否还有其他有意义的消息没有得到增强?

标签: java integration-testing openjpa persistence.xml


【解决方案1】:

一个问题是我试图遵守 JEE6 规范,但 websphere 7 上的 openjpa 2 是 J2EE。 我发现增强我的 openjpa 实体的最终解决方案是将 maven 与 openjpa 插件一起使用:

        <!-- openjpa plugin -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>openjpa-maven-plugin</artifactId>
            <version>1.2</version>
            <configuration>
                <includes>com/ist/salesCert/entities/*.class</includes>
                <excludes>com/ist/salesCert/entitles/Quarters.class</excludes>
                <addDefaultConstructor>true</addDefaultConstructor>
                <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
                <persistenceXmlFile>WebRoot/META-INF/persistence.xml</persistenceXmlFile>
                <detail>true</detail>
            </configuration>
            <executions>
                <execution>
                    <id>enhancer</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

然后这个命令将增强实体:

mvn openjpa:enhance

注意:pom.xml 文件中必须有其他依赖项

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多