user2431903
user2431903

Reputation: 81

How to serialize lazy loaded entities with jackson module hibernate?

I`m trying to build an application with angularjs and springmvc.I have 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 that error: /* No serializer found for class org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer and
no properties discovered to create BeanSerializer (to avoid exception, disable
SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[0]->admin.domain.Provincia["refComunidad"]-< admin.domain.Comunidad$$EnhancerByCGLIB$$68ea9e6f["hibernateLazyInitializer"]) */

I have read on github about jackson module hibernate is a good choice to solve the
problem : https://github.com/FasterXML/jackson-module-hibernate. I 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 don't know where configure the "Hibernate4Module.Feature.FORCE_LAZY_LOADING,true". I try to follow the indications from that page http://blog.pastelstudios.com/2012/03/12/spring-3-1-hibernate-4-jackson-module-hibernate/

but I obtain the same error.

Is there somebody who can help me with an easy example please?

Upvotes: 8

Views: 21850

Answers (4)

RoyB
RoyB

Reputation: 3164

This is how I got it working, i'm using 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;
    }
}

Upvotes: 4

r1ckr
r1ckr

Reputation: 5913

I know is a little bit late, but here is the solution for your problem: Avoid Jackson serialization on non fetched lazy objects

Upvotes: 0

Lee Chee Kiam
Lee Chee Kiam

Reputation: 12058

I have the 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: Only valid for 1.9.x org.codehaus.jackson.map.ObjectMapper and not 2.x Jackson.

Upvotes: 0

ilya
ilya

Reputation: 121

You can try this. It helps to 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();
}

Upvotes: 12

Related Questions