通过休眠将类映射到数据库表时,获取'未找到实体类'错误 [英] Geting 'Entity class not found' error while mapping classes to database table by hibernate

查看:97
本文介绍了通过休眠将类映射到数据库表时,获取'未找到实体类'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的pojo class

  package practice041116; 
public class Cls {
public String name;
public int roll;
public Cls(){}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getRoll(){
return roll;
}
public void setRoll(int roll){
this.roll = roll;
}
}

主登录文件

  package practice041116; 
import org.hibernate.Session;
import org.hibernate.Transaction;
导入org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
public class MainClass {
public static void main(String args []){
Configuration cnfg = new Configuration();
cnfg.configure(hibernate.cfg.xml);
SessionFactory fact = cnfg.buildSessionFactory();
会话会话= fact.openSession();
Transaction tx = session.beginTransaction();
Cls s =新的Cls();
s.setName(Taleev);
s.setRoll(23);
session.save(s);
tx.commit();

}
}



配置文件

 <?xml version =1.0encoding =UTF-8 ?> 
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate
配置DTD 3.0 // ENhttp://www.hibernate.org/dtd/hibernate-
配置-3.0.dtd>
< hibernate-configuration>
< session-factory>
< property name =hibernate.connection.driver_class>
com.microsoft.sqlserver.jdbc.SQLServerDriver
< / property>
< property name =hibernate.connection.url>
jdbc:microsoft:sqlserver://171.8.9.1; DatabaseName:test
< / property>
< property name =hibernate.connection.username>
用户名
< / property>
< property name =hibernate.connection.password>
密码
< / property>
< property name =hibernate.dialect>
org.hibernate.dialect.SQLServerDialect
< / property>
< property name =show_sql> true< / property>
< mapping resource =practice041116 / Cls.hbm.xml/>
< / session-factory>
< / hibernate-configuration>

映射文件

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate映射DTD
3.0 // ENhttp://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd >
< hibernate-mapping>
< class name =Clstable =cls>
< id name =idcolumn =idtype =integer/>
< property name =namecolumn =NAMEtype =string/>
< property name =rollcolumn =ROLLtype =integer/>
< / class>
< / hibernate-mapping>

我在我的数据库中有指定的表,但是在执行时它给出的Entity Class找不到异常,我有尝试过类似问题上的各种答案,例如这一个
和其他来源的净额
项目的层次结构和使用的jar文件
先谢谢了,如果我在这个问题上做了一些格式错误,请原谅我,因为这是我发布的第一个问题。
堆栈跟踪异常

 < class name =class =h2_lin>解决方案

Clstable =cls>

你不指定合格的类名,而只是简单的类名。
您应该更换为:

 < class name =practice041116.Clstable =cls> 


Simple pojo class

package practice041116;
public class Cls {
public String name;
public int roll;
public Cls(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
}

The main login file

package practice041116; 
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; 
import org.hibernate.SessionFactory;
public class MainClass {
public static void main(String args[]){
Configuration cnfg=new Configuration();
cnfg.configure("hibernate.cfg.xml");
SessionFactory fact=cnfg.buildSessionFactory();
Session session=fact.openSession();        
Transaction tx=session.beginTransaction();
Cls s=new Cls();
s.setName("Taleev");
s.setRoll(23);
session.save(s);
tx.commit();

} }

The configuration file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate        
Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-    
configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.connection.url">
jdbc:microsoft:sqlserver://171.8.9.1;DatabaseName:test
</property>
<property name="hibernate.connection.username">
username
</property>
<property name="hibernate.connection.password">
password
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="show_sql">true</property>
<mapping resource="practice041116/Cls.hbm.xml" />
</session-factory>
</hibernate-configuration>

Mapping file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 
3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Cls" table="cls">
<id name="id" column="id" type="integer"/>
<property name="name" column="NAME" type="string"/>
<property name="roll" column="ROLL" type="integer"/>
</class>
</hibernate-mapping>

I have the specified table in my database but while execute it gives Entity Class not found Exception, i have tried various answer available on the similar question such as this one and few more from other sources from the net Hierarchy of the project and the jar files used Thanks in advance and forgive me if i have done some format mistake on this question as this is the first question i am posting. Stack trace of Exception

解决方案

In your mapping file :

<class name="Cls" table="cls">

you don't specify qualified class name but just simple class name. You should replace by :

<class name="practice041116.Cls" table="cls">

这篇关于通过休眠将类映射到数据库表时,获取'未找到实体类'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