spring-data-jpa
spring-data-jpa copied to clipboard
Method parameters validation fails for @Converted collections used as scalars [DATAJPA-1682]
ahaczewski opened DATAJPA-1682 and commented
One of the uses of Spring Data JPA in my company is to store plain Protocol Buffers messages and in @Entity we have a field of type ByteString, which is Iterable, we also have a custom @Converter created for it, which converts ByteString to Base-64 string.
Here is ByteString declaration (from ByteString JavaDoc):
public abstract class ByteString
extends Object
implements Iterable<Byte>, Serializable
Here is a custom converter that we use:
@Converter
public class ByteStringConverter implements AttributeConverter<ByteString, String> {
@Override
public String convertToDatabaseColumn(ByteString attribute) {
return BaseEncoding.base64().encode(attribute.toByteArray());
}
@Override
public ByteString convertToEntityAttribute(String dbData) {
byte[] decodedBytes = BaseEncoding.base64().decode(dbData);
return ByteString.copyFrom(decodedBytes);
}
}
Here is our field declaration in the entity:
@Column(updatable = false, unique = true)
@Convert(converter = ByteStringConverter.class)
private ByteString signature;
Here is our repository method:
Optional<SignatureEntity> findBySignature(ByteString featuresSignature);
This setup worked perfectly fine in Spring Data JPA 2.1, but now fails with the following exception:
Caused by: java.lang.IllegalStateException: Operator SIMPLE_PROPERTY on signature requires a scalar argument, found class com.google.protobuf.ByteString in method public abstract java.util.Optional com.company.project.SignatureEntityRepository.findBySignature(com.google.protobuf.ByteString).
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.throwExceptionOnArgumentMismatch(PartTreeJpaQuery.java:171)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.validate(PartTreeJpaQuery.java:147)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:90)
... 119 common frames omitted
Affects: 2.2.4 (Moore SR4)
Reference URL: https://github.com/spring-projects/spring-data-jpa/pull/228#issuecomment-585244338
3 votes, 4 watchers
Andreas Klein commented
We are faced with much the same regression issue.
Our persistence model makes ample use of Vlad Mihalcea's com.vladmihalcea.hibernate.type.json.``JsonNodeBinaryType, which maps a com.fasterxml.jackson.databind.JsonNode object onto a Postgres jsonb column. This has been working beautifully so far, but after attempting the update to the latest Spring Data JPA 2.2.4, the new org.springframework.data.jpa.repository.query.PartTreeJpaQuery.validate(PartTree, JpaParameters, String) method is spitting the dummy because JsonNode happens to implement Iterable.
On the whole, the validation of repo method signatures isn't a bad idea, but it should of course not mess with anybody's mappings the way it does now. It'll likely be difficult to achieve both to just work out of the box, but maybe an annotation to explicitly suppress this validation for advanced custom user types and converters could be a way out of the dead end
Jens Schauder commented
For expected scalar values we should check first if the parameter is assignable to the relevant property