I'm crazy about the error I'm using Spring MVC 3.1.2 and Jackson 2.
I have the following class model:
@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;
When I get one user instance, it is correctly serialized by Jackson. But when I try to get a User instance that contains friends, the following exception is thrown:
org.hibernate.LazyInitializationException: failed to lazily initialize role collection: com.frooid.model.User.friends, session or session closed
I get this instance using one HQL:
select u from User u left join fetch u.friends f where u.id = :id
Thanks everyone!
source share