I'm getting crazy with an error that I'm having using Spring MVC 3.1.2 and Jackson 2.
I have the following model Class:
@Entity
@Table(name = "USER")
@JsonIgnoreProperties(ignoreUnknown=true)
public class User implements Serializable
{
@Id
@SequenceGenerator(name = "USER_ID", sequenceName = "USER_ID_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_ID")
private Long id;
@Column(length = 50, nullable = false)
private String firstName;
@Column(length = 50, nullable = false)
private String lastName;
@ManyToMany
@JoinTable(name = "FRIENDS",
joinColumns = @JoinColumn(name = "personId"),
inverseJoinColumns = @JoinColumn(name = "friendId")
)
@JsonManagedReference
private List<User> friends;
@ManyToMany
@JoinTable(name="FRIENDS",
joinColumns=@JoinColumn(name="friendId"),
inverseJoinColumns=@JoinColumn(name="personId")
)
@JsonIgnore
private List<User> friendOf;
// Other attributes and methods...
}
When I get an single instance of User it is correctly serialized by Jackson. But when I try to get an instance of User that contains friends, the following exception is thrown:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.frooid.model.User.friends, no session or session was closed
I'm getting this instance using a single HQL:
select u from User u left join fetch u.friends f where u.id = :id
Thanks to everybody!