我有以下实体,由于某种原因我无法映射,在运行时我得到“org.hibernate.MappingException:未知实体”
人:
package abo;
public class Human {
private int id;
private String name;
public Human()
{
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
映射文件Human.hbm.xml(在同一个包中):
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 16:40:24 26/09/2015 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="abo.Human" table="human">
<id name="id" type="int">
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
</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">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">blabla</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">protein_tracker</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="abo/Human.hbm.xml"/>
</session-factory>
</hibernate-configuration>
有什么建议吗? 谢谢!
答案 0 :(得分:0)
如何获得ServiceRegistry和SessionFactory实例?
我得到了同样的错误,但是当我尝试这个时它有效:
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sessionFactory = new MetadataSources( serviceRegistry ).buildMetadata().buildSessionFactory();
我是从Hibernate Getting Started Guide
得到的我正在研究一个类似于你的例子。我希望这能帮到你!
答案 1 :(得分:0)
尝试将您的示例SessionFactory编写为:
SessionFactory objSf = new Configuration().configure().buildSessionFactory();
答案 2 :(得分:0)
你需要在Human类之上的@Entity让Hibernate知道它的映射。您还需要将包路径和类名添加到配置文件中。就像是: &LT; mapping class =&#34; abo.Human&#34; /&gt;