org.hibernate.exception.GenericJDBCException: could not load an entity:

来源:百度知道 编辑:UC知道 时间:2024/09/22 18:23:29
Qjsys29.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="com.qinjing.hibernate.bean.Qjsys29" table="QJSYS29" schema="dbo" catalog="QJDB" lazy="false">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="identity"></generator>
</id>
<many-to-one name="qjsys29f" class="com.qinjing.hibernate.bean.Qjsys12" fetch="select">
<column name="QJS

首先这个问题与主键生成方式没有关系,况且楼主可能使用的是Mysql数据库,因此采用identity生成方式完全正确。
其次关于load取数据出现问题是因为取数据的表有级联关系,在我们分析此问题之前,我们先要明白hibernate中什么是load,什么是get,它们两者之间有什么区别呢??
只要你在以往的开发过程中稍加注意,不难发现,假如有两张表,他们为一对多关系,现在开始查询,如果使用get,那么查询的结果将会是把2张表的数据全部持久化到内存中(如果关系比较深你还打算用它吗?),如果是采用load的方式,那么将只会是把其中一张表(A表)的数据加载到内存中,而另一张表(B表)只存在一个标识,原则上是等页面显示时,如果只要显示A表数据,那么程序直接内存中找A表数据,如果还要显示B表数据,那么就要通过内存中B表的标识重新查找数据库,此时,问题出来了,假如你在DAO中,session.load之后你结束了session的生命周期,那么在页面要显示B表数据,而B表在内存中只是一个标识啊,你的session又处于关闭状态,它无法找啊,这就会出现上面你遇到的那个问题,因此,假如我们不关闭session,页面可以正常显示2张表的数据了,但是问题又来了,如果不关闭session,那么对象将一直以持久态的形式保存在内存中,留下的隐患难以想象(比如该对象容易数据混乱等等),千万不要说session.clear之内的东西也可以把对象变游离态之内的话啊,如果你彻底用session.clear取代session.close的话,嘿嘿,自己去尝试吧,当问题显现出来的时候痛苦吧^_^!好了,我还是说下解决方法吧~最好的方法就是将DAO中的session与事物全部抽取出来做成一个hibernate过滤器,这样既能解决你上面的问题,又能大大减少DAO中的代码...好处很多的哦,使用一次之后就容易上瘾-_-我空间里好象有,你可以去看看~稍微修改就可以用!(最好和工具类一起搭配使用,超爽滴!)

换2005的驱动吧,补充一下,换驱动要修改URL和Driver
org.hibernate.MappingException: org.hibernate.exception.JDBCConnectionException: Cannot open connection org.hibernate.exception.GenericJDBCException: Cannot open connection org.hibernate.tool.ant.HibernateToolTask在那个包下面? org.hibernate.mappingException:Could not read mappings from resource: org.hibernate.HibernateException: Found two representations of same collection org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 异常:ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed... hibernate Hibernate