简体   繁体   中英

Exception when injecting EntityManager with TomEE

I am trying to create a simple project with Java EE. However, I get a huge stacktrace.

org.apache.openejb.config.ValidationFailedException: Module failed validation. AppModule(name=BarSystem_war_exploded)
at org.apache.openejb.config.ReportValidationResults.deploy(ReportValidationResults.java:88)
at org.apache.openejb.config.AppInfoBuilder.build(AppInfoBuilder.java:312)
at org.apache.openejb.config.ConfigurationFactory.configureApplication(ConfigurationFactory.java:974)
at org.apache.tomee.catalina.TomcatWebAppBuilder.startInternal(TomcatWebAppBuilder.java:1227)
at org.apache.tomee.catalina.TomcatWebAppBuilder.configureStart(TomcatWebAppBuilder.java:1100)
at org.apache.tomee.catalina.GlobalListenerSupport.lifecycleEvent(GlobalListenerSupport.java:130)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5416)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.tomee.catalina.TomcatWebAppBuilder.deployWar(TomcatWebAppBuilder.java:663)
at org.apache.tomee.catalina.TomcatWebAppBuilder.deployWebApps(TomcatWebAppBuilder.java:622)
at org.apache.tomee.catalina.deployment.TomcatWebappDeployer.deploy(TomcatWebappDeployer.java:43)
at org.apache.openejb.assembler.DeployerEjb.deploy(DeployerEjb.java:176)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:192)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:173)
at org.apache.openejb.security.internal.InternalSecurityInterceptor.invoke(InternalSecurityInterceptor.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:192)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:173)
at org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:181)
at org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:192)
at org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:173)
at org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:85)
at org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:227)
at org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:194)
at org.apache.openejb.server.ejbd.EjbRequestHandler.doEjbObject_BUSINESS_METHOD(EjbRequestHandler.java:370)
at org.apache.openejb.server.ejbd.EjbRequestHandler.processRequest(EjbRequestHandler.java:181)

So I have a UserDAO like this:

@Stateless
public class UserDAO {

@PersistenceContext
private EntityManager em;

public void createUser(String name) {
//        User user = new User();
//        user.setName(name);
//        em.persist(user);
//        System.out.println("Maliiii");
    }
}

when I comment the EntityManager and @PersistenceContext, the TomEE runs without exceptions. However only by removing comments and deploying, I get a huge stacktrace. Before I switch to TomEE, I tried with glassfish and this problem did not occure. However, there was a problem setting up the jdbc connection pool in the application manager, so that's why i switched to tomee.

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>

<persistence-unit name="NewPersistenceUnit">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/bar"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value="root"/>
        <!--<property name="hibernate.archive.autodetection" value="class"/>-->
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hbm2ddl.auto" value="create"/>
    </properties>
</persistence-unit>

any ideas how to solve it?

First thing that comes to mind is that standard installation of TomEE has OpenJPA implementation of JPA, not hibernate is as the case with . If this is the case, try to remove <provider> from persistence.xml . There is a version of TomEE with Eclipselink (called Plume), but no official version with hibernate. If you want hibernate, you may include it as a dependency within you WAR.

However, glassfish also does not include hibernate, so you probably already have hibernate in your WAR?

  • As @OndrejM said tomcat uses apache open jpa as it's JPA Implementation so you can change your provider to

    org.apache.openjpa.persistence.PersistenceProviderImpl

to specify the persistence provider implementation

Also you will need to change the JDBC configuration propertie names to use OpneJpa instead of hibernate to be like

**

<property name="openjpa.ConnectionDriverName"value="your value" />
<property name="openjpa.ConnectionURL" value="your value" />
<property name="openjpa.ConnectionUserName" value="your value" />
<property name="openjpa.ConnectionPassword" value="your value" />

**

So I found out the error, and it really was a stupid one. Since I am using InteliJ, I am querying the "users" table from the ide. I also did query it from mysql, but without noticing the changes. It turned out that the jpa created a "Users" and "USERS" table (I tried changing the name in the @Entity) and all the time it was writing to these two tables. So I noticed the tables after refreshing the "Tables" in MySql workbench. Thanks to all for the help, and I am sorry for the dumb problem :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM