Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in 操作报错 #226

Closed
harry-lianghairui opened this issue Feb 17, 2017 · 2 comments
Closed

in 操作报错 #226

harry-lianghairui opened this issue Feb 17, 2017 · 2 comments

Comments

@harry-lianghairui
Copy link

参数类型是long, 但是doInSharding那里接收到的values是int, for (Long value : values) 这里转换出错

java代码:
List suidList = new ArrayList();

        boolean isExistUid = false;
        if (userBuy.getUid() != null && userBuy.getUid() > 0) {
            isExistUid = true;
        }
        for (GroupStudentVo student : groupStudentList) {
            if (isExistUid) {
                if (student.getSuid().longValue() == userBuy.getUid().longValue()) {
                    suidList.add(student.getSuid());
                }
            } else {
                suidList.add(student.getSuid());
            }
        }
        if (CollectionUtils.isEmpty(suidList)) {
            return "modules/study/usergroup/userAssignToGroup";
        }
        String uids = StringUtils.getUniqueIds(suidList);
        //userBuy.setUids(uids);
        userBuy.setUidList(suidList);

sql代码

	<select id="findInGroupUserPage" resultType="UserBuy">
		SELECT 
			<include refid="userBuyColumns"/>
		FROM user_buy a
		<include refid="userBuyJoins"/>
		<where>
			<if test="goodsIds != null and goodsIds != ''">
				and a.root_obj_id IN (${goodsIds})
			</if>  
<!-- 			<if test="uids != null and uids != ''">
				and a.uid IN (${uids})
			</if> -->
			<if test="uidList != null">
				and a.uid IN
				<foreach collection="uidList" item="uid" open="(" close=")" separator=",">
					${uid}
				</foreach>
			</if>
		</where>
		GROUP BY uid, root_obj_id
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
			</otherwise>
		</choose>
	</select>

报错:

    public Collection<String> doInSharding(final Collection<String> availableTargetNames, final ShardingValue<Long> shardingValue) {
        Collection<String> result = new LinkedHashSet<String>(availableTargetNames.size());
        Collection<Long> values = shardingValue.getValues();
        for (Long value : values) {
            for (String tableNames : availableTargetNames) {
                if (tableNames.endsWith(value % tableCount + "")) {
                    result.add(tableNames);
                }
            }
        }
        return result;
    }
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
	at cn.huanju.edu100.shardingjdbc.SingleKeyModuloTableShardingAlgorithm.doInSharding(SingleKeyModuloTableShardingAlgorithm.java:45)
	at com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingStrategy.doSharding(ShardingStrategy.java:83)
	at com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingStrategy.doStaticSharding(ShardingStrategy.java:59)
	at com.dangdang.ddframe.rdb.sharding.router.single.SingleTableRouter.routeTables(SingleTableRouter.java:128)
	at com.dangdang.ddframe.rdb.sharding.router.single.SingleTableRouter.route(SingleTableRouter.java:96)
	at com.dangdang.ddframe.rdb.sharding.router.SQLRouteEngine.routeSQL(SQLRouteEngine.java:116)
	at com.dangdang.ddframe.rdb.sharding.router.SQLRouteEngine.routeSQL(SQLRouteEngine.java:94)
	at com.dangdang.ddframe.rdb.sharding.router.PreparedSQLRouter.route(PreparedSQLRouter.java:65)
	at com.dangdang.ddframe.rdb.sharding.jdbc.ShardingPreparedStatement.routeSQL(ShardingPreparedStatement.java:157)
	at com.dangdang.ddframe.rdb.sharding.jdbc.ShardingPreparedStatement.executeQuery(ShardingPreparedStatement.java:93)
	at cn.huanju.edu100.hq.common.persistence.interceptor.SQLHelper.getCount(SQLHelper.java:124)
	at cn.huanju.edu100.hq.common.persistence.interceptor.PaginationInterceptor.intercept(PaginationInterceptor.java:60)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:60)
	at com.sun.proxy.$Proxy37.query(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor76.invoke(Unknown Source)
@hanahmily
Copy link
Contributor

sjdbc会根据sql中数值的长度 自动将其识别为int或者long。这里如果可以将泛型声明为Number类型,来保证兼容性。

@leokongwq
Copy link

新版本:PreciseShardingAlgorithm<T extends Comparable<?>>, Number 不满足泛型要求。
建议通过:StandardShardingStrategyConfiguration 配置分区键的类型。转换到泛型参数类型指定的类型。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants