openejb嵌入式容器找不到persistence.xml [英] openejb embedded container cannot find persistence.xml

查看:95
本文介绍了openejb嵌入式容器找不到persistence.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在嵌入式模式下使用openEjb来测试一个注入了EntityManager的无状态会话bean。但是,当我运行测试时,它会失败,因为它无法初始化应用程序。当我看到控制台,我也可以看到一个与无法找到persistence.xml相关的错误



注意:我尝试将WebContent以及META-INF放在类路径,但这并不帮助。



更新:



在使用openejb + eclipse时,资源查找是非常奇怪的。


  1. 我将WebContent添加到源文件夹。我停止获取persistence.xml错误,但现在我收到一个错误,表明openejb找不到任何托管资源

  2. 我将WebContent添加到源文件夹,但使用不同的输出文件夹使用允许输出文件夹的源文件夹),并且persistence.xml错误再次开始发生

  3. 我将META-INF复制到'test',这是所有测试的源文件夹,以及所有的运行良好

  4. 关于点1和2,问题似乎是WEB-INF也被包含在WebContent的一部分。如果我排除WEB-INF,那么再次openejb可以在测试中找到persistence.xml,测试运行正常

我老实无聊关于发生了什么。



我将所有相关错误和代码粘贴到下面。



错误在Eclipse中失败的JUnit测试的堆栈

  org.apache.openejb.OpenEjbContainer $ InvalidApplicationException:org.apache.openejb .config.ValidationFailedException:模块验证失败。 AppModule(name =)
在org.apache.openejb.OpenEjbContainer $ Provider.createEJBContainer(OpenEjbContainer.java:273)
在javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:56)
在javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:43)
在com.diycomputerscience.slides.service.SlideServiceTest.setUp(SlideServiceTest.java:45)
在junit。 framework.TestCase.runBare(TestCase.java:128)
at junit.framework.TestResult $ 1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124 )
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite。 runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
在junit.framework.TestSuite.run(TestSuite.java:225)
在org.ecli pse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java :683)
在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (RemoteTestRunner.java:197)
导致:org.apache.openejb.config.ValidationFailedException:模块验证失败。在org.apache.openejb.config.AppInfoBuilder.build(AppInfoBuilder.java:264)上的
在org.apache.openejb.config.ReportValidationResults.deploy(ReportValidationResults.java:82)上的AppModule(name =)

在org.apache.openejb.config.ConfigurationFactory.configureApplication(ConfigurationFactory.java:696)
在org.apache.openejb.OpenEjbContainer $ Provider.createEJBContainer(OpenEjbContainer.java:267)
... 18更多

我的控制台输出:

  Apache OpenEJB 4.0.0-beta-2 build:20120115-08:26 
http://openejb.apache.org/
INFO - openejb.home = / home / user / workspace / wwald / slides
INFO - openejb.base = / home / user / workspace / wwald / slides
INFO - 使用'javax。 ejb.embeddable.EJBContainer = true'
INFO - 配置服务(id =默认安全服务,type = SecurityService,provider-id =默认安全服务)
INFO - 配置服务(id =默认事务管理器,键入=反式actionManager,provider-id =默认事务管理器)
INFO - 检查应用程序的类路径:38个网址。考虑调整您的排除/包含。当前设置:openejb.deployments.classpath.exclude ='',openejb.deployments.classpath.include ='。*'
INFO - 在类路径中找到EjbModule:/ home / user / workspace / wwald / slides / build / classes
INFO - 在2000毫秒内搜索了38个classpath urls。平均每个网址52毫秒。
INFO - 开始载入:/ home / user / workspace / wwald / slides / build / classes
INFO - 配置企业应用程序:/ home / user / workspace / wwald / slides
INFO - 配置服务(id =默认无状态容器,type = Container,provider-id =默认无状态容器)
INFO - 自动创建bean的容器SlideService:Container(type = STATELESS,id = Default Stateless Container)
INFO - 配置服务(id =默认托管容器,type = Container,provider-id =默认托管容器)
INFO - 自动创建bean的容器com.diycomputerscience.slides.service.SlideServiceTest:Container(type = MANAGED,id =默认管理容器)
INFO - 将生成的ejb-jar.xml转储到:/tmp/ejb-jar-5804778531295096416slides.xml
INFO - 将生成的openejb-jar.xml转储到:/ tmp / openejb-jar-2921830618491817127slides.xml
错误 - 失败... SlideService:将@PersistenceContext refem缺少所需的persistence.xml到单位entity
ERROR - 无效EjbModule(name = slides,path = / home / user / workspace / wwald / slides / build / classes)
INFO - 将openejb.validation.output.level系统属性设置为VERBOSE以增加验证细节。
WARN - configureApplication.loadFailed

