java - Solution for JsonMappingException: lazily initialize a collection without using @Jsonignore -
i need response json output. shows exception
org.codehaus.jackson.map.jsonmappingexception: failed lazily initialize collection of role: com.sublime.np.entity.user.roles, not initialize proxy - no session (through reference chain: java.util.arraylist[0]->com.sublime.np.entity.question["user"]->com.sublime.np.entity.user["roles"]) @ org.codehaus.jackson.map.jsonmappingexception.wrapwithpath(jsonmappingexception.java:218) @ org.codehaus.jackson.map.jsonmappingexception.wrapwithpath(jsonmappingexception.java:183) @ org.codehaus.jackson.map.ser.std.serializerbase.wrapandthrow(serializerbase.java:140) @ org.codehaus.jackson.map.ser.std.beanserializerbase.serializefields(beanserializerbase.java:158) @ org.codehaus.jackson.map.ser.beanserializer.serialize(beanserializer.java:112) @ org.codehaus.jackson.map.ser.beanpropertywriter.serializeasfield(beanpropertywriter.java:446) @ org.codehaus.jackson.map.ser.std.beanserializerbase.serializefields(beanserializerbase.java:150) @ org.codehaus.jackson.map.ser.beanserializer.serialize(beanserializer.java:112) @ org.codehaus.jackson.map.ser.std.stdcontainerserializers$indexedlistserializer.serializecontents(stdcontainerserializers.java:122) @ org.codehaus.jackson.map.ser.std.stdcontainerserializers$indexedlistserializer.serializecontents(stdcontainerserializers.java:71) @ org.codehaus.jackson.map.ser.std.asarrayserializerbase.serialize(asarrayserializerbase.java:86) @ org.codehaus.jackson.map.ser.stdserializerprovider._serializevalue(stdserializerprovider.java:610) @ org.codehaus.jackson.map.ser.stdserializerprovider.serializevalue(stdserializerprovider.java:256) @ org.codehaus.jackson.map.objectmapper.writevalue(objectmapper.java:1613) @ org.springframework.http.converter.json.mappingjacksonhttpmessageconverter.writeinternal(mappingjacksonhttpmessageconverter.java:209) @ org.springframework.http.converter.abstracthttpmessageconverter.write(abstracthttpmessageconverter.java:208) @ org.springframework.web.servlet.mvc.method.annotation.abstractmessageconvertermethodprocessor.writewithmessageconverters(abstractmessageconvertermethodprocessor.java:143) @ org.springframework.web.servlet.mvc.method.annotation.abstractmessageconvertermethodprocessor.writewithmessageconverters(abstractmessageconvertermethodprocessor.java:89) @ org.springframework.web.servlet.mvc.method.annotation.requestresponsebodymethodprocessor.handlereturnvalue(requestresponsebodymethodprocessor.java:193) @ org.springframework.web.method.support.handlermethodreturnvaluehandlercomposite.handlereturnvalue(handlermethodreturnvaluehandlercomposite.java:71) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:122) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlemethod(requestmappinghandleradapter.java:749) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:689) @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:83) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:938) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:870) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:961)
i used many relationship among entities. qustion entity looks like,
@entity public class question { @id @generatedvalue private integer id; @size(min=5, message = "title must @ least 5 characters !" ) private string title; @size(min=20, message = "description must @ least 20 characters !" ) private string description; private int upvote; private int downvote; @column(name = "published_date") private date publisheddate; @manytoone @joincolumn(name="user_id") private user user; @onetomany(mappedby="question", cascade=cascadetype.remove) private list<answer> answers; @onetomany(mappedby="question", cascade=cascadetype.remove) private list<comment> comments; @manytoone @joincolumn(name="tag_id") private tag tag;
when use @jsonignore annotation, works. json field in relationship disappear.
@onetomany(mappedby="question", cascade=cascadetype.remove) @jsonignore private list<answer> answers; @onetomany(mappedby="question", cascade=cascadetype.remove) @jsonignore private list<comment> comments; @manytoone @joincolumn(name="tag_id") @jsonignore private tag tag;
json after using @jsonignore,
[ { "id": 1, "title": "abc", "description": "abc", "upvote": 0, "downvote": 0, "publisheddate": 1433436068000 }, { "id": 2, "title": "sql exception", "description": "abc", "upvote": 0, "downvote": 0, "publisheddate": 1433436068000 } ]
my controller,
@requestmapping(value = "getquestion", method = requestmethod.get) public @responsebody list<question> getquestion() { list<question> questions = questionservice.getall(); return questions; }
so, need json fields of entity. how can ? idea ?
if you're using spring, opensessioninviewfilter http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/hibernate3/support/opensessioninviewfilter.html or can make @onetomany fetch eager https://docs.oracle.com/javaee/6/api/javax/persistence/onetomany.html
more reading: http://howtodoinjava.com/2014/09/26/lazy-loading-in-hibernate/
Comments
Post a Comment