4

type Exception report

message Could not write content: failed to lazily initialize a collection of role: edu.waa.classified.dto.User.products, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->edu.waa.classified.dto.User["products"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: edu.waa.classified.dto.User.products, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->edu.waa.classified.dto.User["products"])

description The server encountered an internal error that prevented it from fulfilling this request.

Community
  • 1
  • 1
Anand Godar
  • 129
  • 1
  • 1
  • 5

4 Answers4

8

I just added the @JsonIgnore at the after the @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) annotations.

It worked, but not sure why it worked.

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "WISHLIST", joinColumns = {
@JoinColumn(name = "userId", referencedColumnName = "id") }, inverseJoinColumns = {
@JoinColumn(name = "productId", referencedColumnName = "id") })
@JsonIgnore
private List<Product> products;
Chris
  • 5,109
  • 3
  • 19
  • 40
Anand Godar
  • 129
  • 1
  • 1
  • 5
  • 3
    Previously you application is trying to fetch products list to form the json object after session closed. Now you are telling to Json parser ignore products list don't try to populate while constructing json response. So NP. – Lovababu Padala Oct 20 '15 at 07:27
4

The problem is your FetchType.lazy. When Jackson converts the User Entity, it trys to load the products but the hibernate session is closed.

So adding @JsonIgnore is one way to solve this but when you need the products returned as well, this is no option.

I found this answer which solves the same problem for me and gave me the lazy loaded type: https://stackoverflow.com/a/21760361

The good thing about this solution is, that you can still use the lazy load for products.

Community
  • 1
  • 1
buettner123
  • 404
  • 4
  • 8
1

I guess your controller is out of hibernate session. (no @Transactional). But your json list wants to be filled outside that session. But hibernate filled with proxy instead of real data, and outside hibernate session it cannot load that data when you try to access is. JsonIgnore doesn't ask for that data, so it works. Or if you want that Data in your json object, do fetchtype EAGER. Then hibernate load them immediately. If you want that list only in special cases leave it LAZY, but access it then on service layer in a special method , which has annotation @transactional. Then hibernate is able to fill that list.

Michael Hegner
  • 5,555
  • 9
  • 38
  • 64
1

I also came through the same problem. I just added the below annotation to my Entity class (Just before the entity class). It does the job for sure.

@Entity @Table(name="Country") @Proxy(lazy = false) public class Country {}
halfer
  • 19,824
  • 17
  • 99
  • 186
Ram
  • 3,887
  • 4
  • 27
  • 49