0

我得到 org.hibernate.MappingException: Unknown entity。

实体类:

public class Event {
private long eventId;
private String eventTitle;
private Date eventDate;

public Event() {
}


/**
 * @return the eventId
 */
public long getEventId() {
    return eventId;
}

/**
 * @param eventId the eventId to set
 */
public void setEventId(long eventId) {
    this.eventId = eventId;
}

/**
 * @return the eventTitle
 */
public String getEventTitle() {
    return eventTitle;
}

/**
 * @param eventTitle the eventTitle to set
 */
public void setEventTitle(String eventTitle) {
    this.eventTitle = eventTitle;
}

/**
 * @return the eventDate
 */
public Date getEventDate() {
    return eventDate;
}

/**
 * @param eventDate the eventDate to set
 */
public void setEventDate(Date eventDate) {
    this.eventDate = eventDate;
}


}

达尔:

public class EventManager {

public void createAndStoreEvent(String title, Date theDate) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();        
    session.beginTransaction();
    Event eventObj = new Event();
    eventObj.setEventTitle(title);
    eventObj.setEventDate(theDate);
    session.saveOrUpdate(this);
    session.getTransaction().commit();

}
}

主类:

public class MyHibernateSample {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    EventManager eManager=new EventManager();
    eManager.createAndStoreEvent("My Test Event 1", new Date());

}
}

在运行主类时,我在线程“主”org.hibernate.MappingException 中遇到异常异常:未知实体:com.lc.learn.hibernate.sample.dao.EventManager

但 EventManager 类不是实体类。请帮我解决这个问题

提前谢谢,李

4

1 回答 1

2

你在这里保存一个EventManager对象

session.saveOrUpdate(this); // where this refers to the current EventManager object

你应该保存你的Event对象

session.saveOrUpdate(eventObj);
于 2013-11-02T22:16:46.953 回答