首页 > 作文

spring

更新时间:2023-04-03 22:25:13 阅读: 评论:0

目录
spring-data-jpa中findone与getone区别findone与getone的注释中也有些帮助spring data jpa的findone和getone要分清可能会导致httpmessageconversionexception异常

spring-data-jpa中findone与getone区别

项目中用到了spring-data-jpa,今天在写一个update方法的时候报了个空指针,看了看是因为一个对象中的关联没有取出来,我用的是getone取得这个对象,加断点看以一下这个对象是个hibernate的代理对象,而不是实体,里面基本没有值。

然后我又换成findone试了一下,断点查看这个取出来的对象,这个对象是实体,关联也有。

findone与getone的注释中也有些帮助

/**     * retrieves an entity by its id.     *      * @param id must not be {@literal null}.     * @return the entity with the given id or {@literal null} if none found     * @throws illegalargumentexception if {@code id} is {@literal null}     */    t findone(id id); /**     * returns a reference to the entity with the given identifier.     *      * @param id must not be {@literal null}.     * @return a reference to the entity with the given identifier.     * @e entitymanager#getreference(class, object)     */    t getone(id id);

注意getone是“returns a reference“

spring data jpa的findone和getone要分清

可能会导致httpmessageconversionexception异常

getone是返回一个实体的引用——代理对象,findone是返回实体。

springdatajpa1的时候这两个平时用没太大区别,如果升级到2在实体转json的过程中会出错,用debug查看实体其实是空,数据是放在代理对象中的,但jackson将实体转json没有拿到代理对象,然后产生以下错误,这时候json转换失败再导致输出流报错:

