java - JPA and Hibernate - id generator -
i'm using jpa , hibernate frameworks , when i'm trying insert new record, hibernate returns error because column 'employee_id' null. class employee:
@entity public class employee implements serializable { @id @generatedvalue(strategy = generationtype.identity) private long id; @onetomany(fetch = fetchtype.lazy, cascade = cascadetype.all, orphanremoval = true, mappedby="employee") private list<shift> shifts = new arraylist<shift>();
class shift:
@entity public class shift implements serializable { @id @generatedvalue(strategy = generationtype.identity) private long id; @manytoone(fetch = fetchtype.eager, cascade = cascadetype.detach) @joincolumn(name="employee_id") private employee employee;
the moment when insert record:
employee employee = getemployeedata(); entitymanager.persist(employee); entitymanager.flush();
getemployeedata method:
private employee getemployeedata() { employee employee = new employee(); shift shift = new shift(); employee.addshift(shift); return employee; }
exception:
9:24:35,730 error [org.hibernate.engine.jdbc.spi.sqlexceptionhelper] (default task-56) cannot insert value null 'employee_id' column, table 'db.dbo.shift'; column not allow nulls. failure insert 09:24:35,750 error [org.jboss.as.ejb3.invocation] (default task-56) jbas014134: ejb invocation failed on component persistencescopeextendedejb method public abstract void objective.commons.jpa.threadedpersistence.scope.persistencescopewithtransaction.save(): javax.ejb.ejbexception: javax.persistence.persistenceexception: org.hibernate.exception.constraintviolationexception: not execute statement @ org.jboss.as.ejb3.tx.cmttxinterceptor.handleexceptioninourtx(cmttxinterceptor.java:190) [wildfly-ejb3-8.1.0.final.jar:8.1.0.final] @ org.jboss.as.ejb3.tx.cmttxinterceptor.invokeinourtx(cmttxinterceptor.java:275) [wildfly-ejb3-8.1.0.final.jar:8.1.0.final] @ org.jboss.as.ejb3.tx.cmttxinterceptor.required(cmttxinterceptor.java:340) [wildfly-ejb3-8.1.0.final.jar:8.1.0.final] @ org.jboss.as.ejb3.tx.cmttxinterceptor.processinvocation(cmttxinterceptor.java:239) [wildfly-ejb3-8.1.0.final.jar:8.1.0.final] @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.as.ejb3.component.interceptors.currentinvocationcontextinterceptor.processinvocation(currentinvocationcontextinterceptor.java:41) [wildfly-ejb3-8.1.0.final.jar:8.1.0.final] @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.as.ejb3.component.invocationmetrics.waittimeinterceptor.processinvocation(waittimeinterceptor.java:43) [wildfly-ejb3-8.1.0.final.jar:8.1.0.final] @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.as.ejb3.security.securitycontextinterceptor.processinvocation(securitycontextinterceptor.java:95) [wildfly-ejb3-8.1.0.final.jar:8.1.0.final] @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.as.ejb3.component.interceptors.shutdowninterceptorfactory$1.processinvocation(shutdowninterceptorfactory.java:64) [wildfly-ejb3-8.1.0.final.jar:8.1.0.final] @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.as.ejb3.component.interceptors.logginginterceptor.processinvocation(logginginterceptor.java:59) [wildfly-ejb3-8.1.0.final.jar:8.1.0.final] @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.as.ee.component.namespacecontextinterceptor.processinvocation(namespacecontextinterceptor.java:50) @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.as.ejb3.component.interceptors.additionalsetupinterceptor.processinvocation(additionalsetupinterceptor.java:55) [wildfly-ejb3-8.1.0.final.jar:8.1.0.final] @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.invocation.contextclassloaderinterceptor.processinvocation(contextclassloaderinterceptor.java:64) @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.invocation.interceptorcontext.run(interceptorcontext.java:326) @ org.wildfly.security.manager.wildflysecuritymanager.dochecked(wildflysecuritymanager.java:448) @ org.jboss.invocation.accesscheckinginterceptor.processinvocation(accesscheckinginterceptor.java:61) @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.invocation.interceptorcontext.run(interceptorcontext.java:326) @ org.jboss.invocation.privilegedwithcombinerinterceptor.processinvocation(privilegedwithcombinerinterceptor.java:80) @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.invocation.chainedinterceptor.processinvocation(chainedinterceptor.java:61) @ org.jboss.as.ee.component.viewservice$view.invoke(viewservice.java:185) @ org.jboss.as.ee.component.viewdescription$1.processinvocation(viewdescription.java:182) @ org.jboss.invocation.interceptorcontext.proceed(interceptorcontext.java:309) @ org.jboss.invocation.chainedinterceptor.processinvocation(chainedinterceptor.java:61) @ org.jboss.as.ee.component.proxyinvocationhandler.invoke(proxyinvocationhandler.java:73) @ objective.commons.jpa.threadedpersistence.scope.persistencescopewithtransaction$$$view3.save(unknown source) [commons-jpa-threadedpersistence-1.0.4-snapshot.jar:] @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) [rt.jar:1.8.0_40] @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) [rt.jar:1.8.0_40] @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) [rt.jar:1.8.0_40] @ java.lang.reflect.method.invoke(method.java:497) [rt.jar:1.8.0_40] @ org.jboss.weld.util.reflection.reflections.invokeandunwrap(reflections.java:401) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.bean.proxy.enterprisebeanproxymethodhandler.invoke(enterprisebeanproxymethodhandler.java:99) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.bean.proxy.enterprisetargetbeaninstance.invoke(enterprisetargetbeaninstance.java:56) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.bean.proxy.injectionpointpropagatingenterprisetargetbeaninstance.invoke(injectionpointpropagatingenterprisetargetbeaninstance.java:65) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.bean.proxy.proxymethodhandler.invoke(proxymethodhandler.java:100) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ objective.commons.jpa.threadedpersistence.extended.scope.persistencescopewithtransaction$2061512995$proxy$_$$_weld$enterpriseproxy$.save(unknown source) [commons-jpa-threadedpersistence-extended-1.0.4-snapshot.jar:] @ pontualpro.funcionario.cadastrofuncionariocontroller.salvar(cadastrofuncionariocontroller.java:190) [classes:] @ pontualpro.funcionario.cadastrofuncionariocontroller$proxy$_$$_weldsubclass.salvar(unknown source) [classes:] @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) [rt.jar:1.8.0_40] @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) [rt.jar:1.8.0_40] @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) [rt.jar:1.8.0_40] @ java.lang.reflect.method.invoke(method.java:497) [rt.jar:1.8.0_40] @ org.jboss.weld.interceptor.proxy.simpleinterceptionchain.interceptorchaincompleted(simpleinterceptionchain.java:52) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.interceptor.chain.abstractinterceptionchain.invokenextinterceptor(abstractinterceptionchain.java:83) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.interceptor.proxy.interceptorinvocationcontext.proceed(interceptorinvocationcontext.java:146) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ objective.commons.jpa.threadedpersistence.cdi.automaticpersistencescopeinterceptor.managescope(automaticpersistencescopeinterceptor.java:30) [commons-jpa-threadedpersistence-1.0.4-snapshot.jar:] @ sun.reflect.generatedmethodaccessor57.invoke(unknown source) [:1.8.0_40] @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) [rt.jar:1.8.0_40] @ java.lang.reflect.method.invoke(method.java:497) [rt.jar:1.8.0_40] @ org.jboss.weld.interceptor.proxy.simplemethodinvocation.invoke(simplemethodinvocation.java:30) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.interceptor.chain.abstractinterceptionchain.invokenext(abstractinterceptionchain.java:103) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.interceptor.chain.abstractinterceptionchain.invokenextinterceptor(abstractinterceptionchain.java:81) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.interceptor.proxy.interceptormethodhandler.executeinterception(interceptormethodhandler.java:48) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.interceptor.proxy.interceptormethodhandler.invoke(interceptormethodhandler.java:41) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.bean.proxy.combinedinterceptoranddecoratorstackmethodhandler.invoke(combinedinterceptoranddecoratorstackmethodhandler.java:53) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ pontualpro.funcionario.cadastrofuncionariocontroller$proxy$_$$_weldsubclass.salvar(unknown source) [classes:] @ pontualpro.funcionario.cadastrofuncionariocontroller$proxy$_$$_weldclientproxy.salvar(unknown source) [classes:] @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) [rt.jar:1.8.0_40] @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) [rt.jar:1.8.0_40] @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) [rt.jar:1.8.0_40] @ java.lang.reflect.method.invoke(method.java:497) [rt.jar:1.8.0_40] @ com.sun.el.parser.astvalue.invoke(astvalue.java:275) [javax.el-3.0.0.jar:] @ com.sun.el.methodexpressionimpl.invoke(methodexpressionimpl.java:304) [javax.el-3.0.0.jar:] @ org.jboss.weld.util.el.forwardingmethodexpression.invoke(forwardingmethodexpression.java:40) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.el.weldmethodexpression.invoke(weldmethodexpression.java:50) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.util.el.forwardingmethodexpression.invoke(forwardingmethodexpression.java:40) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ org.jboss.weld.el.weldmethodexpression.invoke(weldmethodexpression.java:50) [weld-core-impl-2.1.2.final.jar:2014-01-09 09:23] @ com.sun.faces.facelets.el.tagmethodexpression.invoke(tagmethodexpression.java:105) [jsf-impl-2.2.6-jbossorg-4.jar:] @ javax.faces.component.methodbindingmethodexpressionadapter.invoke(methodbindingmethodexpressionadapter.java:87) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6] @ com.sun.faces.application.actionlistenerimpl.processaction(actionlistenerimpl.java:102) [jsf-impl-2.2.6-jbossorg-4.jar:] @ javax.faces.component.uicommand.broadcast(uicommand.java:315) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6] @ javax.faces.component.uiviewroot.broadcastevents(uiviewroot.java:790) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6] @ javax.faces.component.uiviewroot.processapplication(uiviewroot.java:1282) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6] @ com.sun.faces.lifecycle.invokeapplicationphase.execute(invokeapplicationphase.java:81) [jsf-impl-2.2.6-jbossorg-4.jar:] @ com.sun.faces.lifecycle.phase.dophase(phase.java:101) [jsf-impl-2.2.6-jbossorg-4.jar:] @ com.sun.faces.lifecycle.lifecycleimpl.execute(lifecycleimpl.java:198) [jsf-impl-2.2.6-jbossorg-4.jar:] @ javax.faces.webapp.facesservlet.service(facesservlet.java:646) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6] @ io.undertow.servlet.handlers.servlethandler.handlerequest(servlethandler.java:85) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.filterhandler$filterchainimpl.dofilter(filterhandler.java:130) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ org.ocpsoft.rewrite.servlet.rewritefilter.dofilter(rewritefilter.java:205) [rewrite-servlet-2.0.12.final.jar:2.0.12.final] @ io.undertow.servlet.core.managedfilter.dofilter(managedfilter.java:60) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.filterhandler$filterchainimpl.dofilter(filterhandler.java:132) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.filterhandler.handlerequest(filterhandler.java:85) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.security.servletsecurityrolehandler.handlerequest(servletsecurityrolehandler.java:61) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.servletdispatchinghandler.handlerequest(servletdispatchinghandler.java:36) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.servletinitialhandler.dispatchrequest(servletinitialhandler.java:229) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.servletinitialhandler.dispatchtopath(servletinitialhandler.java:172) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.spec.requestdispatcherimpl.forward(requestdispatcherimpl.java:159) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ org.ocpsoft.rewrite.servlet.impl.httprewriteresulthandler.handleresult(httprewriteresulthandler.java:41) [rewrite-servlet-2.0.12.final.jar:2.0.12.final] @ org.ocpsoft.rewrite.servlet.rewritefilter.rewrite(rewritefilter.java:268) [rewrite-servlet-2.0.12.final.jar:2.0.12.final] @ org.ocpsoft.rewrite.servlet.rewritefilter.dofilter(rewritefilter.java:188) [rewrite-servlet-2.0.12.final.jar:2.0.12.final] @ io.undertow.servlet.core.managedfilter.dofilter(managedfilter.java:60) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.filterhandler$filterchainimpl.dofilter(filterhandler.java:132) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ pontualpro.security.segurancafilter.dofilter(segurancafilter.java:43) [classes:] @ io.undertow.servlet.core.managedfilter.dofilter(managedfilter.java:60) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.filterhandler$filterchainimpl.dofilter(filterhandler.java:132) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ pontualpro.commons.setencodingfilter.dofilter(setencodingfilter.java:19) [classes:] @ io.undertow.servlet.core.managedfilter.dofilter(managedfilter.java:60) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.filterhandler$filterchainimpl.dofilter(filterhandler.java:132) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.filterhandler.handlerequest(filterhandler.java:85) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.security.servletsecurityrolehandler.handlerequest(servletsecurityrolehandler.java:61) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.servletdispatchinghandler.handlerequest(servletdispatchinghandler.java:36) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ org.wildfly.extension.undertow.security.securitycontextassociationhandler.handlerequest(securitycontextassociationhandler.java:78) @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.security.sslinformationassociationhandler.handlerequest(sslinformationassociationhandler.java:113) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.security.servletauthenticationcallhandler.handlerequest(servletauthenticationcallhandler.java:56) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.security.handlers.abstractconfidentialityhandler.handlerequest(abstractconfidentialityhandler.java:45) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.security.servletconfidentialityconstrainthandler.handlerequest(servletconfidentialityconstrainthandler.java:61) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.security.handlers.authenticationmechanismshandler.handlerequest(authenticationmechanismshandler.java:58) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.security.cachedauthenticatedsessionhandler.handlerequest(cachedauthenticatedsessionhandler.java:70) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.security.handlers.securityinitialhandler.handlerequest(securityinitialhandler.java:76) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.15.final.jar:1.0.15.final] @ org.wildfly.extension.undertow.security.jacc.jacccontextidhandler.handlerequest(jacccontextidhandler.java:61) @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.server.handlers.predicatehandler.handlerequest(predicatehandler.java:25) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.servletinitialhandler.handlefirstrequest(servletinitialhandler.java:240) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.servletinitialhandler.dispatchrequest(servletinitialhandler.java:227) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.servletinitialhandler.access$000(servletinitialhandler.java:73) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.servlet.handlers.servletinitialhandler$1.handlerequest(servletinitialhandler.java:146) [undertow-servlet-1.0.15.final.jar:1.0.15.final] @ io.undertow.server.connectors.executeroothandler(connectors.java:177) [undertow-core-1.0.15.final.jar:1.0.15.final] @ io.undertow.server.httpserverexchange$1.run(httpserverexchange.java:727) [undertow-core-1.0.15.final.jar:1.0.15.final] @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) [rt.jar:1.8.0_40] @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) [rt.jar:1.8.0_40] @ java.lang.thread.run(thread.java:745) [rt.jar:1.8.0_40]
i know id of table employee generated when give commit, how can fix keeping "list shift" in class employee?
you have bi-directional one-to-many
association between entities, have maintain same in java code adding line -- shift.setemployee(employee);
so getemployeedata
code this:
private employee getemployeedata() { employee employee = new employee(); shift shift = new shift(); employee.addshift(shift); shift.setemployee(employee); // adding line fixes issue. return employee; }
Comments
Post a Comment