我有以下实体,由于某种原因无法映射,并且在运行时得到"org.hibernate.MappingException: Unknown entity“
人类:
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>
有什么建议吗?谢谢!
发布于 2015-12-01 05:45:23
如何获取ServiceRegistry和SessionFactory实例?
我得到了相同的错误,但当我尝试它时,它工作了:
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sessionFactory = new MetadataSources( serviceRegistry ).buildMetadata().buildSessionFactory();
这是我从Hibernate Getting Started Guide得到的
我正在做一个和你的例子类似的例子。我希望这对你有帮助!
发布于 2017-03-31 10:54:16
尝试在您的示例SessionFactory中将e写为:
SessionFactory objSf = new Configuration().configure().buildSessionFactory();
发布于 2018-03-14 03:53:26
你需要@Entity在你的Human类上面,让Hibernate知道它要被映射。您还需要将包路径和类名添加到配置文件中。类似于:< mapping class="abo.Human"/>
https://stackoverflow.com/questions/32797988
复制相似问题