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

Jackson2DatatypeHelper should register Jackson's Hibernate 5 module automatically if present [DATAREST-607] #980

Closed
spring-projects-issues opened this issue Jul 4, 2015 · 4 comments
Assignees
Labels
type: enhancement

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Jul 4, 2015

Petar Tahchiev opened DATAREST-607 and commented

I saw that jackson-datatype-hibernate has included a hibernate5 module:

https://github.com/FasterXML/jackson-datatype-hibernate

However, I do have the jackson-datatype-hibernate in my classpath (using hibernate5), but when I try to access the https://localhost:8112/storefront/rest/variant/564503594854080/catalogVersion

I get this error:

error=Internal Server Error
exception=org.springframework.http.converter.HttpMessageNotWritableException
message=Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.data.rest.webmvc.json.["content"]->com.nemesis.platform.core.model.catalog.CatalogVersionModel_$$_jvst6b4_96["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.data.rest.webmvc.json.["content"]->com.nemesis.platform.core.model.catalog.CatalogVersionModel_$$_jvst6b4_96["handler"])
trace=org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.data.rest.webmvc.json.["content"]->com.nemesis.platform.core.model.catalog.CatalogVersionModel_$$_jvst6b4_96["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: 

I believe this is because in Jackson2DatatypeHelper you register the Hibernate4 module and not the Hibernate5 one


Affects: 2.4 M1 (Gosling)

Attachments:

Referenced from: commits 31c894e

2 votes, 3 watchers

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 7, 2016

Rikard Oxenstrand commented

Hibernate 5 is included in [Spring IO Platform release Athens
|http://docs.spring.io/platform/docs/Athens-RC1/reference/htmlsingle/#appendix-dependency-versions] so this needs to fixed soon.

I have included a patch that solved the problem for me. It registers the Hibernate 5 module and also force hibernate lazy lookup in the PersistentEntityResourceAssembler. I think the projector.projectExcerpt(instance) doesn't handle hibernate lazy proxy objects correctly.

The dependencies should also be updated with the Hibernate 5 module. I'm not sure where that should be done so it's not included in the patch:

<dependency>
   <groupId>com.fasterxml.jackson.datatype</groupId>
   <artifactId>jackson-datatype-hibernate5</artifactId>
   <version>2.8.1</version>
</dependency>

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 8, 2016

Oliver Drotbohm commented

What's the reason you don't simply register the Jackson module yourself? In RepositoryRestConfigurer.configureJacksonObjectMapper(…) you can just go ahead and register any module you want to.

There's a couple of other issues I see here:

  • sneaking in a change that's unrelated to what the original ticket asked for is a bad idea. If you have something you'd like to see changed, feel free to file another ticket.
  • even more so it is if that change introduces a dependency to Hibernate in code that clearly must not depend on Hibernate. Be aware that Spring Data REST needs to work with all Spring Data modules

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 8, 2016

Rikard Oxenstrand commented

I have been using data-rest since 2.1.4 and the Jackson2DatatypeHelper have previously registered and configured the jackson-datatype-hibernate module. There have never been any need to register the module myself.

Since hibernate 5 is included in the next release that is planned for next week (mid-September) I'm a bit surprised that you downgraded the priority of the issue.
https://spring.io/blog/2016/07/29/spring-io-platform-athens-rc1

The other issue is also related to lazy-loading in Hibernate 5 and I ran into it after testing my updated version of Jackson2DatatypeHelper. With the Hibernate 5 module registered you don't get the No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer error but you do get an "PersistentEntity must not be null!" error in the PersistentEntityResource class. So it's not enough to simply register the module to get it working.

It was not my intention to sneak in any changes. That is why I stated what class I changed and what. Next time I will explain the changes and what needs to be done in more detail.

I agree that the update in PersistentEntityResourceAssembler is bad and introduces an unwanted dependency. Version 2.1.4 also had the same problem but I'm not sure when and where that problem was fixed. Version 2.2.3 didn't have the problem. However the patch does make Data Rest work with Spring Data and Hibernate 5 so it can be used by them who like me need a temporary solution

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 9, 2016

Oliver Drotbohm commented

It always has registered the module for Hibernate 4. You can register the module for Hibernate 5 yourself. Plus the lack of automatic registration is by no means affecting the core functionality of the library. Hence the prioritization.

If, assuming the Hibernate 5 module is registered, we have an issue with serialization, we need to fix that. However — again — this is not what this ticket is about. So feel free to raise a new issue so that we can act on those things independently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants