希望有人可以帮助解决这个问题,因为我发现的所有答案似乎都不适合我。

在我的myProj网络应用程序下,我有以下内容

./WEB=INF/lib/myproj-user.jar containing this class

@Entity
@Table(name = "user_details")
@Data
@EqualsAndHashCode(callSuper=false)
public class UserDetails extends CommonObject {
    ...
}




./WEB=INF/lib/myproj-common.jar containing this class

@MappedSuperclass
public abstract class CommonObject implements Serializable {

    private static final long serialVersionUID = -980608025624315940L;

    @Getter @Setter
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    ...
}


而我的application-context.xml具有此功能(application-context.xml具有称为hibernate-context和model-context.xml的子XML,但是它们一直很好)

<bean id="commonAuto" class="com.myproj.common.model.CommonObject"/>
<bean id="userDetails" class="com.myproj.user.model.UserDetails"/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.myproj.common.model.CommonObject</value>
            <value>com.myproj.user.model.UserDetails</value>


但是在启动时,我的日志中出现以下错误


  严重:将上下文初始化事件发送给侦听器的异常
  类的实例
  org.springframework.web.context.ContextLoaderListener
  org.springframework.beans.factory.BeanCreationException:错误
  创建名称为“ userService”的bean:自动接线的注入
  依赖失败;嵌套异常为
  org.springframework.beans.factory.BeanCreationException:无法
  autowire字段:com.myproj.user.dao.UserDao
  com.myproj.user.servces.impl.UserServiceImpl.userDao;嵌套异常
  是org.springframework.beans.factory.BeanCreationException:错误
  创建名称为“ userAccountDao”的bean:自动接线的注入
  依赖失败;嵌套异常为
  org.springframework.beans.factory.BeanCreationException:无法
  autowire字段:私有org.hibernate.SessionFactory
  com.myproj.user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常
  是org.springframework.beans.factory.BeanCreationException:错误
  创建在ServletContext中定义的名称为'sessionFactory'的bean
  资源[/WEB-INF/context/hibernate-context.xml]:调用init
  方法失败;嵌套的异常是org.hibernate.AnnotationException:
  未为实体指定标识符:com.myproj.user.model.UserDetails
    在
  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    在
  org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:295)
    在
  org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    在
  org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    在
  org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    在
  org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    在
  org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    在
  org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    在
  org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
    在
  org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
    在
  org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    在
  org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4323)
    在
  org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4780)
    在
  org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    在
  org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:785)
    在
  org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:763)
    在
  org.apache.catalina.core.StandardHost.addChild(StandardHost.java:557)
    在
  org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:987)
    在
  org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
    在
  org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:540)
    在org.apache.catalina.startup.HostConfig.start(HostConfig.java:1390)
    在
  org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:355)
    在
  org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    在
  org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:89)
    在
  org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:313)
    在
  org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:293)
    在
  org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:996)
    在
  org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:771)
    在
  org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    在
  org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988)
    在
  org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:275)
    在
  org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    在
  org.apache.catalina.core.StandardService.startInternal(StandardService.java:427)
    在
  org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    在
  org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:649)
    在
  org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    在org.apache.catalina.startup.Catalina.start(Catalina.java:585)处
  sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于
  sun.reflect.NativeMethodAccessorImpl.invoke(未知源)位于
  sun.reflect.DelegatingMethodAccessorImpl.invoke(来源未知)
  java.lang.reflect.Method.invoke(来源未知)
  org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)位于
  org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)导致
  创建人:org.springframework.beans.factory.BeanCreationException:无法
  autowire字段:com.myproj.user.dao.UserDao
  com.myproj.user.servces.impl.UserServiceImpl.userDao;嵌套异常
  是org.springframework.beans.factory.BeanCreationException:错误
  创建名称为“ userAccountDao”的bean:自动接线的注入
  依赖失败;嵌套异常为
  org.springframework.beans.factory.BeanCreationException:无法
  autowire字段:私有org.hibernate.SessionFactory
  com.myproj.user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常
  是org.springframework.beans.factory.BeanCreationException:错误
  创建在ServletContext中定义的名称为'sessionFactory'的bean
  资源[/WEB-INF/context/hibernate-context.xml]:调用init
  方法失败;嵌套的异常是org.hibernate.AnnotationException:
  未为实体指定标识符:com.myproj.user.model.UserDetails
    在
  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    在
  org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    在
  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 45更多原因:
  org.springframework.beans.factory.BeanCreationException:错误
  创建名称为“ userAccountDao”的bean:自动接线的注入
  依赖失败;嵌套异常为
  org.springframework.beans.factory.BeanCreationException:无法
  autowire字段:私有org.hibernate.SessionFactory
  com.myproj.user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常
  是org.springframework.beans.factory.BeanCreationException:错误
  创建在ServletContext中定义的名称为'sessionFactory'的bean
  资源[/WEB-INF/context/hibernate-context.xml]:调用init
  方法失败;嵌套的异常是org.hibernate.AnnotationException:
  未为实体指定标识符:com.myproj.user.model.UserDetails
    在
  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    在
  org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:295)
    在
  org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    在
  org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    在
  org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    在
  org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:891)
    在
  org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:834)
    在
  org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749)
    在
  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 47更多原因:
  org.springframework.beans.factory.BeanCreationException:无法
  autowire字段:私有org.hibernate.SessionFactory
  com.myproj.user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常
  是org.springframework.beans.factory.BeanCreationException:错误
  创建在ServletContext中定义的名称为'sessionFactory'的bean
  资源[/WEB-INF/context/hibernate-context.xml]:调用init
  方法失败;嵌套的异常是org.hibernate.AnnotationException:
  未为实体指定标识符:com.myproj.user.model.UserDetails
    在
  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    在
  org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    在
  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 58更多原因:
  org.springframework.beans.factory.BeanCreationException:错误
  创建在ServletContext中定义的名称为'sessionFactory'的bean
  资源[/WEB-INF/context/hibernate-context.xml]:调用init
  方法失败;嵌套的异常是org.hibernate.AnnotationException:
  未为实体指定标识符:com.myproj.user.model.UserDetails
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    在
  org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:295)
    在
  org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    在
  org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    在
  org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    在
  org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:891)
    在
  org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:834)
    在
  org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749)
    在
  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 60更多原因:org.hibernate.AnnotationException:否
  为实体指定的标识符:com.myproj.user.model.UserDetails,位于
  org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:276)
    在
  org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)
    在
  org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:665)
    在
  org.hibernate.cfg.Configuration $ MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3431)
    在
  org.hibernate.cfg.Configuration $ MetadataSourceQueue.processMetadata(Configuration.java:3385)
    在
  org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1337)
    在
  org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1727)
    在
  org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1778)
    在
  org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242)
    在
  org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:372)
    在
  org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:357)
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
    在
  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    ...更多70

最佳答案

  @MappedSuperclass
  public abstract class CommonObject implements Serializable {
      private Date createDate;
      public abstract Long getId() ;
      public abstract void setId(Long id);

      public Date getCreateDate() {
            return createDate;
      }

      public void setCreateDate(Date createDate) {
            this.createDate = createDate;
      }
  }



   @Entity
   @Table
   public class UserDetails extends CommonObject {

        String name;
        int age;

         @GeneratedValue(strategy= GenerationType.AUTO)
         @Id
         private Long id;
         public String getName() {
                 return name;
         }

         public void setName(String name) {
              this.name = name;
         }

         public int getAge() {
               return age;
         }

         public void setAge(int age) {
                this.age = age;
         }

         public Long getId() {
                 return id;
         }

         public void setId(Long id) {
             this.id = id;
         }
   }

关于java - JPA持久类扩展了来自不同JAR的另一个类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18002022/

10-10 20:19