首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

【Hibernate总结系列】常见错误总结

2014-01-21 
【Hibernate总结系列】常见异常总结1. org/hibernate/criterion/Criterion工程布署的时候没有加入hibernate3

【Hibernate总结系列】常见异常总结
1. org/hibernate/criterion/Criterion工程布署的时候没有加入hibernate3的jar包,这个jar不是默认加入的,要在myeclipse -> add hibernate capabilities... ->选择copy checked libraries to project folder and add to build-path否则工程布署的时候无论如何是不会有hibernate的包的。2. org.hibernate.id.IdentifierGenerationException当出现org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():异常时,一般是因为<id>元素配置不正确,<id>元素缺少其子元素<generator></generator>的配置引起。解决方案:<id>元素映射了相应数据库表的主键字段,对其子元素<generator class="">,其中class的取值可以为increment、identity、sequence、hilo、native……等,更多的可参考hibernate参考文档,一般取其值为native 。generator class="assigned“而类型type="java.lang.Integer”如果主键时自动增长的Int型,把assigned改成自动增长的Identity,assigned是自动增长的varchar型的。3. org.hibernate.PropertyValueException数据库的表里字段设有not null,执行dao.delete(op); 时出现异常:org.hibernate.PropertyValueException: not-null property references a null or transient value: com.easyfile.util. Operators. operatorid在OperatorsDAO.java中private static Logger logger = Logger.getLogger(AdminAction.class);这句并没有显式地自动加载Id过来,还是需要dao.getSession().load(op,op.getOperatorid());来加载id。完整的代码如下:String operatorid = request.getParameter("delOperatorid");OperatorsDAO dao=new OperatorsDAO();Transaction tran=dao.getSession().beginTransaction();Operators op = new Operators();op.setOperatorid(Integer.parseInt(operatorid));dao.getSession().load(op,op.getOperatorid());dao.delete(op);tran.commit();dao.getSession().close();4. null id in entry (don't flush the Session after an exception occurs)产生该异常信息有2种可能:? 我们没有为数据中的非空字段设置值。如果我们在通过Hibernate增加一条记录的时候我们必须显式的通过setXxx方法为该属性赋值(/默认值)。因为在保存之前Hibernate会检查该非空字段对应的实体属性是否为空。如果不想显式赋值的话,我们可以通过xxx.hbm.xml配置文件来实现,也就是在配置文件中给出该字段的默认值。或者在你的类中设置默认值就行了。注意的是rename数据库保留字段。? 在hibernate的配置文件中,有的元素有unique属性的配置,它在数据添加时并不起任何作用,只在从hbm文件生成ddl语句时才有作用,并不会在运行时校验数据。防止数据重复添加,要么在数据库上建立唯一索引(数据库保证),要么在插入时提前校验(人为保证)。当然,多数情况下是两者结合。5. org.hibernate.hql.ast.QuerySyntaxException? 持久类写错了,要不就是写成了数据库表名? hibernate3.0不支持select中嵌套查询,据说from中也不行,只支持where中嵌套查询,好像3.1支持了select中嵌套? sql语句中字段是用了保留关键字

?

?

热点排行