spring-projects / spring-projects/spring-data-jpa

Method parameters validation fails for @Converted collections used as scalars [DATAJPA-1682]

Open
#1,981 2 comments 0 reactions 1 assignee View on GitHub

@gregturn is already working on this.

Since Dec 30, 2020.

type: regression
Dominant language
Java
Stars
3.3k
Forks
1.6k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.