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

TypeConverter not used for inserting/updating type Object #79

Closed
lightbody opened this issue Jul 31, 2019 · 11 comments
Closed

TypeConverter not used for inserting/updating type Object #79

lightbody opened this issue Jul 31, 2019 · 11 comments
Assignees
Labels
type: enhancement New feature or request
Milestone

Comments

@lightbody
Copy link
Contributor

TypeConverters are working wonderfully for reading/querying and writing/inserting/updating data except for one case: when you're trying to insert/update and expect a value to be converted before being bound to the statement.

You can see in this code where the problem is:

All other data types have a nice callout to convertRequired except for the Object. This means my TypeConvert doesn't get utilized, resulting in a JDBC error. The specific use case is I'm trying to convert into JSONB type in Postgres. Interestingly, it works on queries because the code doesn't even try to understand the underlying type, it simply passes all ResultSet values off to the type converters:

I'll see if I can get you a patch soon!

@lightbody
Copy link
Contributor Author

Ugh. I tried working on a patch for this, but deep down in Micronaut Core is a check that makes this a non-trivial change:

https://github.com/micronaut-projects/micronaut-core/blob/18c6fb0421c37bd0ace9df593d203da7148f9fc2/core/src/main/java/io/micronaut/core/convert/DefaultConversionService.java#L90

I can't fully figure out why there is that check for Object.class and while I get that it's technically true (a request to convert anything to Object arguably should just be a no-op), that line of code means for sure my TypeConverter that is taking an object and converting it to a PgObject (for Postgres JSONB support) will never get invoked.

@graemerocher
Copy link
Contributor

Thanks, will have a look, in the meantime do you have an example entity with what you are trying to achieve? Thanks

@lightbody
Copy link
Contributor Author

I can provide a sample hello-world app, but it's not 100% self-contained like my other examples because H2 doesn't support JSON types. So if you want to run it, you'd need to set up a Postgres DB.

But I don't think that is necessary. What I'm doing is straightforward: I have an @Entity that has one field that is some random object (could be anything, could also be a Map). And I want it saved as a JSONB field type on the underlying table. Because there isn't native support for JSON types, I'm using @TypeDef(type=OBJECT) and a TypeConverter to convert MyObject to PgObject with the type set to jsonb and the value set to the JSON string representation , which is what the JDBC drivers need in order to insert/update a JSONB field.

@graemerocher
Copy link
Contributor

It is ok if it is not self contained. We have tests that run against progress using test containers

@lightbody
Copy link
Contributor Author

Here you go.

hello-world.zip

The schema file is in src/main/resources/schema.sql. Application simply inserts and queries Book, which has two fields that require type conversion: one that is from the docs (Quantity) that works and one that I'm trying to do (JsonLikeThing) that doesn't work for inserts/updates but actually does work for queries.

@graemerocher
Copy link
Contributor

Thanks, I will take a look soon, probably tomorrow...

@lightbody
Copy link
Contributor Author

Thanks. It's not a huge rush. My first workaround bumped into the issues described in #80, but my workaround to the workaround is to just for the last generated ID. It's an extra query but it works.

@graemerocher graemerocher added enhancement type: enhancement New feature or request and removed enhancement labels Aug 6, 2019
@graemerocher graemerocher self-assigned this Aug 8, 2019
@graemerocher graemerocher added this to the 1.0.0.RC1 milestone Oct 3, 2019
@graemerocher
Copy link
Contributor

I didn't complete the work since it doesn't work in all different databases but for Postgres I did get support for JSONB working. You can see an example here:

https://github.com/micronaut-projects/micronaut-data/blob/master/data-jdbc/src/test/groovy/io/micronaut/data/jdbc/postgres/PostgresJSONBSpec.groovy

Which uses

https://github.com/micronaut-projects/micronaut-data/blob/master/data-tck/src/main/java/io/micronaut/data/tck/entities/Sale.java#L19

@graemerocher
Copy link
Contributor

Closing since we have native JSON support now

@zearic
Copy link

zearic commented Mar 23, 2020

I have the similar issue when use a List of JsonType, e.g.

@TypeDef(type = DataType.JSON)
List<User> users;

Query and Create are ok. but got error when update this field to postgres DB: The column index is out of range
I found a work around: create wrapper for the List, e.g

@TypeDef(type = DataType.JSON)
UserList users;

UserList is just a wrapper:

public class UserList{
   List<User> userList;
}

Hope this can help anyone has the same issue.

@mchmielarz
Copy link

Thanks @zearic for the tip. I still have the issue storing Map in JSONB column and have to use the workaround.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants