【hibernate】Attribute "column" must be declared for element type "property"解决办法

本文解决在使用Hibernate时遇到的错误:在自定义的xxx.hbm.xml文件中,元素属性column未正确声明在property元素上。通过将配置文件从hibernate.cfg.xml头部文件更改为mapping头文件,成功解决了问题。
hibernate,自己写的xxx.hbm.xml文件 出现错误:
Attribute "column" must be declared for element type"property"

这是因为我是复制了
hibernate.cfg.xml的头部文件
<?xmlversion='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
 
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

  这个应该改为mapping的头文件就行了
<?xmlversion="1.0" encoding="UTF-8"?>
<!DOCTYPEhibernate-mapping PUBLIC
         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

`Attribute "restultType" must be declared for element type "select"` 这个错误提示表明在 `select` 元素中使用了 `restultType` 属性,但该属性未被正确声明。很可能是拼写错误,正确的属性名应该是 `resultType` 或者 `resultMap`。以下是解决此问题的具体方法: ### 检查拼写错误 将错误的 `restultType` 改为正确的 `resultType` 或 `resultMap`。`resultType` 用于指定查询结果的基本类型(如 `java.lang.Integer`, `java.util.Map` 等),而 `resultMap` 用于指定自定义的结果映射。 **示例代码**: ```xml <select id="queryByShopId" resultType="com.example.Shop" parameterType="Long"> SELECT s.shop_id, s.shop_name, s.shop_desc, s.shop_addr, s.phone, s.shop_img, s.priority, s.create_time, s.last_edit_time, s.enable_status, s.advice, a.area_id, a.area_name, sc.shop_category_id, sc.shop_category_name FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id AND s.shop_id = #{shopId} </select> ``` ### 使用 `resultMap` 时确保其已定义 如果使用 `resultMap`,需要确保在 XML 文件中已经定义了相应的 `resultMap`。 **示例代码**: ```xml <resultMap id="shopMap" type="com.example.Shop"> <id property="shopId" column="shop_id" /> <result property="shopName" column="shop_name" /> <result property="shopDesc" column="shop_desc" /> <!-- 其他映射 --> </resultMap> <select id="queryByShopId" resultMap="shopMap" parameterType="Long"> SELECT s.shop_id, s.shop_name, s.shop_desc, s.shop_addr, s.phone, s.shop_img, s.priority, s.create_time, s.last_edit_time, s.enable_status, s.advice, a.area_id, a.area_name, sc.shop_category_id, sc.shop_category_name FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id AND s.shop_id = #{shopId} </select> ``` ### 检查 XML 文件的有效性 确保 XML 文件格式正确,没有未闭合的标签或其他语法错误。可以使用 XML 验证工具来检查文件的有效性。 ### 检查命名空间和配置 确保 `select` 元素所在的命名空间和相关配置正确,避免因配置问题导致属性无法识别。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值