Reputation: 53916
I'm a hibernate newbie and im receiving this error when trying to persist an object -
Error -->Unknown entity: org.apache.struts.register.model.Event
In my hibernate.cfg.xml
, I define my mapping as,
<mapping class="org.apache.struts.register.model.Event"/>
My event class -
package org.apache.struts.register.model;
import org.hibernate.annotations.Entity;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;
@Entity
@Table (name="event")
public class Event {
@Id
@GeneratedValue ( strategy = GenerationType.AUTO)
private int eventId;
private String eventName;
private String eventDescription;
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public String getEventDescription() {
return eventDescription;
}
public void setEventDescription(String eventDescription) {
this.eventDescription = eventDescription;
}
public String toString(){
return this.eventName + "," + this.eventDescription;
}
}
EventDAO -
package dao;
import org.apache.struts.register.model.Event;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class EventDAO {
public static boolean registerEvent(Event u) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Transaction t = null;
try {
Session s = sf.openSession();
t = s.beginTransaction(); // start a new transaction
s.persist(u);
t.commit(); // commit transaction
return true;
}
catch(Exception ex) {
System.err.println("Error -->" + ex.getMessage());
if ( t!=null) t.rollback(); // rollback transaction on exception
return false;
}
}
}
System.err.println("Error -->" + ex.getMessage())
, is the line that throws the exception.
Thanks.
Upvotes: 0
Views: 10393
Reputation: 328770
The error means that Hibernate doesn't know about org.apache.struts.register.model.Event
; so either your config file has an error or Hibernate it not loading the file (maybe its in the wrong place or there are two files with the same name or something like that).
Enable logging at the level DEBUG to see which files Hibernate loads and which types it maps.
Also don't use System.err.println("Error -->" + ex.getMessage());
; this hides a lot of useful information. Use ex.printStackTrace()
instead for debugging and log.error("Can't register event "+u, ex)
if you plan to keep the output.
Upvotes: 1