备注:优惠卷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元素属性