Skip to content
Advertisement

What does Hibernate @Proxy(lazy = false) annotation do?

I was facing two different stack traces (see below) when trying to serialize my ESRBRating object which is a JPA entity. I am using Spring Data JPA. The controller called the service, service called the repository. I was able to resolve the issue by adding @Proxy(lazy = false) on my ESRBRating object.

My main question is what does @Proxy(lazy = false) actually do? Why does it work when adding it? Is this a good solution or will it have side effects such as performance / memory concerns?

For reference, this is my ESRBRating class now.

JavaScript

Stack Trace:

JavaScript

I was also getting this stack trace.

JavaScript

Reference to my original question and issue: JsonMappingException: could not initialize proxy – no Session

Advertisement

Answer

@Proxy(lazy=false) will disable the default lazy loading for a particular entity. This means you always get the initialized entity whenever this entity is being referenced from other entities.

Using this annotation is usually a “code smell”. If I were you, I would remove it and simply load all entity associations using the JOIN FETCH directive.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement