arangodb / spring-data

Spring Data ArangoDB
https://www.arangodb.com/docs/stable/drivers/spring-data-getting-started.html
Apache License 2.0
110 stars 56 forks source link

[DE-805] A way to check if a LazyLoadingProxy entity has been resolved #271

Closed mcso closed 2 months ago

mcso commented 1 year ago

Been playing around with the library, and has come across a situation for which I haven't found a solution for already.

When an object is a LazyLoadingProxy, it doesn't seem like there is a way to check if the entity has already been fetched from the database. Looking at AbstractResolver.ProxyInterceptor, there is a variable resolved which hold this state, but it is currently not exposed. Is there a better way to check if the entity has been resolved, or can this be exposed through the LazyLoadingProxy interface?

This could be useful in situations like mapping with MapStruct (or other similar libaries) where a conditional mapping could be made, so it would only do the mapping if the entity has been resolved. Right now it will access the proxy object, which will trigger resolving the object from the database. Hibernate's LazyInitializer has a similar method (isUninitialized()) for doing this kind of check.

I am using arangodb-spring-data version: 3.8.0.

rashtao commented 1 year ago

Thanks for reporting, we can implement this by exposing the resolved field:

https://github.com/arangodb/spring-data/blob/706a61fcf2311404dc315095e271119cf20d55b0/src/main/java/com/arangodb/springframework/core/convert/resolver/AbstractResolver.java#L112

in LazyLoadingProxy.

mdmm13 commented 7 months ago

We've got the same issue where MapStruct causes tons of non-instantiated proxy objects to be called. This variable would greatly reduce the complexity of the checks.

rashtao commented 2 months ago

released in https://github.com/arangodb/spring-data/releases/tag/v4.2.0