org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany定位未映射的类:

时间:2017-02-12 07:47:01

标签: spring hibernate spring-mvc jpa nhibernate-mapping

SEVERE: Servlet [DMS1] in web application [/DMS1] threw load() exception
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.dms.bean.DMS_Document.document_Classes[com.dms.bean.DMS_DocumentClass]
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1160)
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:691)
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:626)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1586)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1359)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:343)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:431)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:416)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1227)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1140)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1027)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5038)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5348)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

这是我想要加入的表格 我添加了XML,但是当我运行代码时,我看到了这个异常。 我也添加了实体注释。

我的文档课程在

之下
    package com.dms.bean;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="DMS_Document")
public class DMS_Document {


    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name="DMS_Document_Class",
    joinColumns= @JoinColumn(name="Doc_ID"),
    inverseJoinColumns= @JoinColumn(name="Document_ID"))
    private List<DMS_DocumentClass> document_Classes;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="Doc_ID")
    private long docID;

    @Column(name="Doc_Location")
    private String docLocation;

    public String getDocLocation() {
        return docLocation;
    }

    public void setDocLocation(String docLocation) {
        this.docLocation = docLocation;
    }


    @Column(name="Document_URL")
    private String document_URL;

    @Column(name="Doc_Name")
    private String docName;

    @Column(name="Document_Description")
    private String document_Description;

    @Column(name="Document_Size")
    private int document_Size;






    public List<DMS_DocumentClass> getDocument_Classes() {
        return document_Classes;
    }

    public void setDocument_Classes(List<DMS_DocumentClass> document_Classes) {
        this.document_Classes = document_Classes;
    }

    @Column(name="Doc_Crtd_By")
    private String createdBy;

    @Column(name="Doc_Updt_By")
    private String updatedBy;

    @Column(name="Document_Extension")
    private String document_Extension;

    @Column(name="Doc_Type")
    private String docType;

    @Column(name="Document_Folder_Path")
    private String document_Folder_Path;

    @Column(name="Document_Folder_ID")
    private Long document_Folder_ID;

    @Column(name="Document_Hidden")
    private boolean documentHidden;

    @Column(name="Document_Created_Date")
    private Date document_Created_Date;

    @Column(name="Document_Updated_Date")
    private Date document_Updated_Date;

    @Column(name="Document_Version_Number")
    private String document_Version_Number;

    @Column(name="Document_Locked")
    private boolean documentLocked;

    @Column(name="Document_Locked_By")
    private String document_Locked_By;

    @Override
    public String toString() {
        return "DMSDocument [docID=" + docID + ", document_URL=" + document_URL + ", docName=" + docName
                + ", document_Description=" + document_Description + ", document_Size=" + document_Size + ", createdBy="
                + createdBy + ", updatedBy=" + updatedBy + ", document_Extension=" + document_Extension + ", docType="
                + docType + ", document_Folder_Path=" + document_Folder_Path + ", document_Folder_ID="
                + document_Folder_ID + ", documentHidden=" + documentHidden + ", document_Created_Date="
                + document_Created_Date + ", document_Updated_Date=" + document_Updated_Date
                + ", document_Version_Number=" + document_Version_Number + ", documentLocked=" + documentLocked
                + ", document_Locked_By=" + document_Locked_By + "]";
    }

    public long getDocID() {
        return docID;
    }

    public void setDocID(long docID) {
        this.docID = docID;
    }

    public String getDocument_URL() {
        return document_URL;
    }

    public void setDocument_URL(String document_URL) {
        this.document_URL = document_URL;
    }

    public String getDocName() {
        return docName;
    }

    public void setDocName(String docName) {
        this.docName = docName;
    }

    public String getDocument_Description() {
        return document_Description;
    }

    public void setDocument_Description(String document_Description) {
        this.document_Description = document_Description;
    }

    public int getDocument_Size() {
        return document_Size;
    }

    public void setDocument_Size(int document_Size) {
        this.document_Size = document_Size;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public String getDocument_Extension() {
        return document_Extension;
    }

    public void setDocument_Extension(String document_Extension) {
        this.document_Extension = document_Extension;
    }

    public String getDocType() {
        return docType;
    }

    public void setDocType(String docType) {
        this.docType = docType;
    }

    public String getDocument_Folder_Path() {
        return document_Folder_Path;
    }

    public void setDocument_Folder_Path(String document_Folder_Path) {
        this.document_Folder_Path = document_Folder_Path;
    }

    public Long getDocument_Folder_ID() {
        return document_Folder_ID;
    }

    public void setDocument_Folder_ID(Long document_Folder_ID) {
        this.document_Folder_ID = document_Folder_ID;
    }

    public boolean isDocumentHidden() {
        return documentHidden;
    }

    public void setDocumentHidden(boolean documentHidden) {
        this.documentHidden = documentHidden;
    }

    public Date getDocument_Created_Date() {
        return document_Created_Date;
    }

    public void setDocument_Created_Date(Date document_Created_Date) {
        this.document_Created_Date = document_Created_Date;
    }

    public Date getDocument_Updated_Date() {
        return document_Updated_Date;
    }

    public void setDocument_Updated_Date(Date document_Updated_Date) {
        this.document_Updated_Date = document_Updated_Date;
    }

    public String getDocument_Version_Number() {
        return document_Version_Number;
    }

    public void setDocument_Version_Number(String document_Version_Number) {
        this.document_Version_Number = document_Version_Number;
    }

    public boolean isDocumentLocked() {
        return documentLocked;
    }

    public void setDocumentLocked(boolean documentLocked) {
        this.documentLocked = documentLocked;
    }

    public String getDocument_Locked_By() {
        return document_Locked_By;
    }

    public void setDocument_Locked_By(String document_Locked_By) {
        this.document_Locked_By = document_Locked_By;
    }






}

