1

I have been struggling with this exception for hours if not days on an Entity that references too many other entities and I have decided to annotate the entity with the LAZY annotation to improve performance gain.

With the LAZY fetch type annotation I am faced with this exception upon getting json data

No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer

Somewhere along the stacktrace, I found this error too

(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class 

I have googled and tried to disable the exception by adding this at the entity and serialize to get json with the below but unfortunately could not fetch json after the attempt

@JsonIgnoreProperties({"hibernateLazyInitializer","userId"})
class Books{
//here is the relationship
@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userId")
    private User userId;

Also, I tried to disable the exception using application.properties, but still the exception still lingers after below configurations

spring.jackson.serialization.eager-serializer-fetch=true
spring.jackson.serialization.fail-on-empty-beans=false
spring.jackson.serialization.fail-on-self-references=false
spring.jackson.serialization.fail-on-unwrapped-type-identifiers=false

Please is there a way I can configure and handle this exception in ApplicationStartUp.java file. Please could someone help me get out of the dark in this

user10445503
  • 165
  • 1
  • 12

1 Answers1

0

It seems that there are many solutions to your problem ... you can check this link to see them...

Normally, I would recomend the DTO approach, but sometimes that solution implies many changes in code ...

As I see in your question, you are following the annotation approach ... if I understood the solutions proposed in the link that I provided, the:

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

Should be put it in your User entity and not in your Book entity! (Try this, but restore your spring configuration before by removing the jackson properties)

Hope this help ...

Carlitos Way
  • 3,279
  • 20
  • 30
  • Remember to check each approach proposed in the link that I provided (specially, the solution of adding hibernate-datatype library to your classpath) – Carlitos Way Jan 11 '19 at 11:07
  • will this approach fetch the user entity in the json as well – user10445503 Jan 11 '19 at 12:48
  • Could not write JSON: could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session – user10445503 Jan 11 '19 at 12:53
  • here is the query I am using >>> return getSession().createQuery("from Users as u... – user10445503 Jan 11 '19 at 12:54
  • Yes, that approach returns the user ... but I though that you were returning a Book with its associated user ... why do you post a users query? – Carlitos Way Jan 11 '19 at 14:46
  • As reference, I have resolved the no session issue by loading what I want in the spring controller or service, this is what I called DTO approach ... however, according to this, https://stackoverflow.com/a/33953018, you only neeed to include that library, and configure your spring app accordingly – Carlitos Way Jan 11 '19 at 14:56
  • upon adding your suggetion, I can fetch books but not user, the userid now returns null in json output – user10445503 Jan 11 '19 at 21:29
  • I am pointing to this >>>> As reference, I have resolved the no session issue by loading what I want – user10445503 Jan 12 '19 at 07:38