Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing HibernateProxy objects #91

Closed
mantithetical opened this issue Jan 21, 2015 · 25 comments
Closed

Serializing HibernateProxy objects #91

mantithetical opened this issue Jan 21, 2015 · 25 comments
Assignees

Comments

@mantithetical
Copy link

@mantithetical mantithetical commented Jan 21, 2015

Attempting to serialize HibernateProxy objects throws an error like:

CLASS_NOT_MANAGED JaVers runtime error - given javaClass 'mypackage.myclass_$$_javassist_171'

I have tried to add a HibernateProxySerializer like so:

private static class HibernateProxySerializer extends JsonTypeAdapterTemplate<HibernateProxy> {
    @Inject
    private TypeMapper typeMapper;

    @Override
    public HibernateProxy fromJson(JsonElement json,
            JsonDeserializationContext jsonDeserializationContext) {
        return jsonDeserializationContext.deserialize(json, typeMapper.getDehydratedType(HibernateProxy.class));
    }

    @Override
    public JsonElement toJson(HibernateProxy sourceValue,
            JsonSerializationContext jsonSerializationContext) {
        Object deProxied = sourceValue.getHibernateLazyInitializer().getImplementation();
        return jsonSerializationContext.serialize(deProxied, typeMapper.getDehydratedType(HibernateProxy.class));
    }

    @Override
    public Class getValueType() {
        return HibernateProxy.class;
    }
}

And then registered the serializer like so:

javers = JaversBuilder.javers().registerJaversRepository(mongoRepo)
    .registerValueTypeAdapter(new HibernateProxySerializer()).build();

However, I am still running into the same error.

@bartoszwalacik bartoszwalacik self-assigned this Jan 22, 2015
@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Jan 22, 2015

Hi mantithetical
two things:

  1. Could you please come up with running example which reproduces this error, and push it to github?
  2. Don't think serializing Hibernate proxies is a right way. Proxies are runtime-generated wrappers for domain objects. JaVers should have a feature to skip/ignore proxies

Loading

@mantithetical
Copy link
Author

@mantithetical mantithetical commented Jan 26, 2015

  1. Let me back to you with a compact example
  2. Ignoring proxies is a good start. However, an option to unproxy the proxies before serialization could also be useful.

Loading

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Jan 27, 2015

as far as I remember, unproxing Hibernate proxies is not trivial. Hibernate proxies domain objetcs
and all collections.
We don't want to create dependency to Hibernate in javers-core, so unproxying will be implemented as another javers module or generic approach.

First of all we need runnable example for testing, could you push to github compact project with Hibernate/H2 setup?

Loading

@mantithetical
Copy link
Author

@mantithetical mantithetical commented Jan 29, 2015

Here's an example https://github.com/mantithetical/javers-hibernate-proxy. The call to javers is in PodcastDaoJPA2Impl. Gson allows one to skip classes; perhaps, we could exploit that feature to ignore proxies.

There's another issue with circular references when serializing bi-directional mappings; I will open a separate issue for that. Thanks for your help.

Loading

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Jan 29, 2015

got it, I will start to working on unproxing issue at the weekend

Loading

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Feb 4, 2015

hi, I've prepared some unit test but I can't access your repo to push it, could you give me write access ?

Loading

@mantithetical
Copy link
Author

@mantithetical mantithetical commented Feb 5, 2015

Done.

Loading

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Feb 5, 2015

I've pushed unfinished unit test
https://github.com/mantithetical/javers-hibernate-proxy/blob/master/src/test/java/org/codingpedia/demo/rest/dao/impl/JaversIntegrationTest.java

I'm not in JPA/Hibernate for few yeras, could you help me to configure this test?
I need autowired entityManeger with H2 database

Loading

@mantithetical
Copy link
Author

@mantithetical mantithetical commented Feb 6, 2015

Sure, I will take a stab at it shortly.

Loading

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Feb 15, 2015

hi, any progress in configuring our test?

Loading

@mantithetical
Copy link
Author

@mantithetical mantithetical commented Feb 16, 2015

Sorry about the delay; I was traveling. Let me get back to you.

Loading

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Feb 16, 2015

No hurry , just checking if this issue is still alive. I think, this feature would be valuable.

Loading

@mantithetical
Copy link
Author

@mantithetical mantithetical commented Feb 18, 2015

Done. You should now see the following error when you run that test.

JaversException: ENTITY_INSTANCE_WITH_NULL_ID JaVers runtime error - Found Entity instance of class 'org.codingpedia.demo.rest.entities.Edition_$$_jvstd69_1' with null id

Loading

@bgalek bgalek assigned bgalek and unassigned bartoszwalacik Feb 19, 2015
@mantithetical
Copy link
Author

@mantithetical mantithetical commented Feb 25, 2015

Hey there, any progress on this? Is there anything I could help with?

Loading

@bgalek
Copy link
Contributor

@bgalek bgalek commented Feb 25, 2015

I'm researching this problem right now ;) Need to have one two free evening and I'll make pull request :) thx for beeing ready to help 👍

Loading

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Feb 25, 2015

@bgalek when come up with working version, we can release it as snapshot and get feedback from @mantithetical

Loading

@bgalek
Copy link
Contributor

@bgalek bgalek commented May 8, 2015

to be released at the end of May :)

Loading

@mantithetical
Copy link
Author

@mantithetical mantithetical commented May 8, 2015

Amazing!

Loading

@phelypp
Copy link

@phelypp phelypp commented Jun 3, 2015

Hello Friends,

We really want to use Javers however need this issue
Any position to delivery date?

thank you

Loading

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Jun 3, 2015

@bgalek is working on it, i cant push him :)

Loading

@phelypp
Copy link

@phelypp phelypp commented Jun 16, 2015

Hello Javers :)

You have a definite date?
But we will make an outline in our application.

Thank you

Loading

@bgalek
Copy link
Contributor

@bgalek bgalek commented Jun 16, 2015

Working on it right now, hope to write here about results later today :)

Loading

@bgalek
Copy link
Contributor

@bgalek bgalek commented Jun 16, 2015

I've just updated branch with this feature, hope @bartoszwalacik @pawelszymczyk will check it out soon and maybe we could release ASAP :)

Loading

@phelypp
Copy link

@phelypp phelypp commented Jun 18, 2015

Good, I'm waiting merge

Thanks :)

Loading

@bartoszwalacik
Copy link
Member

@bartoszwalacik bartoszwalacik commented Jul 2, 2015

released in 1.2.11

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants