Skip to content

HibernateProxyConverter - wrong serialization in case of abstract class #73

Open
@DonatasC

Description

@DonatasC

Hibernate allows to use entity inheritance, thus this code is valid:
private AbstractEntity myEntity = new ChildEntity();

During runtime object of ChildEntity gets proxied (by HibernateProxy). But HibernateProxyConverter currently outputs just this:

<myEntity>
... fields of ChildEntity ...
</myEntity>

Since AbstractEntity is abstract class upon deserialization I'm getting:

java.lang.InstantiationException: AbstractEntity
    at sun.misc.Unsafe.allocateInstance(Native Method) [rt.jar:1.8.0_102]
    at com.thoughtworks.xstream.converters.reflection.SunLimitedUnsafeReflectionProvider.newInstance(SunLimitedUnsafeReflectionProvider.java:76) [xstream-1.4.7.jar:1.4.7]
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.instantiateNewInstance(AbstractReflectionConverter.java:553) [xstream-1.4.7.jar:1.4.7]

Obviously AbstractReflectionConverter doesn't know what real class must be used and just tries to use the type of field "myEntity".
I have fixed this by modifying HibernateProxyConverter this way:

    public void marshal(final Object object, final HierarchicalStreamWriter writer, final MarshallingContext context) {
        HibernateProxy hibernateProxy = (HibernateProxy) object;
        final Object item = hibernateProxy.getHibernateLazyInitializer().getImplementation();
        // FIX goes here:
        writer.addAttribute("class", Hibernate.getClass(hibernateProxy).getName());
        context.convertAnother(item);
    }

That is, add the attribute "class" with the value of real object class. Now serialization result is:

<myEntity class="ChildEntity">
... fields of ChildEntity ...
</myEntity>

and deserialization works OK.

Could you please correct this issue with HibernateProxyConverter?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions