JPA持久化类从不同的JAR扩展另一个类

时间:2013-08-01 18:41:32

标签: java spring hibernate persistence

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

在我的myProj webapp下,我有以下

./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有子XML,叫做hibernate-context和model-context.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>    

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

  

SEVERE:将上下文初始化事件发送给侦听器的异常   类的实例   org.springframework.web.context.ContextLoaderListener   org.springframework.beans.factory.BeanCreationException:错误   使用名称&#39; userService&#39;创建bean:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:com.myproj.user.dao.UserDao   com.myproj.user.servces.impl.UserServiceImpl.userDao;嵌套异常   是org.springframework.beans.factory.BeanCreationException:错误   使用名称&#39; userAccountDao&#39;创建bean:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private org.hibernate.SessionFactory   com.myproj.user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常   是org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; sessionFactory&#39;的bean在ServletContext中定义   资源[/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)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)at at   org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)导致   by:org.springframework.beans.factory.BeanCreationException:不能   autowire字段:com.myproj.user.dao.UserDao   com.myproj.user.servces.impl.UserServiceImpl.userDao;嵌套异常   是org.springframework.beans.factory.BeanCreationException:错误   使用名称&#39; userAccountDao&#39;创建bean:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private org.hibernate.SessionFactory   com.myproj.user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常   是org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; sessionFactory&#39;的bean在ServletContext中定义   resource [/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:错误   使用名称&#39; userAccountDao&#39;创建bean:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private org.hibernate.SessionFactory   com.myproj.user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常   是org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; sessionFactory&#39;的bean在ServletContext中定义   resource [/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字段:private org.hibernate.SessionFactory   com.myproj.user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常   是org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; sessionFactory&#39;的bean在ServletContext中定义   resource [/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:错误   创建名为&#39; sessionFactory&#39;的bean在ServletContext中定义   resource [/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 at   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多个

1 个答案:

答案 0 :(得分:1)

  @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;
         }
   }