0

I get this exception when Jackson tries to parse my data to Json:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: packagename.Thing.Stuffs, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->packagename.Stuff[“thing"]->packagename.Thing[“stuffs"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: packagename.Thing.Stuffs, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->packagename.Stuff[“thing"]->packagename.Thing[“stuffs"])

I have the following entities(names have been replaced with Stuff and Thing):

Stuff:

@Entity
@Table(name = "stuff")
@SQLDelete(sql = "UPDATE stuff SET deleted = 1 WHERE id = ?")
@Where(clause = "deleted = 0")
public class Stuff implements Serializable {

    private Long id;
    private Thing thing;
    private String stuffName;

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue
    public Long getId() {
        return this.id;
    }

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, optional = false)
    @JoinColumn(name = "thing_id", nullable = false)
    public Thing getThing() {
        return thing;
    }  

    @Transient
    public String getStuffName() {
        return stuffName;
    }

    // Setters and constructor(s) omitted

}

Thing:

@Entity
@Table(name = "thing")
public class Thing implements Serializable {

    private Long id;
    private String name;
    private List<Stuff> stuffs;


    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "name", unique = false, nullable = false, length = 45)
    public String getName() {
        return this.name;
    }

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "thing_id")
    @JsonIgnore
    public List<Stuffs> getStuffs() {
        return stuffs;
    }


    // Setters and constructor(s) omitted
}

I have been googling for this type of error. It seems that this is happening when the json-parser is trying to parse objects that aren't loaded due to lazy-load. Lazy load seems to be on by default. I'd like to avoid setting everything to eager so I put @JsonIgnore instead. Changes nothing for me. Help would be greatly appreciated, this is driving me nuts.

EDIT:

Adding @JsonIgnore and @Eager in both classes gives me another problem. An exception that is looking like this:

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException Handling of [org.springframework.http.converter.HttpMessageNotWritableException] resulted in Exception java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

Searching for that on stackoverflow gives me this: Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed)

The link is basically saying that I should add @JsonIgnore, and I have already done that.

why_vincent
  • 2,172
  • 9
  • 33
  • 49

2 Answers2

1

From comments you said that you are using Jackson 1.9.10, which will allow you to use the new syntax of @JsonProperty annotation by adding READ_ONLY and WRITE_ONLY property for access type.

So you can use :

@JsonProperty(access = Access.WRITE_ONLY)

With your field definition.

For further details you can check Only using @JsonIgnore during serialization, but not deserialization discussion.

Note:

With older cersions you could just use @JsonIgnore on class member getter and @JsonProperty on its setter.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

Maybe your problem caused by wrong mapping, you shouldn't use @JoinColumn for @OneToMany relation in Thing entity, you need to add mappedBy = "thing" as parameter to specify relationship correctly.