java - How to persist entity in hibernate postPersist listener -
let's got entity account
:
@entity(name = "account") @entitylisteners(listener.class) public class accountentity { @id @generatedvalue(strategy = generationtype.table) @column(name = "account_id") long id; @column(name = "username") string username; @column(name = "password") string password; <getters , setters... />
entity history store information changes in database:
@entity(name="operation_history") public class operationhistoryentity { @id @generatedvalue(strategy = generationtype.sequence) @column(name="operation_history_id") long id; @column(name = "entity_name") string entityname; @column(name = "record_id") long recordid; @column(name = "operation") string operation; @column(name = "creation_date") timestamp creationdate; @column(name = "update_date") timestamp updatedate; @column(name = "remove_date") timestamp removedate; <getters , setters... />
service store information:
@service public class operationhistoryservice { @persistencecontext entitymanager entitymanager; @autowired operationhistoryrepository operationhistoryrepository; @transactional public void addoperation(operationhistoryentity operationhistoryentity) { entitymanager.persist(operationhistoryentity); } }
and listener:
@component public class listener { @autowired operationhistoryservice operationhistoryservice; @postpersist public void postpersist(object obj) { <variables... /> operationhistoryentity operationhistoryentity = new operationhistoryentity(); operationhistoryentity.setentityname(entityname); operationhistoryentity.setrecordid(recordid); operationhistoryentity.setoperation(operation); operationhistoryentity.setcreationdate(creationdate); operationhistoryentity.setupdatedate(creationdate); operationhistoryentity.setremovedate(creationdate); operationhistoryservice.addoperation(operationhistoryentity); }
when run error:
java.lang.exception: exception purpose of providing stack trace @ org.hibernate.resource.transaction.backend.jdbc.internal.jdbcresourcelocaltransactioncoordinatorimpl$transactiondrivercontrolimpl.markrollbackonly(jdbcresourcelocaltransactioncoordinatorimpl.java:265) @ org.hibernate.resource.transaction.backend.jdbc.internal.jdbcresourcelocaltransactioncoordinatorimpl.beforecompletioncallback(jdbcresourcelocaltransactioncoordinatorimpl.java:156) @ org.hibernate.resource.transaction.backend.jdbc.internal.jdbcresourcelocaltransactioncoordinatorimpl.access$100(jdbcresourcelocaltransactioncoordinatorimpl.java:38) @ org.hibernate.resource.transaction.backend.jdbc.internal.jdbcresourcelocaltransactioncoordinatorimpl$transactiondrivercontrolimpl.commit(jdbcresourcelocaltransactioncoordinatorimpl.java:231) @ org.hibernate.engine.transaction.internal.transactionimpl.commit(transactionimpl.java:65) @ org.hibernate.jpa.internal.transactionimpl.commit(transactionimpl.java:61) @ org.springframework.orm.jpa.jpatransactionmanager.docommit(jpatransactionmanager.java:517) @ org.springframework.transaction.support.abstractplatformtransactionmanager.processcommit(abstractplatformtransactionmanager.java:761) @ org.springframework.transaction.support.abstractplatformtransactionmanager.commit(abstractplatformtransactionmanager.java:730) @ org.springframework.transaction.interceptor.transactionaspectsupport.committransactionafterreturning(transactionaspectsupport.java:483) @ org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:290) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:96) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.aop.framework.cglibaopproxy$dynamicadvisedinterceptor.intercept(cglibaopproxy.java:655) @ com.service.accountservice$$enhancerbyspringcglib$$f91573cb.addaccount(<generated>) @ com.rest.authenticationcontroller.createnewaccount(authenticationcontroller.java:92) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:221) @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:136) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:114) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:827) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:738) @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:85) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:963) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:897) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:970) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:872) @ javax.servlet.http.httpservlet.service(httpservlet.java:648) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:846) @ javax.servlet.http.httpservlet.service(httpservlet.java:729) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:230) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:165) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:192) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:165) @ com.jwtimpl.security.authenticationtokenfilter.dofilter(authenticationtokenfilter.java:47) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:192) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:165) @ com.jwtimpl.corsfilter.dofilter(corsfilter.java:22) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:192) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:165) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:317) @ org.springframework.security.web.access.intercept.filtersecurityinterceptor.invoke(filtersecurityinterceptor.java:127) @ org.springframework.security.web.access.intercept.filtersecurityinterceptor.dofilter(filtersecurityinterceptor.java:91) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:331) @ org.springframework.security.web.access.exceptiontranslationfilter.dofilter(exceptiontranslationfilter.java:115) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:331) @ org.springframework.security.web.session.sessionmanagementfilter.dofilter(sessionmanagementfilter.java:137) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:331) @ org.springframework.security.web.authentication.anonymousauthenticationfilter.dofilter(anonymousauthenticationfilter.java:111) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:331) @ org.springframework.security.web.servletapi.securitycontextholderawarerequestfilter.dofilter(securitycontextholderawarerequestfilter.java:169) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:331) @ org.springframework.security.web.savedrequest.requestcacheawarefilter.dofilter(requestcacheawarefilter.java:63) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:331) @ org.springframework.security.web.authentication.logout.logoutfilter.dofilter(logoutfilter.java:121) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:331) @ org.springframework.security.web.header.headerwriterfilter.dofilterinternal(headerwriterfilter.java:66) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:331) @ org.springframework.security.web.context.securitycontextpersistencefilter.dofilter(securitycontextpersistencefilter.java:105) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:331) @ org.springframework.security.web.context.request.async.webasyncmanagerintegrationfilter.dofilterinternal(webasyncmanagerintegrationfilter.java:56) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:331) @ org.springframework.security.web.filterchainproxy.dofilterinternal(filterchainproxy.java:214) @ org.springframework.security.web.filterchainproxy.dofilter(filterchainproxy.java:177) @ org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:346) @ org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:262) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:192) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:165) @ org.springframework.web.filter.requestcontextfilter.dofilterinternal(requestcontextfilter.java:99) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:192) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:165) @ org.springframework.web.filter.httpputformcontentfilter.dofilterinternal(httpputformcontentfilter.java:87) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:192) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:165) @ org.springframework.web.filter.hiddenhttpmethodfilter.dofilterinternal(hiddenhttpmethodfilter.java:77) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:192) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:165) @ org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:197) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:192) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:165) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:198) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:108) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:522) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:140) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:87) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:349) @ org.apache.coyote.http11.http11processor.service(http11processor.java:1110) @ org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:66) @ org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:785) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1425) @ org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:49) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745)
and error message rest service:
could not commit jpa transaction; nested exception javax.persistence.rollbackexception: error while committing transaction
is possible save operationhistoryentity object in postpersist method? thought annotate entities @entitylisteners , save information new, modified or deleted records in database operationhistory table.
Comments
Post a Comment