0

I was using jackson-datatype-hibernate5 to avoid lazy field serialize, it work fine. When I add hibernate-enhanced-plugin in my project, which is used to lazy init basic field. And problem comes, as I am using the byte code enhanced, the lazy field is no longer a HibernateProxy instance, as far as I know, it using LazyAttributeLoadingInterceptor to mark and get value of the lazy fields, and the lazy fields are exactly their actual type. Is there any suggestion for my problem? Thank you!

I was trying to let fastjackson not to serialize null field, but it won't help.

1 Answers1

0

You should at least add the mapping of the entities.

I'm assuming you are doing something like:

@Entity
class Example {
   ...

   @Basic(fetch = FetchType.LAZY)
   private String field;
}

If you haven't enabled attribute lazy loading bytecode enhancement (this is the default), this annotation won't have any effect. Hibernate will load the attribute field any time an entity Example is loaded. This means that you will never have a LazyInitializationException for this field.

When you enable bytecode enhancement, Hibernate won't automatically load the field unless you try to access it. This probably happens in your application during serialization.

Because you haven't loaded it while the Hibernate session was still open, when the app tries to serialize it, you see the exception.

If that's the case, you have several options:

  1. Access the field when the session is still open, before serializing it
  2. Avoid the serialization of the field
  3. Don't use attribute lazy loading: I'm not sure about your use case, but make sure that you actually need it before using it. It shouldn't be necessary unless the size of the field is big (if you want to serialize it, it's probably not that big)
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30