Exception in thread "main" org.hibernate.MappingException:
Could not determine type for: java.lang.int, at table: users, for columns:
[org.hibernate.mapping.Column(userId)]
这个是由于写user.hbm.xml中
<id name="userId" type="java.lang.int"> 没有存在的int型
要写成
<id name="userId" type="java.lang.Integer"> 要用Integer型
Exception in thread "main" org.hibernate.PropertyAccessException:
Exception occurred inside getter of entity.User.userId
报如下,异常是因为,在Person对象中.设置的属性类型不一致造成的..
请检查。对应bean中set方法数据类型和hibernate配置文件中定义的类型是否一致。
比如你Person中的属性id 定义的类型为Integer ,
而set方法是int id.这样则会发生这样的错误.改成统一的类型即可..
org.hibernate.InvalidMappingException: Could not parse mapping document from resource entity/Info.hbm.xml
要是出现这个错误,一般来说就是hbm.xml中 没有加包名导致的
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
总结:原因是hibernate的session已经关闭,集合没有被初始化。在hibernate中:hibernate3 默认支持延迟加载(lazy="proxy"我们可以把proxy看作是true),hibernate2 默认立即加载(lazy="false")。
在hibernate3中,所有的实体设置文件(user.hbm.xml)中的lazy属性都被默认设成了true,就是当这个类没有被调用时,延时加载,导致了以上情况的发生,在配置文件中将lzay属性设为false就可以了。
----------------------------------------------------------------
原因 :
<may-to-one>or<one-to-may> 的 lazy 属性默认为 :lazy = "proxy"
解决 :<many-to-one> & <set> 中设置 lazy="false"
Exception in thread "main" org.hibernate.SessionException: Session is closed
如果调用 delete()方法 或者其他方法前 要调用 get()方法 得到对象 那么中间不能关闭连接
如果是通用的 dao类 那么要建立一个 不关闭连接的 get()得到对象的方法
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
Hibernate中使用聚集函数异常代码:
原因:
由于Hibernate3.2.5版本的(select count(*))查询返回的结果是个Long 不能强制转换。
解决方法:
方法1:借用String进行转换:
public int method(arg.........){
Session session=getSession();
Query query=session.createQuery(hql);
---关键----Long lo=(Long)query.uniqueResult();
---关键--- Integer intge=new Integer(String.valueOf(lo));
return intge;
}
方法2:用hashcode()
public int method(arg.........){
Session session=getSession();
Query query=session.createQuery(hql);
return ---关键----query.uniqueResult().hashCode();