Reputation: 9808
I'm trying to create a many to many relation between movies and users. When I save a movie I get this error:
2017-12-01 16:12:43.351 WARN 17328 --- [nio-8090-exec-5] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.movieseat.models.Movie]]: java.lang.IllegalArgumentException: Can not handle managed/back reference 'defaultReference': back reference type (java.util.List) not compatible with managed type (com.movieseat.model.security.User)
2017-12-01 16:12:43.354 WARN 17328 --- [nio-8090-exec-5] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.movieseat.models.Movie]]: java.lang.IllegalArgumentException: Can not handle managed/back reference 'defaultReference': back reference type (java.util.List) not compatible with managed type (com.movieseat.model.security.User)
2017-12-01 16:12:43.356 WARN 17328 --- [nio-8090-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
I had an issue:
org.hibernate.collection.internal.PersistentBag[0]->com.movieseat.model.security.User["authorities"]->org.hibernate.collection.internal.PersistentBag[0]->com.movieseat.model.security.Authority["users"]
Which I fixed by: User.java
@JsonManagedReference
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "USER_AUTHORITY",
joinColumns = {@JoinColumn(name = "USER_ID", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "AUTHORITY_ID", referencedColumnName = "ID")})
private List<Authority> authorities;
And Authority.java:
@JsonBackReference
@ManyToMany(mappedBy = "authorities", fetch = FetchType.LAZY)
private List<User> users;
But now it seems that my Movie.java model also needs some kind of annotation. The error states:
Can not handle managed/back reference 'defaultReference': back reference type (java.util.List)
So I've tried adding a @JsonBackReference to the relation in my Movie.java model:
@JsonBackReference
@ManyToMany(mappedBy = "movies")
private Set<User> users = new HashSet<>();
But this doesn't help. Is there something else I'm missing?
Upvotes: 0
Views: 16828
Reputation: 9808
I fixed this by adding
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class User {
to my User.java class
Upvotes: 1