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
fix: Hibernate would insert JSON as STRING #944
Conversation
Hibernate tried by default to insert columns that had been annotated with `@JdbcTypeCode(SqlTypes.JSON)` as a STRING instead of JSON. This change adds a default type registration for JSON that does include the correct type code with the value.
@olavloite how can I store null value to the json column? |
@anton-rynkovyi-exa Sorry, apparently we also need to override and explicitly type NULL values as JSON. See #952 |
@olavloite I am currently using Hibernate 6.4.4 version, along with spanner-hibernate-dialect 3.3.0 version and google-cloud-spanner-jdbc 2.16.1 version. But still facing this error |
@tanushree-bs-exa The StackOverflow question includes the error message, but not the entire stacktrace. Would you mind sharing that here? |
@tanushree-bs-exa The error message is most probably an indication that there is a mismatch between the actual data (the JSON string) and the Java class(es) that you are trying to deserialize it to. Would you mind sharing the relevant classes here as well? Meaning at least the following classes (but potentially more, depending on the further nesting of structured classes in these classes):
|
@olavloite I added annotation @JdbcTypeCode(SqlTypes.JSON) to my column
Below is the model classes of Template, Descriptor and DetailTemplate and its nested structured classes
Also, this is the sample data which i have to store in the DB column - TemplateObj
|
I noticed that in the original example that you posted above, you had this definition for @Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ConfigTemplate {
private String type;
@JdbcTypeCode( SqlTypes.JSON )
private Descriptor descriptor;
@NotEmpty(message = "{configTemplate.properties.required}")
private List<Property> properties;
} The |
@olavloite Yes, have removed the |
I just copy-pasted the example code into a test project and tried the following:
That failed at first, due to lack of public getters and setters in the JSON classes.
|
|
@olavloite any suggestions/workaround how this can be fixed? |
I will try to take another look at this later today. |
@tanushree-bs-exa I've incorporated your schema into one of the sample applications, but there is works as expected. See #1000 Would you mind taking a look at that sample and see if that is consistent with how your application is set up? One thing to consider is that Hibernate requires you to have a JSON library on the classpath for serializing and de-serializing. This sample uses Jackson for that: Line 51 in fdfed5c
If you would not have that, then I would have expected a different error message. If that does not reveal any obvious differences, could you then please share a small runnable project that reproduces the problem, preferably using the Spanner emulator? |
@tanushree-bs-exa Friendly ping. Were you able to solve the problem based on the sample above? Or are you still facing this problem? And if so, would you be able to share a full reproduction case? |
Hibernate tried by default to insert columns that had been annotated with
@JdbcTypeCode(SqlTypes.JSON)
as a STRING instead of JSON. This change adds a default type registration for JSON that does include the correct type code with the value.