KomachiSion commented on issue #2865: 4.0.0-RC1 jpa select result change some values and save shoud update Row Data rather then insert URL: https://github.com/apache/incubator-shardingsphere/issues/2865#issuecomment-521081656 @DongJigong, I try to reproduce your problem, but failed. I create a sharding table `t_order` with three column. configuration following: ``` spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl spring.jpa.properties.hibernate.show_sql=true spring.shardingsphere.enabled=true spring.shardingsphere.datasource.names=ds0, ds1 spring.shardingsphere.datasource.ds0.type=org.apache.commons.dbcp2.BasicDataSource spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/demo_ds_0 spring.shardingsphere.datasource.ds0.username=root spring.shardingsphere.datasource.ds0.password= spring.shardingsphere.datasource.ds1.type=org.apache.commons.dbcp2.BasicDataSource spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/demo_ds_1 spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.password= spring.shardingsphere.sharding.default-data-source-name=ds0 spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order_$->{0..1} spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.sharding-column=id spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.algorithm-expression=ds$->{id % 2} spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 2} spring.shardingsphere.props.sql.show=true ``` Insert 24 row to `t_order` as init data. Then use `saveAll` to change column `status`: ``` OrderRepo repo = applicationContext.getBean(OrderRepo.class); List<Order> list = new ArrayList<>(); for (int i = 1; i <= 24; i++) { Order order = new Order(); order.setId(i); order.setOrderId(i); order.setStatus("TEST"); list.add(order); } repo.saveAll(list); ``` It can update, not insert new row: ``` Hibernate: select order0_.id as id1_0_0_, order0_.order_id as order_id2_0_0_, order0_.status as status3_0_0_ from t_order order0_ where order0_.id=? 2019-08-14 10:42:21.713 INFO 1581 --- [ main] ShardingSphere-SQL : Rule Type: sharding 2019-08-14 10:42:21.713 INFO 1581 --- [ main] ShardingSphere-SQL : Logic SQL: select order0_.id as id1_0_0_, order0_.order_id as order_id2_0_0_, order0_.status as status3_0_0_ from t_order order0_ where order0_.id=? 2019-08-14 10:42:21.713 INFO 1581 --- [ main] ShardingSphere-SQL : SQLStatement: ShardingSelectOptimizedStatement(tables=Tables(tables=[Table(name=t_order, alias=Optional.of(order0_))], schema=Optional.absent()), groupBy=org.apache.shardingsphere.core.optimize.sharding.segment.select.groupby.GroupBy@12214f2f, orderBy=org.apache.shardingsphere.core.optimize.sharding.segment.select.orderby.OrderBy@756c67cd, selectItems=SelectItems(startIndex=7, stopIndex=96, distinctRow=false, items=[ColumnSelectItem(owner=order0_, name=id, alias=Optional.of(id1_0_0_)), ColumnSelectItem(owner=order0_, name=order_id, alias=Optional.of(order_id2_0_0_)), ColumnSelectItem(owner=order0_, name=status, alias=Optional.of(status3_0_0_))], tables=[TableSegment(startIndex=103, stopIndex=109, name=t_order, quoteCharacter=NONE, owner=Optional.absent(), alias=Optional.of(order0_))]), pagination=org.apache.shardingsphere.core.optimize.sharding.segment.select.pagination.Pagination@50de907a, containsSubquery=false) 2019-08-14 10:42:21.713 INFO 1581 --- [ main] ShardingSphere-SQL : Actual SQL: ds0 ::: select order0_.id as id1_0_0_, order0_.order_id as order_id2_0_0_, order0_.status as status3_0_0_ from t_order_0 order0_ where order0_.id=? ::: [24] 2019-08-14 10:42:21.713 INFO 1581 --- [ main] ShardingSphere-SQL : Actual SQL: ds0 ::: select order0_.id as id1_0_0_, order0_.order_id as order_id2_0_0_, order0_.status as status3_0_0_ from t_order_1 order0_ where order0_.id=? ::: [24] ... Hibernate: update t_order set order_id=?, status=? where id=? 2019-08-14 10:42:22.158 INFO 1581 --- [ main] ShardingSphere-SQL : Rule Type: sharding 2019-08-14 10:42:22.159 INFO 1581 --- [ main] ShardingSphere-SQL : Logic SQL: update t_order set order_id=?, status=? where id=? 2019-08-14 10:42:22.159 INFO 1581 --- [ main] ShardingSphere-SQL : SQLStatement: ShardingConditionOptimizedStatement(tables=Tables(tables=[Table(name=t_order, alias=Optional.absent())], schema=Optional.absent()), shardingConditions=ShardingConditions(conditions=[ShardingCondition(routeValues=[ListRouteValue(columnName=id, tableName=t_order, values=[24])])]), encryptConditions=EncryptConditions(conditions=[])) 2019-08-14 10:42:22.159 INFO 1581 --- [ main] ShardingSphere-SQL : Actual SQL: ds0 ::: update t_order_0 set order_id=?, status=? where id=? ::: [24, TEST, 24] 2019-08-14 10:42:22.162 INFO 1581 --- [ main] ShardingSphere-SQL : Actual SQL: ds0 ::: update t_order_1 set order_id=?, status=? where id=? ::: [24, TEST, 24] ```
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
