org.hibernate.MappingException:未知实体 - 当我使用DAL类时

时间:2013-11-02 22:15:58

标签: java hibernate

我收到org.hibernate.MappingException:未知实体。

实体类:

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;
}


}

DAL:

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());

}
}

运行主类时iam获取异常线程“main”中的异常org.hibernate.MappingException:未知实体:com.lc.learn.hibernate.sample.dao.EventManager

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

提前致谢, 利

1 个答案:

答案 0 :(得分:2)

您正在此处保存EventManager个对象

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

您应该保存Event对象

session.saveOrUpdate(eventObj);