penweizgx opened a new issue, #31443:
URL: https://github.com/apache/shardingsphere/issues/31443
jdk 1.8
spring-boot 2.7
JPA/Hibernate
```
After upgrading shardingsphere-jdbc-core-spring-boot-starter-5.2.1 to the
new version of shardingsphere5.5 in the project, the routing of the sharding
table fails and an exception is reported
Caused by:
org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException:
Table or view 't_opt_log' does not exist.
at
org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.lambda$checkTableExists$4(SimpleTableSegmentBinder.java:151)
at
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions.checkState(ShardingSpherePreconditions.java:41)
at
org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.checkTableExists(SimpleTableSegmentBinder.java:148)
at
org.apache.shardingsphere.infra.binder.segment.from.impl.SimpleTableSegmentBinder.bind(SimpleTableSegmentBinder.java:84)
at
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder.lambda$bind$0(InsertStatementBinder.java:58)
at java.util.Optional.ifPresent(Optional.java:159)
at
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder.bind(InsertStatementBinder.java:58)
at
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder.bind(InsertStatementBinder.java:48)
at
org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bindDMLStatement(SQLBindEngine.java:85)
at
org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bind(SQLBindEngine.java:72)
at
org.apache.shardingsphere.infra.binder.engine.SQLBindEngine.bind(SQLBindEngine.java:63)
at
org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.<init>(ShardingSpherePreparedStatement.java:207)
at
org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.<init>(ShardingSpherePreparedStatement.java:172)
at
org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection.prepareStatement(ShardingSphereConnection.java:94)
at
com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:337)
at
com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)
at
org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:90)
at
org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:176)
at
org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:81)
at
org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.buildBatchStatement(AbstractBatchImpl.java:142)
at
org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.getBatchStatement(AbstractBatchImpl.java:131)
at
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3356)
at
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3937)
at
org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:107)
at
org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
at
org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478)
```
"t_opt_log" is a tenant sub-table and is expected to access the
t_opt_log_*** table
The core configuration and related codes are as follows
```
HintManager instance = HintManager.getInstance();
instance.addTableShardingValue("t_opt_log", info.getTenantId());
logRepository.add(ulog);
```
```
public class TenantHintAlgorithm implements HintShardingAlgorithm<Long> {
@Override
public Collection<String> doSharding(Collection<String>
availableTargetNames, HintShardingValue<Long> shardingValue) {
Collection<String> result = new ArrayList<>();
String logicTableName = shardingValue.getLogicTableName();
for (Long value : shardingValue.getValues()) {
String newtable = logicTableName+"_"+value;
result.add(newtable);
}
return result;
}
@Override
public void init(Properties properties) {
log.debug("TenantHintAlgorithm inited");
}
}
```
```
spring:
datasource:
# 驱动类名,这里使用的是ShardingSphere提供的驱动
driver-class-name:
org.apache.shardingsphere.driver.ShardingSphereDriver
# 数据源URL,使用ShardingSphere的JDBC URL格式
url: jdbc:shardingsphere:classpath:sharding-jdbc.yml
```
```
mode:
type: Standalone
repository:
type: JDBC
databaseName: kmpc4
dataSources:
db1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://127.0.0.1:3306/kmpc4
username: root
password: 12345678
# 配置其他数据源
rules:
- !SHARDING
tables: # 数据分片规则配置
t_opt_log:
actualDataNodes: db1.t_opt_log
t_org:
actualDataNodes: db1.t_org
t_stat_report:
actualDataNodes: db1.t_stat_report
defaultTableStrategy:
hint: # Hint 分片策略
shardingAlgorithmName: tenantHintAlgorithm
shardingAlgorithms:
tenantHintAlgorithm:
type: CLASS_BASED # 分片算法类型:CLASS_BASED(自定义类分片算法)
props:
strategy: HINT # STANDARD、COMPLEX、HINT
algorithmClassName: com.kmp.api.utils.TenantHintAlgorithm # 自定义类
- !SINGLE
tables:
- db1.p_account
- db1.p_tenant
- db1.p_task
- db1.t_common_config
defaultDataSource: db1
props:
sql-show: true
```
--
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.
To unsubscribe, e-mail:
[email protected]
For queries about this service, please contact Infrastructure at:
[email protected]