Hibernate对象属性可以为空的处理

xiaoxiao2021-02-28  89

前言 现有消费信息表如下: CREATE TABLE `t_customer_consume` ( `id` varchar(50) NOT NULL, `account_id` varchar(50) NOT NULL COMMENT '账户id', `sale_id` varchar(50) NOT NULL COMMENT '销售人员id', `payable_amount` decimal(10,2) NOT NULL COMMENT '本次应付金额', `amounts` decimal(10,2) NOT NULL COMMENT '本次余额支付金额', `arrears` decimal(10,2) NOT NULL COMMENT '本次欠款金额', `coupon_amount` decimal(10,2) NOT NULL COMMENT '本次优惠卷支付金额', `coupon_id` varchar(200) DEFAULT NULL COMMENT '优惠卷id', `other_amount` decimal(10,2) NOT NULL COMMENT '本次剩余支付金额', `other_payment` tinyint(4) NOT NULL COMMENT '本次剩余支付方式1:现金;2:银行卡;', `summary` text COMMENT '备注', `create_time` date NOT NULL, `create_id` varchar(50) NOT NULL COMMENT '操作人id', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='顾客充值记录'

备注:优惠卷id(coupon_id)可以为空 CustomerConsume.java如下:

/** * 顾客消费记录 * */ @SuppressWarnings("serial") public class CustomerConsume implements java.io.Serializable { private String id; //本次使用优惠卷id private CustomerCoupon customerCoupon; public String getId() { return id; } public void setId(String id) { this.id = id; } 其他属性略.... public CustomerCoupon getCustomerCoupon() { return customerCoupon; } public void setCustomerCoupon(CustomerCoupon customerCoupon) { this.customerCoupon = customerCoupon; } }

CustomerConsume.hbm.xml如下:

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.weiran.model.CustomerConsume" table="t_customer_consume" > <id name="id" type="java.lang.String"> <column name="ID" length="50" /> <generator class="uuid.hex" /> </id> <many-to-one name="customerCoupon" class="com.weiran.model.CustomerCoupon" fetch="select"> <column name="coupon_id" length="50" /> </many-to-one> </class> </hibernate-mapping>

后台查询消费记录的代码如下:

@Override public List<CustomerConsume> queryCustomerConsumes(String cid) throws Exception { String hql = " from CustomerConsume t where t.customerAccount.customer.id = ? "; return baseDao.query(hql, cid); } 遇到的问题 查询数据时,没有查询到数据 后台报错日志如下: <WARN> Handler execution resulted in exception: Could not write content: No row with the given identifier exists: [com.weiran.model.CustomerCoupon#] (through reference chain: java.util.ArrayList[0]->com.weiran.model.CustomerConsume["customerCoupon"]->com.weiran.model.CustomerCoupon_$$_jvst51c_1e["customerAccount"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No row with the given identifier exists: [com.weiran.model.CustomerCoupon#] (through reference chain: java.util.ArrayList[0]->com.weiran.model.CustomerConsume["customerCoupon"]->com.weiran.model.CustomerCoupon_$$_jvst51c_1e["customerAccount"]) 怎样解决 修改CustomerConsume.hbm.xml文件中的 <many-to-one name="customerCoupon" class="com.weiran.model.CustomerCoupon" fetch="select"> <column name="coupon_id" length="50" /> </many-to-one>

改为

<many-to-one name="customerCoupon" class="com.weiran.model.CustomerCoupon" fetch="select" not-found="ignore"> <column name="coupon_id" length="50" /> </many-to-one> ***备注:添加not-found="ignore"*** not-found属性说明:(可选 - 默认为 exception ): 指定外键引用的数据不存在时如何处理: ignore 会将数据不存在作为关联到一个空对象(null)处理。

4. 后续 修改后查询正常,可以正常显示。 many-to-one的其他元素属性可以参考: hibernate映射文件many-to-one元素属性

转载请注明原文地址: https://www.6miu.com/read-59074.html

最新回复(0)