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?
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();
}
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With