这是新的DMS_Document_Class

package com.dms.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="DMS_Document_Class")
public class DMS_DocumentClass {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="Document_Class_ID")
    private long ID;


    @Column(name="Document_ID")
    private long document_ID;

    @Column(name="Document_Class")
    private String document_Class;

    @Column(name="Document_Priority")
    private int document_Priority;

    public long getID() {
        return ID;
    }

    public void setID(long iD) {
        ID = iD;
    }

    public long getDocument_ID() {
        return document_ID;
    }

    public void setDocument_ID(long document_ID) {
        this.document_ID = document_ID;
    }

    public String getDocument_Class() {
        return document_Class;
    }

    public void setDocument_Class(String document_Class) {
        this.document_Class = document_Class;
    }

    public int getDocument_Priority() {
        return document_Priority;
    }

    public void setDocument_Priority(int document_Priority) {
        this.document_Priority = document_Priority;
    }

}

请查找附带的context.xml。我在其中提到了带注释的类。我现在无法理解问题在哪里。

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
<?xml version="1.0" encoding="UTF-8"?>  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  

    <context:property-placeholder location="classpath:resources/database.properties" />
    <context:component-scan base-package="com.dms" />
    <context:annotation-config/>

    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>



    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>

    <bean id="springDataSource" 
        class="org.apache.commons.dbcp.BasicDataSource">
       <property name="url" value="${database.url}" />
       <property name="driverClassName" value="${database.driver}" />
       <property name="username" value="${database.user}" />
       <property name="password" value="${database.password}" />
       <property name="removeAbandoned" value="true"/>
       <property name="initialSize" value="20" />
       <property name="maxActive" value="30" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="springDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.dms.bean.DMS_Document</value>
                <value>com.dms.bean.DMS_User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
            </props>
        </property>




    </bean>

    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
         <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
          <list>
            <ref bean="jacksonMessageConverter"/>
          </list>
        </property>
        </bean>

    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

控制器类

package com.dms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dms.bean.DMS_Document;
import com.dms.bean.DMS_DocumentClass;
import com.dms.service.IDMSDocumentService;
import com.dms.validator.DocumentValidator;

@Controller
public class DMSDocumentController {

    @Autowired
    private IDMSDocumentService documentService;

    private static final Logger logger = Logger.getLogger(DMSDocumentController.class);

    @RequestMapping(value="/addDoc",method=RequestMethod.POST)
    public String addDocument(@RequestParam Map<String, String> requestParam, Model model)
    {
        logger.info("add document initiated.");
        DMS_Document newDocument = new DMS_Document();
        newDocument.setCreatedBy("admin");
        newDocument.setDocLocation(requestParam.get("location"));
        newDocument.setDocName(requestParam.get("docName"));
        newDocument.setDocType(requestParam.get("docType"));
        newDocument.setDocumentHidden(false);
        newDocument.setDocumentLocked(false);

        DMS_DocumentClass doc_class1 = new DMS_DocumentClass();
        doc_class1.setDocument_Class("SALARY");
        doc_class1.setDocument_Priority(1);

        DMS_DocumentClass doc_class2 = new DMS_DocumentClass();
        doc_class2.setDocument_Class("SALARY");
        doc_class2.setDocument_Priority(1);
        List<DMS_DocumentClass> document_Classes = new ArrayList<DMS_DocumentClass>();
        document_Classes.add(doc_class1);
        document_Classes.add(doc_class2);

        newDocument.setDocument_Classes(document_Classes);

        DocumentValidator validator = new DocumentValidator();
        validator.validateDocumentAddition(newDocument);

        documentService.addDocument(newDocument);
        logger.info("add document completed.");
        return "Success";
    }



}

1 个答案:

答案 0 :(得分:0)

<property name="annotatedClasses">
            <list>
                <value>com.dms.bean.DMS_Document</value>
                <value>com.dms.bean.DMS_User</value>
            </list>
        </property>

我在xml中添加了错误的映射类