Cause: java.lang.IllegalArgumentException: Sharding value must implements Comparable

在使用sharding jdbc 进行 分库分表时 插入语句 遇到如下错误

错误如下

Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: java.lang.IllegalArgumentException: Sharding value must implements Comparable.
### The error may exist in com/style/note/sharding/mapper/OrderGoodsMapper.java (best guess)
### The error may involve com.style.note.sharding.mapper.OrderGoodsMapper.insert-Inline
### The error occurred while setting parameters
### SQL: insert into t_order_goods(user_id,order_sn,goods_name,price,gmt_create,gmt_update) values(?,?,?,?,?,?)
### Cause: java.lang.IllegalArgumentException: Sharding value must implements Comparable.
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:199)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:427)
	... 50 more
Caused by: java.lang.IllegalArgumentException: Sharding value must implements Comparable.
	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:144)
	at org.apache.shardingsphere.sharding.route.engine.condition.engine.InsertClauseShardingConditionEngine.getRouteValue(InsertClauseShardingConditionEngine.java:112)
	at org.apache.shardingsphere.sharding.route.engine.condition.engine.InsertClauseShardingConditionEngine.createShardingCondition(InsertClauseShardingConditionEngine.java:94)
	at 

代码如图

OrderGoods orderGoods = new OrderGoods();
//orderGoods.setUserId((long) i);
orderGoods.setOrderSn(order.getOrderSn());
orderGoods.setGoodsName("AAA");
orderGoods.setPrice(BigDecimal.valueOf(200));
orderGoods.setGmtCreate(instance.getTime());
orderGoods.setGmtUpdate(instance.getTime());
orderGoodsService.addOrderGoods(orderGoods);

注释的代码后面才补上 之前是没有的
因为order_goods表 根据user_id 进行分库 这个值没有设置就会出错了

总结一下这个错误的原因

  • 被用来分片的键不能为空
  • 可能该键为自生成ID 但这个自生成ID没有生效也就是没有生成成功
  • 其他错误原因引起
上一篇:hydra用法


下一篇:数据结构与算法 06 排序_Compareble接口&&冒泡排序