简体   繁体   中英

org.hibernate.MappingException: Unknown entity - when I am using a DAL class

I am getting org.hibernate.MappingException: Unknown entity.

Entity Class :

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

}
}

Main Class:

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

}
}

while running the main class iam getting an exception Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.lc.learn.hibernate.sample.dao.EventManager

but the EventManager Class is not an entity class. Please help me to solve this issue

Thanks in advance, Lee

You are saving an EventManager object here

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

You should be saving your Event object

session.saveOrUpdate(eventObj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM