您的位置:首页 > 其它

用Hberinate自动生成实体类的一些异常

2016-06-30 16:24 183 查看
[align=center]用Hberinate自动生成实体类的一些异常[/align]

 1.org.hibernate.InvalidMappingException:
解决方案。是pojo类的属性(变量)与xml配置文件的property不对应,还有xml配置文件抬头

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ;">需要改成<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">。
2.别人遇到的


解决多个实体类同名时,Hibernate会报异常

启动tomcat时报Hibernate异常 
org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/demo/pojo/DepartmentVO.hbm.xml

..

..

..

Caused by: org.hibernate.DuplicateMappingException: duplicate import: DepartmentVO refers to both com.demo.pojo.DepartmentVO and com.test.pojo.DepartmentVO (try using auto-import="false")

 

原因:
通过异常信息,发现项目中的两个类都叫DepartmentVO,只是他们的包路径不同,

如果不做特殊处理,按平时常规方式去配置映射时,就会报该异常。

当我们使用HQL语句查询DepartmentVO时,会用

select d from DepartmentVO d where d.name=:name

如果项目只有一个DepartmentVO类,Hibernate可以自动帮我们找到,

但像上述所说,如果出现有同名类出现时,Hibernate就不知道这个

DepartmentVO具体是指哪下包下的。

解决方案:
第(1)种方法:修改下类名,不要同名

第(2)种方法:

     在某一方中的映射文件DepartmentVO.hbm.xml中的<hibernate-mapping>标记中设置auto-import="false"

     这个配置默认被设置为true,即使用select d from DepartmentVO时会指向

     配置auto-import="true"的那个具体类

     当设置为false后,要查询这个类时就要使用以下这种指定包路径的方式去查询

     select d from com.test.pojo.DepartmentVO d where d.name=:name 

3.
 


错误org.hibernate.InvalidMappingException: Unable
to read XML解决方法

hbm.xml中:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">

在dtd后面有个空格,导致无法解析,将此空格去掉即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: