我正在使用spring 4.2.2.RELEASE
hibernate 5.0.2.Final
开发一个项目,并在jboss Application server 7.1.1上部署它。我必须使用jboss进行部署,这是一项艰难的要求。
我不想使用注释来配置JPA实体(这是一个很难的要求)所以为此我定义了一个orm.xml
文件映射
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<description>OR Mapping</description>
<persistence-unit-metadata>
<xml-mapping-metadata-complete />
</persistence-unit-metadata>
<mapped-superclass class="com.lba.backend.rest.models.IdentifiableEntity">
<attributes>
<id name="id" />
</attributes>
</mapped-superclass>
<entity class="com.company.rest.models.User">
<table name="MyUser" />
<attributes>
<id name="id" />
<basic name="username" />
<basic name="firstName" />
<basic name="lastName" />
<basic name="image" />
</attributes>
</entity>
定义此映射后,当我将war文件部署到jboss 7.1.1应用程序服务器时,我在服务器启动时遇到异常。
我甚至看过hibernate源代码,这就是抛出异常的行AnnotationProxy#L87-89
if ( processedValuesFromDescriptor != descriptor.numberOfElements() ) {
throw new RuntimeException( "Trying to instanciate " + annotationType + " with unknown elements" );
}
当我从xml文件中删除表元素时,它可以正常工作,但它会为
抛出相同的异常<join-column name="roleId" insertable="false" updatable="false" />
我为我的控制器和服务编写了Junit4测试用例,但这并没有在那里发生。我使用spring mvc测试框架来编写使用SpringJUnit4ClassRunner
类的单元测试。
有人知道为什么我会收到这个例外吗?
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/backend-rest-1.0]] (MSC service thread 1-2) Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myEmf' defined in ServletContext resource [/WEB-INF/application-context.xml]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Trying to instanciate interface javax.persistence.Table with unknown elements
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) [spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) [spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) [spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) [spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) [spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1048) [spring-context-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:825) [spring-context-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) [spring-context-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446) [spring-web-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328) [spring-web-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) [spring-web-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_79]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_79]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]
Caused by: java.lang.RuntimeException: Trying to instanciate interface javax.persistence.Table with unknown elements
at org.hibernate.annotations.common.annotationfactory.AnnotationProxy.getAnnotationValues(AnnotationProxy.java:88) [hibernate-commons-annotations-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.annotations.common.annotationfactory.AnnotationProxy.<init>(AnnotationProxy.java:69) [hibernate-commons-annotations-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.annotations.common.annotationfactory.AnnotationFactory.create(AnnotationFactory.java:80) [hibernate-commons-annotations-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.annotations.common.annotationfactory.AnnotationFactory.create(AnnotationFactory.java:53) [hibernate-commons-annotations-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader.getTable(JPAOverriddenAnnotationReader.java:2780) [hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader.initAnnotations(JPAOverriddenAnnotationReader.java:345) [hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader.isAnnotationPresent(JPAOverriddenAnnotationReader.java:314) [hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.annotations.common.reflection.java.JavaXAnnotatedElement.isAnnotationPresent(JavaXAnnotatedElement.java:60) [hibernate-commons-annotations-5.0.0.Final.jar:5.0.0.Final]
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.categorizeAnnotatedClass(AnnotationMetadataSourceProcessorImpl.java:115) [hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.<init>(AnnotationMetadataSourceProcessorImpl.java:104) [hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.<init>(MetadataBuildingProcess.java:147) [hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:141) [hibernate-core-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:770) [hibernate-entitymanager-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:797) [hibernate-entitymanager-5.0.2.Final.jar:5.0.2.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:135) [hibernate-entitymanager-5.0.2.Final.jar:5.0.2.Final]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343) [spring-orm-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) [spring-orm-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) [spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) [spring-beans-4.2.2.RELEASE.jar:4.2.2.RELEASE]
... 20 more
21:39:25,475 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-2) Error listenerStart
21:39:25,476 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-2) Context [/backend-rest-1.0] startup failed due to previous errors
21:39:25,488 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/backend-rest-1.0]] (MSC service thread 1-2) Closing Spring root WebApplicationContext
21:39:25,498 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC00001: Failed to start service jboss.web.deployment.default-host."/backend-rest-1.0": org.jboss.msc.service.StartException in service jboss.web.deployment.default-host."/backend-rest-1.0": JBAS018040: Failed to start context
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:95)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_79]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_79]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]
答案 0 :(得分:2)
Syed,正如您发布的link所示,问题在于混合了JPA 2.0和JPA 2.1库。您的测试可能正常工作,因为2.1类库首先显示的类路径。在您的应用程序服务器上,首先显示2.0库(可能是hibernate-jpa-2.0-api),然后出现错误
我有同样的问题但是在不同的设置中。我的解决方案是从我的pom.xml中排除对旧的hibernate-jpa-2.0-api lib的传递依赖:
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa-codegen</artifactId>
<version>4.1.2</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
</exclusion>
</exclusions>
</dependency>