简体   繁体   中英

Hibernate mapping error - Unknown entity

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.

import org.hibernate.annotations.Entity;

the hibernate @Entity is not enough to get your entity discovered. Use javax.persistence.Entity instead.

See here :

@javax.persistence.Entity is still mandatory, @org.hibernate.annotations.Entity is not a replacement.

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.

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