新手开发Hibernate时遇到的错误集锦

   

1.org.hibernate.MappingException: Unknown entity

错误原因:没有在hibernate.cfg.xml文件中加入 *.hbm.xml造成的,例如没有填写<mapping resource="com/netctoss/entity/Cost.hbm.xml"/>

2.org.hibernate.exception.SQLGrammarException: could not initialize a collection

错误原因:数据库和映射文件没有对上,例如在*.hbm.xml文件中配置实体类属性与数据库字段映射时,<property name="osUserName" type="string" column="os_username" />,这里如果把name和column其中一个属性写错了(少一个字母啊、拼写错误啊),都会报上述错误,所以好好检查这里吧

3.org.hibernate.hql.ast.QuerySyntaxException: is not mapped

错误原因:hql语句中from是实体类而不是表名,这个应该大家都知道,注意大小写,例如String hql="from Emp";实体类为Emp,数据库表名为emp

4.This project is a not MyEclipse Hibenate project assuming hibernate3 capabilites for

原因:使用版本控制的时候,工程根目录下的:.project  和 .myhibernatedata文件不在版本控制范围内,因此组内成员更新source后,缺少.project 内的com.genuitec.eclipse.hibernate.hibernatenature 和.myhibernatedata 文件

解决:项目名上右键--〉myeclipse-->add hibernatecapabilites -->next-->hibernate config file --> existing-->选择现有工程存在的hibernate配置文件--> next --> 不生成factory class--> end

5.提供一个二级缓存配置文件的dtd及xml scheme头信息

<?xml version="1.0"encoding="UTF-8"?>
<ehcache  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"></ehcache>

6.hibernate中配置mysql数据库主键生成策略,其他配置请参考(http://blog.sina.com.cn/s/blog_a7b8ab2801014m0e.html)

<!-- 配置主键属性和字段的关系 -->
        <id name="id" type="integer" column="id">
            <!-- 用来指明主键的生成方式 , native可以适应多种数据库 increment mysql自动增长策略 sequence oracle自动增长策略 -->
            <generator class="native"></generator>
        </id>



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值