编程语言
首页 > 编程语言> > java – 线程“main”中的异常org.hibernate.MappingException:未知实体:

java – 线程“main”中的异常org.hibernate.MappingException:未知实体:

作者:互联网

我正在使用myeclipse 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;
}

}

客户端程序将记录插入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();


}

}

以下是我的hibernate映射文件
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>

以下是我的hibernate配置文件

的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>

解决方法:

您没有为对象Emp配置映射.配置文件hibernate.cfg.xml应包含资源Emp.hbm.xml的映射.

<mapping resource="info/inetsolv/Emp.hbm.xml"/>

标签:java,hibernate,hibernate-mapping
来源: https://codeday.me/bug/20190728/1564725.html