无状态会话Bean

  @Stateless 
public class SlideService {
@PersistenceContext(unitName =entities,type = PersistenceContextType .TRANSACTION)
private EntityManager em;

//为简洁而不显示各种商业方法
}

persistence.xml

 < persistence version =1.0
xmlns = http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http:// java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd\">
< persistence-unit name =entities>
< provider> org.apache.openjpa.persistence.PersistenceProviderImpl< / provider>
< jta-data-source> myds< / jta-data-source>
< non-jta-data-source> myds< / non-jta-data-source>
<属性>
< property name =openjpa.jdbc.SynchronizeMappingsvalue =buildSchema/>
< / properties>
< / persistence-unit>
< / persistence>

JUnit测试用例:

  public class SlideServiceTest扩展TestCase {

private SlideService slideService;

public SlideServiceTest(String name){
super(name);
}

protected void setUp()throws Exception {
super.setUp();

final属性p = new Properties();
p.put(myds,new:// Resource?type = DataSource);
p.put(myds.JdbcDriver,org.hsqldb.jdbcDriver);
p.put(myds.JdbcUrl,jdbc:hsqldb:mem:slidedb);

EJBContainer ejbContainer = EJBContainer.createEJBContainer();
Object oSlideService = ejbContainer.getContext()。lookup(java:global / slides / SlideService);
assertNotNull(oSlideService);
this.slideService =(SlideService)oSlideService;
}

//没有显示测试方法的简洁,因为代码在setUp本身失败
}


解决方案

我收到了同样的异常,问题是两个 persistence.xml 在OpenEJB的路径上。



当我删除其中一个时,问题解决了。



异常消息在OpenEJB的一部分是没有帮助的,但是如果您调试到 org.apache.openejb.config.Appmodule hasFailures() hasErrors()方法,您可以在 EjbModule.getValidation()中看到更多有用的信息来电! (4.0.0版)。


I am using openEjb in embedded mode to test a stateless session bean, which has an injected EntityManager. However, when I run the test, it fails because it could not initialize the application. When I see the console I can also see an error related to not being able to find persistence.xml

Note: I tried putting WebContent, as well as META-INF in the classpath, but that does not help either.

Update:

There is something very strange about resource lookups when using openejb + eclipse.

  1. I added WebContent to source folders. I stopped getting the persistence.xml error, but now I get an error which suggests that openejb could not find any managed resource
  2. I added WebContent to source folders, but with a different output folder (using allow output folders for source folders), and the persistence.xml error starts occuring again
  3. I copied META-INF into 'test', which is the source folder for all tests, and everything runs fine
  4. Regarding points 1, and 2, teh issue seems to be the fact that WEB-INF also gets included as part of WebContent. If I exclude WEB-INF, then again openejb can find persistence.xml in the test and the test runs fine

I am honestly clueless about what is happening.

I am pasting all the relevant errors and code below.

Error stack from failing JUnit test in Eclipse

org.apache.openejb.OpenEjbContainer$InvalidApplicationException: org.apache.openejb.config.ValidationFailedException: Module failed validation. AppModule(name=)
    at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:273)
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:56)
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:43)
    at com.diycomputerscience.slides.service.SlideServiceTest.setUp(SlideServiceTest.java:45)
    at junit.framework.TestCase.runBare(TestCase.java:128)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:230)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at junit.framework.TestSuite.runTest(TestSuite.java:230)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.openejb.config.ValidationFailedException: Module failed validation. AppModule(name=)
    at org.apache.openejb.config.ReportValidationResults.deploy(ReportValidationResults.java:82)
    at org.apache.openejb.config.AppInfoBuilder.build(AppInfoBuilder.java:264)
    at org.apache.openejb.config.ConfigurationFactory.configureApplication(ConfigurationFactory.java:696)
    at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:267)
    ... 18 more

