8

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?

user2431903
  • 81
  • 1
  • 1
  • 3

4 Answers4

12

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();
}
ilya
  • 121
  • 4
4

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;
    }
}
RoyB
  • 3,104
  • 1
  • 16
  • 37
  • I should have mentioned... Too long ago to retrace. It were probably recent versions when I started using them, early 2015 – RoyB Jul 17 '16 at 16:58
  • aight, I'm not sure but I don't think this works in Jackson 2.x and RESTEasy Jackson 2 provider 3.x. I need someone else to test it as well. – Kevin Thorne Jul 18 '16 at 16:31
0

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.

Lee Chee Kiam
  • 11,450
  • 10
  • 65
  • 87
  • 2
    For Jackson 2.x, use `ObjectMapper MAPPER = new ObjectMapper();` `MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);` That said, neither of these answers seemed to help my similar problem. – Marcus Dec 03 '13 at 22:58
0

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

Community
  • 1
  • 1
r1ckr
  • 5,643
  • 5
  • 20
  • 24