Getting underlying value object when using lazy loading

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting underlying value object when using lazy loading

Mark Nolan

Is there a way to get the underlying value object when using lazy loading?

When using lazy loading, the objects returned are javassist proxies. This means that equals won't work (and, by extension, contains). I could change equals to use instanceof rather than getClass, but that opens up other problems: there is a reason why getClass is best practice.

I would rather change equals to check for the class of the underlying value object.

In Hibernate, there is HibernateProxyHelper. Is there anything similar in MyBatis?

Thanks,
Mark.
.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Getting underlying value object when using lazy loading

Iwao AVE!
Hi Mark,

If you know that the object is a proxy, obj.getClass().getSuperclass()
may return the original (=underlying) class.
To check if the object is a proxy:
javassist.util.proxy.ProxyFactory.isProxyClass(obj.getClass()).

Regards,
Iwao
On Wed, Oct 24, 2018 at 7:19 PM Mark Nolan <[hidden email]> wrote:

>
>
> Is there a way to get the underlying value object when using lazy loading?
>
> When using lazy loading, the objects returned are javassist proxies. This means that equals won't work (and, by extension, contains). I could change equals to use instanceof rather than getClass, but that opens up other problems: there is a reason why getClass is best practice.
>
> I would rather change equals to check for the class of the underlying value object.
>
> In Hibernate, there is HibernateProxyHelper. Is there anything similar in MyBatis?
>
> Thanks,
> Mark.
> .
>
> --
> You received this message because you are subscribed to the Google Groups "mybatis-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Getting underlying value object when using lazy loading

Mark Nolan

Sorry for the delay. I finally got a chance to check this. That works as you described, but it still isn't possible to get the "unwrapped" value object. Instead, I have had to change the equals method in a more convoluted way:

if (getClass() != obj.getClass())
return false;

Has become:

// MyBatis uses javassist proxies. Proxied objects should be equal to unproxied objects.
Class<?> testClassThis = getClass();
if (ProxyFactory.isProxyClass(testClassThis)) {
testClassThis = testClassThis.getSuperclass();
}
Class<?> testClassOther = obj.getClass();
if (ProxyFactory.isProxyClass(testClassOther)) {
testClassOther = testClassOther.getSuperclass();
}
if (testClassThis != testClassOther)
return false;

Although rather more verbose, I currently prefer this to the other option, which was to switch to instanceof, in spite of the extra dependency on javassist. I think this is safer in the presence of subclasses. However, I may change my mind ;)

Thanks,
Mark.
.








On Tue, 6 Nov 2018 at 19:30, Iwao AVE! <[hidden email]> wrote:
Hi Mark,

If you know that the object is a proxy, obj.getClass().getSuperclass()
may return the original (=underlying) class.
To check if the object is a proxy:
javassist.util.proxy.ProxyFactory.isProxyClass(obj.getClass()).

Regards,
Iwao
On Wed, Oct 24, 2018 at 7:19 PM Mark Nolan <[hidden email]> wrote:
>
>
> Is there a way to get the underlying value object when using lazy loading?
>
> When using lazy loading, the objects returned are javassist proxies. This means that equals won't work (and, by extension, contains). I could change equals to use instanceof rather than getClass, but that opens up other problems: there is a reason why getClass is best practice.
>
> I would rather change equals to check for the class of the underlying value object.
>
> In Hibernate, there is HibernateProxyHelper. Is there anything similar in MyBatis?
>
> Thanks,
> Mark.
> .
>
> --
> You received this message because you are subscribed to the Google Groups "mybatis-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.