Java – exception org. In thread “main” hibernate. Mappingexception: unknown entity:
•
Java
I am using my eclipse ide
log4j:WARN No appenders Could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.hibernate.MappingException: UnkNown entity:info.inetsolv.Emp at org.hibernate.impl.SessionFactoryImpl.getEntityPersister (SessionFactoryImpl.java:628) at org.hibernate.impl.SessionImpl.getEntityPersister (SessionImpl.java:1366) at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:203) at org.hibernate.event.def.AbstractSaveEventListener.getEntityState (AbstractSaveEventListener.java:535) at org.hibernate.event.def.DefaultPersistEventListener.onPersist (DefaultPersistEventListener.java:93) at org.hibernate.event.def.DefaultPersistEventListener.onPersist (DefaultPersistEventListener.java:61) at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:646) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:620) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:624) at info.inetsolv.InsertEmprecord.main(InsertEmprecord.java:22)
POJO CLASS
package info.inetsolv; @SuppressWarnings("serial") public class Emp implements java.io.Serializable { // Fields private Integer eno; private String name; private Double salary; // Constructors /** default constructor */ public Emp() { } /** minimal constructor */ public Emp(Integer eno) { this.eno = eno; } /** full constructor */ public Emp(Integer eno,String name,Double salary) { this.eno = eno; this.name = name; this.salary = salary; } // Property accessors public Integer getEno() { return this.eno; } public void setEno(Integer eno) { this.eno = eno; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Double getSalary() { return this.salary; } public void setSalary(Double salary) { this.salary = salary; }
}
HibernateSessionFactory. java
package info.inetsolv; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; public class HibernateSessionFactory { private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static org.hibernate.SessionFactory sessionFactory; private static Configuration configuration = new Configuration(); private static String CONfig_FILE_LOCATION = "/hibernate.cfg.xml"; private static String configFile = CONfig_FILE_LOCATION; static { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } private HibernateSessionFactory() { } public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get(); if (session == null || !session.isopen()) { if (sessionFactory == null) { rebuildSessionFactory(); } session = (sessionFactory != null) ? sessionFactory.openSession(): null; threadLocal.set(session); } return session; } public static void rebuildSessionFactory() { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } } public static org.hibernate.SessionFactory getSessionFactory() { return sessionFactory; } public static void setConfigFile(String configFile) { HibernateSessionFactory.configFile = configFile; sessionFactory = null; } public static Configuration getConfiguration() { return configuration; }
}
The client program inserts the record into the DB
InsertEmprecord. java
package info.inetsolv; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class InsertEmprecord { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); Session hsession = sf.openSession(); Transaction tx = hsession.beginTransaction(); Emp e = new Emp(); e.setEno(6); e.setName("six"); e.setSalary(1234d); hsession.persist(e); tx.commit(); hsession.close(); sf.close(); } }
Here is my hibernate mapping file EMP hbm. xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="info.inetsolv.Emp" table="EMP" schema="HIB"> <id name="eno" type="java.lang.Integer"> <column name="ENO" precision="5" scale="0" /> <generator class="assigned" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="10" /> </property> <property name="salary" type="java.lang.Double"> <column name="SALARY" precision="10" /> </property> </class> </hibernate-mapping>
Here is my hibernate configuration file
Hibernate cfg. xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.Oracle9Dialect </property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:xe </property> <property name="connection.username">hib</property> <property name="connection.password">abc</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="myeclipse.connection.profile"> my oracle drive </property> <property name="show_sql">true</property> </session-factory> </hibernate-configuration>
Solution
You do not have a mapping configured for the EMP object Configuration file hibernate cfg. XML should contain resource EMP hbm. XML Mapping
<mapping resource="info/inetsolv/Emp.hbm.xml"/>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码