欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring-data-jpa中findOne与getOne的区别说明

程序员文章站 2022-03-15 20:53:44
目录spring-data-jpa中findone与getone区别findone与getone的注释中也有些帮助spring data jpa的findone和getone要分清可能会导致httpm...

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.
     * @see 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 serializer found for class org.hibernate.proxy.pojo.javassist.javassistlazyinitializer and no properties discovered to create beanserializer (to avoid exception, disable serializationfeature.fail_on_empty_beans) (through reference chain: com.essence.framework.common.callbackbody["result"]->com.essence.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.servlet.mvc.method.annotation.abstractmessageconvertermethodprocessor.writewithmessageconverters(abstractmessageconvertermethodprocessor.java:272)
	at org.springframework.web.servlet.mvc.method.annotation.requestresponsebodymethodprocessor.handlereturnvalue(requestresponsebodymethodprocessor.java:180)
	at org.springframework.web.method.support.handlermethodreturnvaluehandlercomposite.handlereturnvalue(handlermethodreturnvaluehandlercomposite.java:82)
	at org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:119)
	at org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:877)
	at org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:783)
	at org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:87)
	at org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:991)
	at org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:925)
	at org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:974)
	at org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:866)
	at javax.servlet.http.httpservlet.service(httpservlet.java:635)
	at org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:851)
	at javax.servlet.http.httpservlet.service(httpservlet.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.server.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.java: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.authenticatorbase.invoke(authenticatorbase.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.service(coyoteadapter.java:342)
	at org.apache.coyote.http11.http11processor.service(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.socketprocessorbase.run(socketprocessorbase.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)
caused by: com.fasterxml.jackson.databind.exc.invaliddefinitionexception: no serializer found for class org.hibernate.proxy.pojo.javassist.javassistlazyinitializer and no properties discovered to create beanserializer (to avoid exception, disable serializationfeature.fail_on_empty_beans) (through reference chain: com.essence.framework.common.callbackbody["result"]->com.essence.framework.sys.model.dictionary_$$_jvstbca_7["handler"])
	at com.fasterxml.jackson.databind.exc.invaliddefinitionexception.from(invaliddefinitionexception.java:77)
	at com.fasterxml.jackson.databind.serializerprovider.reportbaddefinition(serializerprovider.java:1191)
	at com.fasterxml.jackson.databind.databindcontext.reportbaddefinition(databindcontext.java:312)
	at com.fasterxml.jackson.databind.ser.impl.unknownserializer.failforempty(unknownserializer.java:71)
	at com.fasterxml.jackson.databind.ser.impl.unknownserializer.serialize(unknownserializer.java:33)
	at com.fasterxml.jackson.databind.ser.beanpropertywriter.serializeasfield(beanpropertywriter.java:727)
	at com.fasterxml.jackson.databind.ser.std.beanserializerbase.serializefields(beanserializerbase.java:719)
	at com.fasterxml.jackson.databind.ser.beanserializer.serialize(beanserializer.java:155)
	at com.fasterxml.jackson.databind.ser.beanpropertywriter.serializeasfield(beanpropertywriter.java:727)
	at com.fasterxml.jackson.databind.ser.std.beanserializerbase.serializefields(beanserializerbase.java:719)
	at com.fasterxml.jackson.databind.ser.beanserializer.serialize(beanserializer.java:155)
	at com.fasterxml.jackson.databind.ser.defaultserializerprovider._serialize(defaultserializerprovider.java:480)
	at com.fasterxml.jackson.databind.ser.defaultserializerprovider.serializevalue(defaultserializerprovider.java:319)
	at com.fasterxml.jackson.databind.objectwriter$prefetch.serialize(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 response
	at org.apache.catalina.connector.response.getwriter(response.java:624) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.apache.catalina.connector.responsefacade.getwriter(responsefacade.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.release.jar:3.0.9.release]
	at org.thymeleaf.spring5.view.thymeleafview.render(thymeleafview.java:187) ~[thymeleaf-spring5-3.0.9.release.jar:3.0.9.release]
	at org.springframework.web.servlet.dispatcherservlet.render(dispatcherservlet.java:1325) ~[spring-webmvc-5.0.6.release.jar:5.0.6.release]
	at org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:1069) ~[spring-webmvc-5.0.6.release.jar:5.0.6.release]
	at org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:1008) ~[spring-webmvc-5.0.6.release.jar:5.0.6.release]
	at org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:925) ~[spring-webmvc-5.0.6.release.jar:5.0.6.release]
	at org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:974) ~[spring-webmvc-5.0.6.release.jar:5.0.6.release]
	at org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:866) ~[spring-webmvc-5.0.6.release.jar:5.0.6.release]
	at javax.servlet.http.httpservlet.service(httpservlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:851) ~[spring-webmvc-5.0.6.release.jar:5.0.6.release]
	at javax.servlet.http.httpservlet.service(httpservlet.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.server.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.release.jar:5.0.6.release]
	at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.0.6.release.jar:5.0.6.release]
	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.release.jar:5.0.6.release]
	at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.0.6.release.jar:5.0.6.release]
	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.release.jar:5.0.6.release]
	at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.0.6.release.jar:5.0.6.release]
	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.release.jar:5.0.6.release]
	at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) ~[spring-web-5.0.6.release.jar:5.0.6.release]
	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.authenticatorbase.invoke(authenticatorbase.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.invoke(standardenginevalve.java:87) [tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:342) [tomcat-embed-core-8.5.31.jar:8.5.31]
	at org.apache.coyote.http11.http11processor.service(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.socketprocessorbase.run(socketprocessorbase.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。

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