i'm using spring 4.0.1 , hibernate 4.3.5 ,jackson 1.9.2 and STS IDE
I'm creating a RESTful webservice that returns a data in JSON format
when i use Hibernate code generator it generates getters and setter of associated entities annotated by @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
for the source
and @ManyToOne(fetch = FetchType.LAZY)
for the reference
which causes an infinite recursion during serialization. I tried using Jackson's @JsonIgnore and @JsonBackReference annotations to fix the problem but it seems as if they are being totally ignored and the infinite recursion is still occurring.
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)
This is my entity classes User.class
//i get that suggestion from some sites
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@Entity
@Table(name = "user", catalog = "someSchema")
public class User implements java.io.Serializable {
private String name;
private String password;
private String username;
private Set<Telephone> telephones = new HashSet<Telephone>(0);
@JsonManagedReference
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<Telephone> getTelephones() {
return this.telephones;
}
public void setTelephones(Set<Telephone> telephones) {
this.telephones = telephones;
}
}
Telephone.class
@Entity
@Table(name = "telephone", catalog = "someSchema")
public class Telephone implements java.io.Serializable {
private User user;
private String telephone;
@ManyToOne(fetch = FetchType.LAZY)
//tried @JsonIgnore only and both
@JsonIgnore
//tried @JsonBackReference only and both
@JsonBackReference
@JoinColumn(name = "user_id", nullable = false)
public User getUser() {
return this.user;
}
@JsonIgnore
@JsonBackReference
public void setUser(User user) {
this.user = user;
}
}
concerning registering jackson to my application, i used xml config
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean
class="web.jsonConverters.HibernateAwareObjectMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
and mapper class
public class HibernateAwareObjectMapper extends ObjectMapper {
public HibernateAwareObjectMapper() {
Hibernate4Module hm = new Hibernate4Module();
registerModule(hm);
}
}
Do you have any idea why the Jackson annotations are being ignored?
any help will be appreciated...