SQiShER/java-object-diff

Exclude the properties market as _metadata_

itsravi77 opened this issue · 7 comments

How can I exclude the properties marked as metadata? I keep getting the exception with message "java.lang.UnsupportedOperationException: org.hibernate.SessionFactory.getAllClassMetadata is no longer supported" on propertyname "allClassMetadata" and source is org.hibernate.internal.SessionFactoryImpl.

In fact ideally I would want to exclude all the fields from SessionFactoryImpl class. I have tried many ways but fails to achieve the result.

Here are some examples about exclusion, that should point you in the right direction. The one in line 48 seems like the most promising one for your scenario.

However, I'm not sure you really want to diff the SessionFactory. Maybe it would be better just to ignore the entire type.

Thanks Daniel. Yes I would like to ignore whole of SessionFactory object. I did try to do it by setting the exclusion type to SessionFactoryImpl.class but it didn’t work.

It has been ages since I've used Hibernate, so I'm just guessing here, but could it be that the SessionFactory is not an instance of SessionFactoryImpl but of SessionFactoryDelegatingImpl?

It’s other way. SessionFactoryImpl is implementation of SessionFactory. I noticed in your example “objectDifferBuilder.inclusion().exclude().type(ArrayList)”, Do we need to say ArrayList.class?

Hi Daniel, I fixed the issue by adding following exclusions:

ObjectDiffer objectDiffer = ObjectDifferBuilder.startBuilding() .inclusion().exclude().propertyName("handler").and() .inclusion().exclude().propertyName("hibernateLazyInitializer").and() .build();
But interestingly, now I don't see it throwing exception if I remove those exclusions, nothing has changed as well. And I see these log entries:

2019-01-25 18:13:57 [http-nio-0.0.0.0-8899-exec-1] WARN d.d.d.c.CircularReferenceService - Detected circular reference in node at path /country/handler/session/delegate. Going deeper would cause an infinite loop, so I'll stop looking at this instance along the current path. 2019-01-25 18:13:57 [http-nio-0.0.0.0-8899-exec-1] WARN d.d.d.c.CircularReferenceService - Detected circular reference in node at path /country/handler/session/session. Going deeper would cause an infinite loop, so I'll stop looking at this instance along the current path. 2019-01-25 18:13:57 [http-nio-0.0.0.0-8899-exec-1] WARN d.d.d.c.CircularReferenceService - Detected circular reference in node at path /country/hibernateLazyInitializer/session/delegate. Going deeper would cause an infinite loop, so I'll stop looking at this instance along the current path. 2019-01-25 18:13:57 [http-nio-0.0.0.0-8899-exec-1] WARN d.d.d.c.CircularReferenceService - Detected circular reference in node at path /country/hibernateLazyInitializer/session/session. Going deeper would cause an infinite loop, so I'll stop looking at this instance along the current path.

Any reason what has changed?

Glad it works now. It's hard to tell what has changed. Didn't Hibernate have some kind of mechanism to attach and detach objects from it? Maybe your object is in detached state and wasn't before? It's really hard to say without seeing your code. And even then it might be some strange little detail. Looking at the reported circular references I'm kinda worried, your object might not be the best target for the object differ. Especially hibernateLazyInitializer and session look like properties that just should not be part of the comparison. Maybe you can make it easier on yourself if you wrap your class in a facade that just exposes the important parts of your data.