My console output:

Apache OpenEJB 4.0.0-beta-2    build: 20120115-08:26
http://openejb.apache.org/
INFO - openejb.home = /home/user/workspace/wwald/slides
INFO - openejb.base = /home/user/workspace/wwald/slides
INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Inspecting classpath for applications: 38 urls. Consider adjusting your exclude/include.  Current settings: openejb.deployments.classpath.exclude='', openejb.deployments.classpath.include='.*'
INFO - Found EjbModule in classpath: /home/user/workspace/wwald/slides/build/classes
INFO - Searched 38 classpath urls in 2000 milliseconds.  Average 52 milliseconds per url.
INFO - Beginning load: /home/user/workspace/wwald/slides/build/classes
INFO - Configuring enterprise application: /home/user/workspace/wwald/slides
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
INFO - Auto-creating a container for bean SlideService: Container(type=STATELESS, id=Default Stateless Container)
INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
INFO - Auto-creating a container for bean com.diycomputerscience.slides.service.SlideServiceTest: Container(type=MANAGED, id=Default Managed Container)
INFO - Dumping Generated ejb-jar.xml to: /tmp/ejb-jar-5804778531295096416slides.xml
INFO - Dumping Generated openejb-jar.xml to: /tmp/openejb-jar-2921830618491817127slides.xml
ERROR - FAIL ... SlideService:  Missing required persistence.xml for @PersistenceContext ref "em" to unit "entities"
ERROR - Invalid EjbModule(name=slides, path=/home/user/workspace/wwald/slides/build/classes)
INFO - Set the 'openejb.validation.output.level' system property to VERBOSE for increased validation details.
WARN - configureApplication.loadFailed

The Stateless Session Bean:

@Stateless
public class SlideService {
    @PersistenceContext(unitName="entities", type=PersistenceContextType.TRANSACTION)
    private EntityManager em;

    //various business methods not shown for brevity
}

persistence.xml

<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="entities">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <jta-data-source>myds</jta-data-source>
        <non-jta-data-source>myds</non-jta-data-source>
        <properties>
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
        </properties>
    </persistence-unit>
</persistence>

The JUnit test case:

public class SlideServiceTest extends TestCase {

    private SlideService slideService;

    public SlideServiceTest(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        super.setUp();

        final Properties p = new Properties();
        p.put("myds", "new://Resource?type=DataSource");
        p.put("myds.JdbcDriver", "org.hsqldb.jdbcDriver");
        p.put("myds.JdbcUrl", "jdbc:hsqldb:mem:slidedb");

        EJBContainer ejbContainer = EJBContainer.createEJBContainer();
        Object oSlideService = ejbContainer.getContext().lookup("java:global/slides/SlideService");
        assertNotNull(oSlideService);
        this.slideService = (SlideService)oSlideService;
    }

    //not showing test methods for brevity, since the code fails in setUp itself
}

解决方案

I received the same exception and the problem was that two persistence.xml were on the classpath of OpenEJB.

When I removed one of them, the problem was solved.

The exception message is not helpful here on part of OpenEJB, but if you debug into org.apache.openejb.config.Appmodule's hasFailures() or hasErrors() method, you can see more useful info in EjbModule.getValidation() calls! (Version 4.0.0).

这篇关于openejb嵌入式容器找不到persistence.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