I\'m working with RestEasy, Jboss 7 and EJB 3.1. I\'m creating a RESTful web service that returns data in JSON format.
The problem is that I have a @ManyToOne<
Look, I have something similar to you and is working just fine for me...
@JsonBackReference("promotion-travel")
@ManyToOne(cascade = CascadeType.ALL)
@JoinTable(name="PROMOTION_TRAVEL_REL",
joinColumns = @JoinColumn(name="TRAVEL_ID"),
inverseJoinColumns = @JoinColumn(name="PROMOTION_ID")
)
public Promotion getPromotion(){...
And this is what I have on Entity1
@JsonManagedReference("promotion-travel")
@OneToMany(mappedBy="promotion")
public List<Travel> getTravelList(){...
I don't know if moving the annotation to the top will change anything, but that's the only thing that I can see...
Cheers,
use import com.fasterxml.jackson.annotation.JsonBackReference;
instead of import org.codehaus.jackson.annotate.JsonBackReference;
.
It was the problem for me.
I had this problem and got the same code to work by updating the version of the Jackson library in my build (pom.xml) from 1.8.x to 1.9.13. If you are using maven, edit your pom.xml to contain:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
Documentation doesn't help, but it seems that back references for Sets were not supported in the 1.8.x versions.
i'm using spring 4.0.1 , hibernate 4.3.5 ,jackson 1.9.2 and STS IDE
i had the same exception but solved by annotating the setter by @Transient
idont know why but it works fine
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);
@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)
@JoinColumn(name = "user_id", nullable = false)
public User getUser() {
return this.user;
}
@Transient
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);
}
}
The problem here seems to be related to using Set<User>
instead of List<User>
. I had exactly the same problem and changing from Set<User>
to List<User>
fixed this, otherwise I always got Infinite recursion
error from Jackson. I don't know if this is really a bug in Jackson or do you have to provide some other annotations etc. when using Set
.
I had a similar problem where my getter function was not named based on the field name, so the json parsing was still happening for the getter. Changing the getter name solved the issue.