Skip to content

Converter<String, …> is applied to derived query #1682

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

Closed
genuss opened this issue Nov 30, 2023 · 3 comments
Closed

Converter<String, …> is applied to derived query #1682

genuss opened this issue Nov 30, 2023 · 3 comments
Labels
status: duplicate A duplicate of another issue

Comments

@genuss
Copy link

genuss commented Nov 30, 2023

Hi, maintainers!
I started my upgrade to spring data jdbc 3.2.0 and found an issue with my setup. I've got a postgresql database and some tables with jsonb type which are converted to simple strings in my entities. To achieve this, I created a pair of Converters. The code is pretty straitforward:

@ReadingConverter
class StringJsonbReadingConverter implements Converter<PGobject, String> {

  @Override
  public String convert(@NonNull PGobject source) {
    if (source.getValue() == null) {
      return null;
    }
    return source.getValue();
  }
}

@WritingConverter
class StringJsonbWritingConverter implements Converter<String, PGobject> {

  @Override
  public PGobject convert(@NonNull String source) {
    var pGobject = new PGobject();
    pGobject.setType("jsonb");

    try {
      pGobject.setValue(source);
    } catch (SQLException e) {
      throw new IllegalStateException(e);
    }

    return pGobject;
  }
}

With them, I could use Strings in my entities both as varchar or jsonb up until 3.1.6 but it broke with 3.2.0. To better illustrate it I created a reproducer here.

I'm not sure whether it's a regression, or I just misused an existing API. Could you help me out here?

@genuss genuss changed the title Possible regression in 3.2.0 Possible regression in conversion in 3.2.0 Nov 30, 2023
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 30, 2023
@mp911de
Copy link
Member

mp911de commented Dec 1, 2023

This is expected behavior and should have had never worked otherwise. By specifying a Converter<String, PGobject>, all String properties are using the converter regardless.

Derived queries have an association to the property that is being queried. You can bypass the converter on the query side by providing a custom query, such as @Query("SELECT * FROM test_entity where text_column = :textColumn").

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Dec 1, 2023
@mp911de mp911de added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 1, 2023
@mp911de mp911de changed the title Possible regression in conversion in 3.2.0 Converter<String, …> is applied to derived query Dec 1, 2023
@schauder
Copy link
Contributor

schauder commented Dec 1, 2023

While this converter setup is indeed "interesting", I think this is actually a duplicate of #1681

@schauder schauder added status: duplicate A duplicate of another issue and removed status: invalid An issue that we don't feel is valid labels Dec 1, 2023
@genuss
Copy link
Author

genuss commented Dec 1, 2023

Indeed my issue is related to #1681, and your fix solves my issue as well.
However, I suppose it's better to stick with a wrapper and and a pair of Converters in order to avoid surprises in future.

record JsonbValue(String value) {}

@ReadingConverter
class JsonbValueReadingConverter implements Converter<PGobject, JsonbValue> {
  @Override
  public JsonbValue convert(@NonNull PGobject source) {
    if (source.getValue() == null) {
      return null;
    }
    return new JsonbValue(source.getValue());
  }
}

@WritingConverter
class JsonbValueWritingConverter implements Converter<JsonbValue, PGobject> {
  @Override
  public PGobject convert(@NonNull JsonbValue source) {
    var pGobject = new PGobject();
    pGobject.setType("jsonb");
    try {
      pGobject.setValue(source.value());
    } catch (SQLException e) {
      throw new IllegalStateException(e);
    }
    return pGobject;
  }
}

This should be more robust and it doesn't require adding stringtype=unspecified to JDBC driver parameters.

Thank you for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants