Shardingsphere: 分表:insert sql加上id分片键(分布式id),去掉则正常?

Created on 15 Nov 2017  ·  5Comments  ·  Source: apache/shardingsphere

Please answer these questions before submitting your issue. Thanks!

Which version of Sharding-Jdbc do you using?

2.0.0.M2

Expected behavior

SQL加上id应该正常

Actual behavior

报错:Caused by: io.shardingjdbc.core.exception.ShardingJdbcException: Parameter null should extends Comparable for sharding value.

Steps to reproduce the behavior

使用mybatis:
1、SQL

INSERT INTO traffic (
id, name, createTime
)
VALUES (
#{id,jdbcType=BIGINT},
#{name,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}
)

2、配置
public String doSharding(final Collection availableTargetNames, final PreciseShardingValue shardingValue) {
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new UnsupportedOperationException();
}

3、运行

Error updating database. Cause: io.shardingjdbc.core.exception.ShardingJdbcException: Parameter null should extends Comparable for sharding value.

The error may involve defaultParameterMap

The error occurred while setting parameters

SQL: INSERT INTO traffic ( id, name, createTime ) VALUES ( ?, ?, ? )

Cause: io.shardingjdbc.core.exception.ShardingJdbcException: Parameter null should extends Comparable for sharding value.

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:200)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)
... 7 more

Caused by: io.shardingjdbc.core.exception.ShardingJdbcException: Parameter null should extends Comparable for sharding value.
at io.shardingjdbc.core.parsing.parser.context.condition.Condition.getValues(Condition.java:100)
at io.shardingjdbc.core.parsing.parser.context.condition.Condition.getShardingValue(Condition.java:83)
at io.shardingjdbc.core.routing.type.simple.SimpleRoutingEngine.getShardingValues(SimpleRoutingEngine.java:108)
at io.shardingjdbc.core.routing.type.simple.SimpleRoutingEngine.getTableShardingValues(SimpleRoutingEngine.java:78)
at io.shardingjdbc.core.routing.type.simple.SimpleRoutingEngine.route(SimpleRoutingEngine.java:62)
at io.shardingjdbc.core.routing.router.ParsingSQLRouter.route(ParsingSQLRouter.java:120)
at io.shardingjdbc.core.routing.router.ParsingSQLRouter.route(ParsingSQLRouter.java:85)
at io.shardingjdbc.core.routing.PreparedStatementRoutingEngine.route(PreparedStatementRoutingEngine.java:57)
at io.shardingjdbc.core.jdbc.core.statement.ShardingPreparedStatement.route(ShardingPreparedStatement.java:150)
at io.shardingjdbc.core.jdbc.core.statement.ShardingPreparedStatement.execute(ShardingPreparedStatement.java:140)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:46)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198)
... 13 more

Please provide the reproduce example codes (such as github link) if possible.

invalid

Most helpful comment

这里的使用是有问题的。既然使用了自增主键,在sql中就不应该在写id,道理与mysql的autoincrement相同。
因此

INSERT INTO traffic (
name, createTime
)
VALUES (
#{name,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}
)

才是正确的

All 5 comments

sql改为这样就正常:INSERT INTO traffic (
name, createTime
)
VALUES (
#{name,jdbcType=VARCHAR},

{createTime,jdbcType=TIMESTAMP}

)

这里的使用是有问题的。既然使用了自增主键,在sql中就不应该在写id,道理与mysql的autoincrement相同。
因此

INSERT INTO traffic (
name, createTime
)
VALUES (
#{name,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}
)

才是正确的

这里确实报错信息不太明确,但是由于配置了自增主键却没传值,,放到mybatis也是不能运行的

因为字段id我是想用这个column-key-generator-class="io.shardingjdbc.core.keygen.DefaultKeyGenerator"生成的分布式id进行设值,所以insert加上id字段应该是没错,只不过是sj帮我设值了,而不是由数据库自增主键。

我也遇到这个问题,主要是使用了mysqlGenerato的生成工具,自动会生成对象所有字段,由于报错信息不明显,查了大半天分片配置问题。 幸好看到这个问题,不然还在查分片配置文件加载过程中可能出的问题。

Was this page helpful?
0 / 5 - 0 ratings