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

Hibernate4Module causes NullPointerException when @OneToMany map is encountered #44

Closed
cowtowncoder opened this issue Sep 5, 2013 · 2 comments
Milestone

Comments

@cowtowncoder
Copy link
Member

(moved from https://jira.codehaus.org/browse/JACKSON-906 reported by Patrick Haas)


Serialization results in a NullPointerException when a Map field is annotated with @OneToMany and the Hibernate4Module is loaded.

Tested with jackson-datatype-hibernate4-2.2.2.jar

@cowtowncoder
Copy link
Member Author

import static org.junit.Assert.*;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.persistence.OneToMany;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;

public class JacksonHibernateModuleTest {

private static final String EXPECTED_JSON = "{\"m\":{\"A\":\"A\"}}";

@Test
public void testMap() throws Exception {
    Y object = new Y();
    object.m.put("A", "A");

    assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
    assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}

@Test
public void testMapWithOneToMany() throws Exception {
    X object = new X();
    object.m.put("A", "A");

    assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
    assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}


private String mapWithHibernateModule(Object object) throws JsonProcessingException {
    return new ObjectMapper().registerModule(new Hibernate4Module()).writeValueAsString(object);
}

private String mapWithoutHibernateModule(Object object) throws JsonProcessingException {
    return new ObjectMapper().writeValueAsString(object);
}

private static final class X {
    @OneToMany
    public final Map<String, String> m = new LinkedHashMap<String, String>();
}

private static final class Y {
    public final Map<String, String> m = new LinkedHashMap<String, String>();
}
}

cowtowncoder added a commit that referenced this issue Sep 5, 2013
cowtowncoder added a commit that referenced this issue Sep 5, 2013
@cowtowncoder
Copy link
Member Author

Fixed for 2.3.0 (and 2.2.4, if that ever gets released).

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

No branches or pull requests

1 participant