org.hibernate.MappingException:未知实体:xyz.bean.test

时间:2013-05-02 09:55:56

标签: hibernate

我在hibernate中使用注释.M收到此错误:

org.hibernate.MappingException:未知实体:xyz.bean.test     在org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)     在org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:68)     在org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)     在org.hibernate.impl.SessionImpl.load(SessionImpl.java:795)     在org.hibernate.impl.SessionImpl.load(SessionImpl.java:788)     在sushila.Servlet.testS.processRequest(testS.java:41)     在sushila.Servlet.testS.doGet(testS.java:60)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:734)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:847)     在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)     在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)     在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)     在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)     at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)     在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)     at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)     at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)     在com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)     在com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)     at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)     at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)     在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)     在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)     在com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)     at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)     在com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)     在com.sun.grizzly.ContextTask.run(ContextTask.java:71)     at com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532)     在com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513)     在java.lang.Thread.run(Thread.java:722)

我的代码如下:

test.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xyz.bean;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Id;
import org.hibernate.annotations.Entity;
import javax.persistence.Table;

/**
 *
 * @author Dell10
 */
@Entity
@Table( name = "hrms_people")
public class test implements Serializable{
    @Id
  @Column(name = "person_code")
    public Integer person_code;

    @Column(name="first_name")
    public String first_name;

    @Column(name="last_name")
    public String last_name;

    @Column(name="email")
    public String email;

    @Column(name="permission")
    public String permission;

    @Column(name="create_project")
    public String create_project;

    @Column(name="companyID")
    public Integer companyID;

    @Column(name="person_pwd")
    public String person_pwd;


    public Integer getCompanyID() {
        return companyID;
    }

    public void setCompanyID(Integer companyID) {
        this.companyID = companyID;
    }

    public String getCreate_project() {
        return create_project;
    }

    public void setCreate_project(String create_project) {
        this.create_project = create_project;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String frist_name) {
        this.first_name = frist_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public Integer getPerson_code() {
        return person_code;
    }

    public void setPerson_code(Integer person_code) {
        this.person_code = person_code;
    }

    public String getPerson_pwd() {
        return person_pwd;
    }

    public void setPerson_pwd(String person_pwd) {
        this.person_pwd = person_pwd;
    }
}

testS.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xyz.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import xyz.bean.test;

/**
 *
 * @author Dell10
 */
public class testS extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           //Session ses= new Configuration().configure().buildSessionFactory().openSession();
            Configuration configuration= new AnnotationConfiguration().addAnnotatedClass(test.class).configure();
            Session ses= configuration.buildSessionFactory().openSession();
           test t=new test();

           t=(test)ses.load(test.class, 1);

           out.println("Success " + t.getFirst_name());
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

请帮忙。提前致谢..........

1 个答案:

答案 0 :(得分:1)

失败是因为错误的@Entity注释。

导入后应删除:

import org.hibernate.annotations.Entity;

而这个用来代替:

import javax.persistence.Entity;