未知的实体异常

时间:2014-09-10 07:33:04

标签: java spring hibernate spring-mvc hibernate-mapping

我是spring mcv的新手,我创建了一个spring mvc,java和hibernate项目。我使用了spring mvc 4 release。

现在我正在练习如何在休眠中建立一对一的关系。除了mvc-dispatcher-servlet.xml和web.xml之外,只有一个配置xml。 它是hibernate.cfg.xml文件。

这是我写的两个课程。

    package lk.ddarz4u.sales.database;


import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Agency  {
    @Id
    private String agencyId;
    private String agencyName;
    @OneToOne
    private Shipment shipment;




    public Shipment getShipment() {
        return shipment;
    }

    public void setShipment(Shipment shipment) {
        this.shipment = shipment;
    }


     public String getAgencyId() {
        return agencyId;
    }

    public void setAgencyId(String agencyId) {
        this.agencyId = agencyId;
    }

    public String getAgencyName() {
        return agencyName;
    }

    public void setAgencyName(String agencyName) {
        this.agencyName = agencyName;
    }


}

这是发货类

package lk.ddarz4u.sales.database;


import javax.persistence.*;
import java.util.Date;

@Entity
public class Shipment  {
    @Id
    private String shipmentId;
    private int Quantity;
    @Temporal(TemporalType.DATE)
    private Date updateDate;




    public String getShipmentId() {
        return shipmentId;
    }

    public void setShipmentId(String shipmentId) {
        this.shipmentId = shipmentId;
    }

    public int getQuantity() {
        return Quantity;
    }

    public void setQuantity(int quantity) {
        Quantity = quantity;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }


}

我为这两个人编写了一个测试类。

package lk.ddarz4u.sales.database;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import java.util.Date;

public class Test_one_to1 {
    public static void main() {

        Date date=new Date();


        Shipment shipment = new Shipment();
        shipment.setQuantity(45);
        shipment.setShipmentId("s1");
        shipment.setUpdateDate(date);

        Agency agency = new Agency();
        agency.setAgencyId("A1");
        agency.setAgencyName("Colombo");
        agency.setShipment(shipment);


        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.saveOrUpdate(agency);
        session.saveOrUpdate(shipment);
        session.getTransaction().commit();
        session.close();

    }

}

这里是我写的hibernate.cfg.xml文件。

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3307/SalesDelivery</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping class="lk.ddarz4u.sales.database.Agency"></mapping>
        <mapping class="lk.ddarz4u.sales.database.Shipment"></mapping>


    </session-factory>
</hibernate-configuration>

当我运行时,我得到这样的错误。

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.MappingException: Unknown entity: lk.ddarz4u.sales.database.Agency

type Exception report

message Request processing failed; nested exception is org.hibernate.MappingException: Unknown entity: lk.ddarz4u.sales.database.Agency

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.MappingException: Unknown entity: lk.ddarz4u.sales.database.Agency
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
root cause

org.hibernate.MappingException: Unknown entity: lk.ddarz4u.sales.database.Agency
    org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)
    org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1443)
    org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:242)
    org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:511)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:100)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)
    org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:676)
    org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:671)
    lk.ddarz4u.sales.database.Test_one_to1.main(Test_one_to1.java:36)
    lk.ddarz4u.sales.HelloController.manreg(HelloController.java:39)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:439)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:427)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

考虑到作为春季和休眠的初学者,请给我一个解决这个问题的方法。

1 个答案:

答案 0 :(得分:0)

    package lk.ddarz4u.sales.database;


import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
@Table(name = "agency")

public class Agency
{
     @Id
     @Column(name="agencyId")
     private String agencyId;

     @Column(name="agencyName")
     private String agencyName;

     //getter setter method
}

请为装运课程做同样的事情