一,****is not mapped
例如:session.createQuery("from Educate").;
注意这里from后面对应的是类名,而不是数据库表名。
因为Educate类和表educate做了映射,在sql的检索中不是从数据库中取,而是从类中,这也是ORM的思想。
二。ClassCastException
org.hibernate.transaction.TransactionFactoryFactory.buildTransactionFactory(TransactionFactoryFactory.java:37)
参考如下配置即可
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- datasource connection properties -->
<property name="connection.datasource">jdbc/sps</property>
<property name="hibernate.jndi.url">t3://localhost:80</property>
<!-- dialect for MySQL -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name="transaction.manager_lookup_class">
org.hibernate.transaction.WeblogicTransactionManagerLookup
</property>
<mapping resource="Tbluser.hbm.xml" />
</session-factory>
</hibernate-configuration>
三.org.hibernate.LazyInitializationException
failed to lazily initialize a collection of role: springexample.hibernate.Customer.accounts, no session or session was closed
出现这种错误的原因为在one-to-many关系中想要加载one对象的many Set, 比如 Customer和Account
如果我们查询Customer对象时想要把他的account属性的数据也加载进来(即当我们要查看Customer的Account属性的属性时),就会出现这种异常。
解决办法,把Customer.hbm.xml里面Set元素(为account属性)的lazy设置为false(默认为true),或者用下面的方法来查询Customer的数据(同时包含了Account属性的数据)
public Customer findPopulatedGroupByName(final Customer customer) {
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Customer customer = null;
String query = "from Customer customer "
+ "where customer.id =?";
System.out.println(query);
Query queryObject = session.createQuery(query);
long id = 9;
queryObject.setParameter(0, id);
customer = (Customer) queryObject.list().get(0);
customer.getAccounts().size();// force load
return customer;
}
};
return (Customer) getHibernateTemplate().execute(callback);
}
四,Could not bind factory to JNDI
此错误是配置文件的问题,如果hibernate配置文件有session_factory_name这个变量,<session-factory name="foo"> 会试图将一个SessionFactory实例绑定到JNDI中的. 这个选项一般是使用Hibernate Tools时候生成的.
no bean specialed.
出错了,jsp页面报错。
原因是html:select 标签 中 option bean is null/.
Set不能加同一实体
在保存数据的时候循环添加一PO数据到Set,居然最后Set的size()为1。各实体设置不同数据啊,只是没Id.
原因是这个PO以前自己实现了Comparable .只比较Id,
HibernateSystemException: SQL insert,update or delete failed
hibernate级联保存或更新时可能会出这个错误
原因是由于子实体的主键设置了不符合保存更新的数据.
No persister for错误
这个错误说的是no persister for java.lang.Integer;
映射文件未添加肯定不是这个问题了。
那只有映射文件出错。
锁定了关联的id;
PO里面写的关联ID,而建表看不出问题。而且映射文件里面基本上看不出来。
出现No persister for错误后一般是3个情况:
1hbm.xml文件特殊类未制定类型。
2cfg.xml文件未添加类的hbm.xml文件名。
3PO和 hbm类映射不匹配。
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
hibernate最常见错误.
Lazy为true
session关闭后调用延迟加载属性.
在session关闭之前如果调用该属性,会加载.
或者是使用Hibernate.Initalize();
BigDecimal对象属性映射出错
这个问题是不小心造成的.
属性映射应该加上 length="2"指定2位小数,或者几位小数,小数位数在0-19之间
property value with CGLIB 错误
比较常见的hibernate错误了.
当PO属性为原始类型(int,double...)时,数据库纪录为null,调出PO时由于这些属性必须有值而出现错误.
把属性值设置为对象属性解决.
hibernate出错
在configure.buildSessionFactory()时java.nullpointException.
在eclipse里没问题,但部署在tomcat里就出问题了,
是由于包冲突了。
还有个可能就是configure(path),path不对。
是由于eclipse的class loader和tomcat不同,在tomcat里,不能有两个一样一起加载。
eclipse里却是指定jar的。
Caused by: org.dom4j.DocumentException: Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.
如果出现这行错误说明你的xml配置文件有不规范的字符,检查下。
net.sf.hibernate.MappingException: Error reading resource: hibernate/Hello_Bean.hbm.xml
如果出现这行错误说明你的hibernate的XML配置文件有错/r
net.sf.hibernate.MappingException: Resource: hibernate/Hello_Bean.hbm.xml not found
如果出现这行错误说明hibernate的XML配置文件没有找到,你应该把XML文件放在与你的类文件同个目录下,本文中是放在hibernate/classes/hibernate/目录下,也就是跟Hello_Bean.class类文件一起。
net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class hibernate.Hello_Bean
如果出现这行错误说明你的xml文件里设置的字段名name的值与Hello_Bean.java类里的getXXX或setXXX方法不一致。
net.sf.hibernate.HibernateException: JDBC Driver class not found: org.gjt.mm.mysql.Driver
如果出现这行错误说明你的MYSQL驱动没有加进JB库里或者不在CLASSPATH里。
本文针对Hibernate框架使用过程中遇到的各种常见错误进行了解析,并提供了相应的解决方案。涵盖了错误绑定、LazyInitializationException、配置问题、数据映射及类型设定等关键议题。
949

被折叠的 条评论
为什么被折叠?



