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:
- Access the field when the session is still open, before serializing it
- Avoid the serialization of the field
- 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)