org.springframework.http.converter.httpmessageconversionexception: type definition error: [simple type, class org.hibernate.proxy.pojo.javassist.javassistlazyinitializer]; nested exception is com.fasterxml.jackson.databind.exc.invaliddefinitionexception: no rializer found for class org.hibernate.proxy.pojo.javassist.javassistlazyinitializer and no properties discovered to create beanrializer (to avoid exception, disable rializationfeature.fail_on_empty_beans) (through reference chain: com.esnce.framework.common.callbackbody["result"]->com.esnce.framework.sys.model.dictionary_$$_jvstbca_7["handler"])at org.springframework.http.converter.json.abstractjackson2httpmessageconverter.writeinternal(abstractjackson2httpmessageconverter.java:293)at org.springframework.http.converter.abstractgenerichttpmessageconverter.write(abstractgenerichttpmessageconverter.java:102)at org.springframework.web.rvlet.mvc.method.annotation.abstractmessageconvertermethodprocessor.writewithmessageconverters(abstractmessageconvertermethodprocessor.java:272)at org.springframework.web.rvlet.mvc.method.annotation.requestresponbodymethodprocessor.handlereturnvalue(requestresponbodymethodprocessor.java:180)at org.springframework.web.method.support.handlermethodreturnvaluehandlercomposite.handlereturnvalue(handlermethodreturnvaluehandlercomposite.java:82)at org.springframework.web.rvlet.mv上海九星村c.method.annotation.rvletinvocablehandlermethod.invokeandhandle(rvletinvocablehandlermethod.java:119)at org.springframework.web.rvlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:877)at org.springframework.web.rvlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:783)at org.springframework.web.rvlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:87)at org.springframework.web.rvlet.dispatcherrvlet.dodispatch(dispatcherrvlet.java:991)at org.springframework.web.rvlet.dispatcherrvlet.dorvice(dispatcherrvlet.java:925)at org.springframework.web.rvlet.frameworkrvlet.processrequest(frameworkrvlet.java:974)at org.springframework.web.rvlet.frameworkrvlet.doget(frameworkrvlet.java:866)at javax.rvlet.http.httprvlet.rvice(httprvlet.java:635)at org.springframework.web.rvlet.frameworkrvlet.rvice(frameworkrvlet.java:851)at javax.rvlet.http.httprvlet.rvice(httprvlet.java:742)at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:231)at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166)at org.apache.tomcat.websocket.rver.wsfilter.dofilter(wsfilter.java:52)at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193)at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166)at com.urms.urmsfilter.dofilter(urmsfilter.java:199)at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193)at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.j魔蛇之拥ava:166)at org.springframework.web.filter.requestcontextfilter.dofilterinternal(requestcontextfilter.java:99)at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107)at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193)at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166)at org.springframework.web.filter.httpputformcontentfilter.dofilterinternal(httpputformcontentfilter.java:109)at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107)at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193)at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166)at org.springframework.web.filter.hiddenhttpmethodfilter.dofilterinternal(hiddenhttpmethodfilter.java:81)at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107)at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193)at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166)at org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:200)at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107)at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193)at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166)at org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:198)at org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:96)at org.apache.catalina.authenticator.authenticatorba.invoke(authenticatorba.java:496)at普通话怎么练 org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:140)at org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:81)at org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:87)at org.apache.catalina.connector.coyoteadapter.rvice(coyoteadapter.java:342)at org.apache.coyote.http11.http11processor.rvice(http11processor.java:803)at org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:66)at org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:790)at org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1468)at org.apache.tomcat.util.net.socketprocessorba.run(socketprocessorba.java:49)at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142)at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617)at org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61)at java.lang.thread.run(thread.java:745)caud by: com.fasterxml.jackson.databind.exc.invaliddefinitionexception: no rializer found for class org.hibernate.proxy.pojo.javassist.javassistlazyinitializer and no properties discovered to create beanrializer (to avoid exception, disable rializationfeature.fail_on_empty_beans) (through reference chain: com.esnce.framework.common.callbackbody["result"]->com.esnce.framework.sys.model.dictionary_$$_jvstbca_7["handler"])at com.銤fasterxml.jackson.databind.exc.invaliddefinitionexception.from(invaliddefinitionexception.java:77)at com.fasterxml.jackson.databind.rializerprovider.reportbaddefinition(rializerprovider.java:1191)at com.fasterxml.jackson.databind.databindcontext.reportbaddefinition(databindcontext.java:312)at com.fasterxml.jackson.databind.r.impl.unknownrializer.failforempty(unknownrializer.java:71)at com.fasterxml.jackson.databind.r.impl.unknownrializer.rialize(unknownrializer.java:33)at com.fasterxml.jackson.databind.r.beanpropertywriter.rializeasfield(beanpropertywriter.java:727)at com.fasterxml.jackson.databind.r.std.beanrializerba.rializefields(beanrializerba.java:719)at com.fasterxml.jackson.databind.r.beanrializer.rialize(beanrializer.java:155)at com.fasterxml.jackson.databind.r.beanpropertywriter.rializeasfield(beanpropertywriter.java:727)at com.fasterxml.jackson.databind.r.std.beanrializerba.rializefields(beanrializerba.java:719)at com.fasterxml.jackson.databind.r.beanrializer.rialize(beanrializer.java:155)at com.fasterxml.jackson.databind.r.defaultrializerprovider._rialize(defaultrializerprovider.java:480)at com.fasterxml.jackson.databind.r.defaultrializerprovider.rializevalue(defaultrializerprovider.java:319)at com.fasterxml.jackson.databind.objectwriter$prefetch.rialize(objectwriter.java:1396)at com.fasterxml.jackson.databind.objectwriter.writevalue(objectwriter.java:913)at org.springframework.http.converter.json.abstractjackson2httpmessageconverter.writeinternal(abstractjackson2httpmessageconverter.java:286)... 55 more
java.lang.illegalstateexception: getoutputstream() has already been called for this responat org.apache.catalina.connector.respon.getwriter(respon.java:624) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.connector.responfacade.getwriter(responfacade.java:211) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.thymeleaf.spring5.view.thymeleafview.renderfragment(thymeleafview.java:354) ~[thymeleaf-spring5-3.0.9.relea.jar:3.0.9.relea]at org.thymeleaf.spring5.view.thymeleafview.render(thymeleafview.java:187) ~[thymeleaf-spring5-3.0.9.relea.jar:3.0.9.relea]at org.springframework.web.rvlet.dispatcherrvlet.render(dispatcherrvlet.java:1325) ~[spring-webmvc-5.0.6.relea.jar:5.0.6.relea]at org.springframework.web.rvlet.dispatcherrvlet.processdispatchresult(dispatcherrvlet.java:1069) ~[spring-webmvc-5.0.6.relea.jar:5.0.6.relea]at org.springframework.web.rvlet.dispatcherrvlet.dodispatch(dispatcherrvlet.java:1008) ~[spring-webmvc-5.0.6.relea.jar:5.0.6.relea]at org.springframework.web.rvlet.dispatcherrvlet.dorvice(dispatcherrvlet.java:925) ~[spring-webmvc-5.0.6.relea.jar:5.0.6.relea]at org.springframework.web.rvlet.frameworkrvlet.processrequest(frameworkrvlet.java:974) ~[spring-webmvc-5.0.6.relea.jar:5.0.6.relea]at org.springframework.web.rvlet.frameworkrvlet.doget(frameworkrvlet.java:866) ~[spring-webmvc-5.0.6.relea.jar:5.0.6.relea]at javax.rvlet.http.httprvlet.rvice(httprvlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.springframework.web.rvlet.frameworkrvlet.rvice(frameworkrvlet.java:851) ~[spring-webmvc-5.0.6.relea.jar:5.0.6.relea]at javax.rvlet.http.httprvlet.rvice(httprvlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:231) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.tomcat.websocket.rver.wsfilter.dofilter(wsfilter.java:52) ~[tomcat-embed-websocket-8.5.31.jar:8.5.31]at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at com.urms.urmsfilter.dofilter(urmsfilter.java:199) ~[urms-1.8.0.jar:na]at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.springframework.web.filter.requestcontextfilter.dofilterinternal(requestcontextfilter.java:99) ~[spring-web-5.0.6.relea.jar:5.0.6.relea]at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.0.6.relea.jar:5.0.6.relea]at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.springframework.web.filter.httpputformcontentfilter.dofilterinternal(httpputformcontentfilter.java:109) ~[spring-web-5.0.6.relea.jar:5.0.6.relea]at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.0.6.relea.jar:5.0.6.relea]at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.springframework.web.filter.hiddenhttpmethodfilter.dofilterinternal(hiddenhttpmethodfilter.java:81) ~[spring-web-5.0.6.relea.jar:5.0.6.relea]at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.0.6.relea.jar:5.0.6.relea]at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:200) ~[spring-web-5.0.6.relea.jar:5.0.6.relea]at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.0.6.relea.jar:5.0.6.relea]at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:198) ~[tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:96) [tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.authenticator.authenticatorba.invoke(authenticatorba.java:496) [tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:140) [tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:81) [tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.core.standardenginevalve.invok文学手抄报e(standardenginevalve.java:87) [tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.catalina.connector.coyoteadapter.rvice(coyoteadapter.java:342) [tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.coyote.http11.http11processor.rvice(http11processor.java:803) [tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:66) [tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:790) [tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1468) [tomcat-embed-core-8.5.31.jar:8.5.31]at org.apache.tomcat.util.net.socketprocessorba.run(socketprocessorba.java:49) [tomcat-embed-core-8.5.31.jar:8.5.31]at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) [na:1.8.0_31]at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) [na:1.8.0_31]at org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) [tomcat-embed-core-8.5.31.jar:8.5.31]at java.lang.thread.run(thread.java:745) [na:1.8.0_31] 

最终解决办法是在需要jackjson将实体转json的地方不要用getone,改用findone,另外推荐使用jpa2的findbyid。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。

本文发布于:2023-04-03 22:25:11,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/8d53e3c54b5e926357c41bdcbb26d9e4.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:spring.doc

本文 PDF 下载地址:spring.pdf

下一篇:返回列表
标签:实体   对象   可能会   区别
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图