How to serialize lazy loaded objects using hibernate jackson module?

I am trying to create an application with angularjs and springmvc.I there are two classes Province and Comunidad.

@Entity(name="Provincia") @Table(name="T_PROVINCIA") public class Provincia implements Serializable{ private String idProvincia; private String nombre; private Comunidad refComunidad; public Provincia() { } @Id @TableGenerator(name="provinciaGen", table="T_GENERATOR", pkColumnName="ID_GENERATOR", pkColumnValue="ID_PROVINCIA", valueColumnName="ID_VALUE") @GeneratedValue(generator="provinciaGen",strategy=GenerationType.TABLE) @Column(name="ID_PROVINCIA") public String getIdProvincia() { return idProvincia; } @Column(name="NOMBRE") public String getNombre() { return nombre; } @ManyToOne(targetEntity=Comunidad.class, fetch=FetchType.LAZY) @JoinColumn(name="ID_COMUNIDAD") public Comunidad getRefComunidad() { return refComunidad; } setters ..... ..... } @Entity(name="Comunidad") @Table(name="T_COMUNIDAD") public class Comunidad implements Serializable{ @Id @TableGenerator(name="comunidadGen", table="T_GENERATOR", pkColumnName="ID_GENERATOR", pkColumnValue="ID_COMUNIDAD", valueColumnName="ID_VALUE") @GeneratedValue(generator="comunidadGen",strategy=GenerationType.TABLE) @Column(name="ID_COMUNIDAD") private String idComunidad; @Column(name="NOMBRE") private String nombre; @Column(name="SHORTNAME") private String shortName; public Comunidad() { } getters and setters ............... } 

In my controller:

 @RequestMapping("/userlist.json") public @ResponseBody List<Provincia> getUserList(){ return this.provinciaService.loadAllProvincias(); } 

And I get this error: / * Serial analyzer not found for class org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer and
there are no properties found to create a BeanSerializer (to avoid an exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) (via the link chain: java.util.ArrayList [0] → admin.domain.Provincia ["refComunidad"] - <admin.domain. Comunidad $$ EnhancerByCGLIB $$ 68ea9e6f ["hibernateLazyInitializer"]) * /

I read about github about the hibernate module of the jackson module - this is a good choice to solve the problem problem: https://github.com/FasterXML/jackson-module-hibernate . I have included the jackson module hibernate dependency in my pom.xml

  <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-hibernate4</artifactId> <version>2.2.0</version> </dependency> 

But I do not know where to configure "Hibernate4Module.Feature.FORCE_LAZY_LOADING, true". I try to follow the directions from this page http://blog.pastelstudios.com/2012/03/12/spring-3-1-hibernate-4-jackson-module-hibernate/

but I get the same error.

Is there anyone who can help me with an easy example?

+7
source share
4 answers

You can try this. It helps me.

 ObjectMapper mapper = new ObjectMapper(); Hibernate4Module hbm = new Hibernate4Module(); hbm.enable(Hibernate4Module.Feature.FORCE_LAZY_LOADING); mapper.registerModule(hbm); ObjectWriter w = mapper.writer(); String result = null; try { result = w.writeValueAsString(o); } catch (JsonProcessingException e) { e.printStackTrace(); } 
+10
source

This is how I work it, I use RestEASY on JBoss WildFly

 @Provider @Produces(MediaType.APPLICATION_JSON) public class JacksonConfig implements ContextResolver<ObjectMapper> { @Override public ObjectMapper getContext(Class<?> type) { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Hibernate4Module()); return mapper; } } 
+3
source

I have an exact problem, and here is my solution.

 public class YourObjectMapper extends ObjectMapper { @PostConstruct public void afterPropertiesSet() throws Exception { getSerializationConfig().set(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); } } 

p / s: valid only for 1.9.x org.codehaus.jackson.map.ObjectMapper , not 2.x Jackson.

0
source

I know a little late, but here is the solution to your problem: Avoid serializing Jackson on laid-back lazy objects

0
source

All Articles