1

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

在我的 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 有称为 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:无法自动装配字段:com.myproj.user.dao.UserDao com.myproj.user.servces.impl.UserServiceImpl.userDao;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“userAccountDao”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.hibernate.SessionFactory com.myproj.user.dao.impl.UserDaoImpl。会话工厂;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/context/hibernate-context.xml] 中定义名称为“sessionFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:com.myproj.user.model.UserDetails java:288) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415) 原因:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:com.myproj.user.dao.UserDao com.myproj.user.servces.impl.UserServiceImpl.userDao;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“userAccountDao”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.hibernate.SessionFactory com.myproj.user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/context/hibernate-context.xml] 中定义名称为“sessionFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException: No identifier specified for entity: com.myproj.user.model.UserDetails at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) at org. springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) ... 45 更多原因:org.springframework .beans.factory.BeanCreationException:创建名为“userAccountDao”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory。BeanCreationException:无法自动装配字段:私有 org.hibernate.SessionFactory com.myproj.user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/context/hibernate-context.xml] 中定义名称为“sessionFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException: No identifier specified for entity: com.myproj.user.model.UserDetails at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) at org.springframework。 org.springframework 上的 beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)。891) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:749) 在 org.springframework.beans 的 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:834) .factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486) ... 47 更多原因:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.hibernate.SessionFactory com.myproj。 user.dao.impl.UserDaoImpl.sessionFactory;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/context/hibernate-context.xml] 中定义名称为“sessionFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException: No identifier specified for entity: com.myproj.user.model.UserDetails at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) at org. springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) ... 58 更多原因:org.springframework .beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/context/hibernate-context.xml] 中定义名称为“sessionFactory”的 bean 创建错误:调用 init 方法失败;

4

1 回答 1

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;
         }
   }
于 2013-08-01T19:32:49.267 回答