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

Class cast exception when deleting row from h2 database #807

Closed
aswarcewicz opened this issue Dec 22, 2022 · 2 comments
Closed

Class cast exception when deleting row from h2 database #807

aswarcewicz opened this issue Dec 22, 2022 · 2 comments
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@aswarcewicz
Copy link

spring-r2dbc:5.3.24

When using H2 together with spring-data-r2dbc (1.5.6) there is a class cast error when deleting row from database.
This code fragment:

private static Mono<Integer> sumRowsUpdated(
			Function<Connection, Flux<Result>> resultFunction, Connection it) {
		return resultFunction.apply(it)
				.flatMap(Result::getRowsUpdated)
				.collect(Collectors.summingInt(Integer::intValue));
	}

Is using integers but getRowsUpdated contains Long value

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 22, 2022
@aswarcewicz
Copy link
Author

I have update my project to the Java17 and to all latest dependencies and now it's fine

@q444tita
Copy link

I have update my project to the Java17 and to all latest dependencies and now it's fine

Because in version 3.xx, R2dbcEntityTemplate.doDelete(Query query, Class<?>entityClass) returns Mono<Long>

in version 1.5.6 R2dbcEntityTemplate. doDelete is:

        @Override
	public Mono<Integer> delete(Query query, Class<?> entityClass) throws DataAccessException {

		Assert.notNull(query, "Query must not be null");
		Assert.notNull(entityClass, "Entity class must not be null");

		return doDelete(query, entityClass, getTableName(entityClass));
	}

	Mono<Integer> doDelete(Query query, Class<?> entityClass, SqlIdentifier tableName) {

		StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityClass);

		StatementMapper.DeleteSpec deleteSpec = statementMapper //
				.createDelete(tableName);

		Optional<CriteriaDefinition> criteria = query.getCriteria();
		if (criteria.isPresent()) {
			deleteSpec = criteria.map(deleteSpec::withCriteria).orElse(deleteSpec);
		}

		PreparedOperation<?> operation = statementMapper.getMappedObject(deleteSpec);
		return this.databaseClient.sql(operation).fetch().rowsUpdated().defaultIfEmpty(0);
	}

in version 3.xx, R2dbcEntityTemplate. doDelete is:

	@Override
	public Mono<Long> delete(Query query, Class<?> entityClass) throws DataAccessException {

		Assert.notNull(query, "Query must not be null");
		Assert.notNull(entityClass, "Entity class must not be null");

		return doDelete(query, entityClass, getTableName(entityClass));
	}

	Mono<Long> doDelete(Query query, Class<?> entityClass, SqlIdentifier tableName) {

		StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityClass);

		StatementMapper.DeleteSpec deleteSpec = statementMapper //
				.createDelete(tableName);

		Optional<CriteriaDefinition> criteria = query.getCriteria();
		if (criteria.isPresent()) {
			deleteSpec = criteria.map(deleteSpec::withCriteria).orElse(deleteSpec);
		}

		PreparedOperation<?> operation = statementMapper.getMappedObject(deleteSpec);
		return this.databaseClient.sql(operation).fetch().rowsUpdated().defaultIfEmpty(0L);
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants