Good morning friends, I'm about 3 days with difficulties in an error that has been occurring in my application.
I have two entities, CLIENT and VEI. I did the mapping bidirectionally saying that my client may have one or more vehicles.
private List<Veiculo> veiculo = new ArrayList<>();
@OneToMany(mappedBy = "cliente", cascade= CascadeType.ALL, fetch = FetchType.LAZY)
public List<Veiculo> getVeiculo() {
return veiculo;}
I can register the Customer and the vehicle related to it normally. But I can not edit them. I created a screen where I search the client. WhenIclicktheeditbutton,ittakesthevalueoftheidtodotheconversionandsetsthedataalreadyfilledintotheclienteditscreen.Allvaluesarefilledastheynormallyshould,butwhenattemptingtosavetheclientobjectafterachangehasbeenmade.Ihavethefollowingerror.
out31,201811:44:58AMcom.sun.faces.lifecycle.InvokeApplicationPhaseexecuteADVERTÊNCIA:#{cadastroClienteBean.salvar}:org.hibernate.LazyInitializationException:failedtolazilyinitializeacollectionofrole:model.Cliente.veiculo,couldnotinitializeproxy-noSessionjavax.faces.FacesException:#{cadastroClienteBean.salvar}:org.hibernate.LazyInitializationException:failedtolazilyinitializeacollectionofrole:model.Cliente.veiculo,couldnotinitializeproxy-noSessionatcom.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)atjavax.faces.component.UICommand.broadcast(UICommand.java:315)atjavax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)atjavax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)atcom.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)atcom.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)atcom.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)atjavax.faces.webapp.FacesServlet.service(FacesServlet.java:658)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)atorg.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)atorg.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)atorg.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)atorg.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)atorg.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)atorg.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)atorg.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)atjava.util.concurrent.ThreadPoolExecutor.runWorker(UnknownSource)atjava.util.concurrent.ThreadPoolExecutor$Worker.run(UnknownSource)atorg.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)atjava.lang.Thread.run(UnknownSource)Causedby:javax.faces.el.EvaluationException:org.hibernate.LazyInitializationException:failedtolazilyinitializeacollectionofrole:model.Cliente.veiculo,couldnotinitializeproxy-noSessionatjavax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)atcom.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)...29moreCausedby:org.hibernate.LazyInitializationException:failedtolazilyinitializeacollectionofrole:model.Cliente.veiculo,couldnotinitializeproxy-noSessionatorg.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)atorg.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)atorg.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)atorg.hibernate.collection.internal.AbstractPersistentCollection.write(AbstractPersistentCollection.java:370)atorg.hibernate.collection.internal.PersistentBag.add(PersistentBag.java:291)atController.CadastroClienteBean.cadastrarCarro(CadastroClienteBean.java:38)atController.CadastroClienteBean.salvar(CadastroClienteBean.java:42)atController.CadastroClienteBean$Proxy$_$$_WeldClientProxy.salvar(CadastroClienteBean$Proxy$_$$_WeldClientProxy.java)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)atjava.lang.reflect.Method.invoke(UnknownSource)atorg.apache.el.parser.AstValue.invoke(AstValue.java:247)atorg.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)atorg.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)atorg.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)atcom.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)atjavax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)...30more
ButIwanttoupdateonlythecustomerdataandnotthevehiclerelatedtoitatthistime,howtosolvethis?
I'mstillstarting,anythingcanaskmetorespondatt;
ErrorusingEAGERformatting
ADVERTÊNCIA:#{cadastroClienteBean.salvar}:javax.persistence.RollbackException:Errorwhilecommittingthetransactionjavax.faces.FacesException:#{cadastroClienteBean.salvar}:javax.persistence.RollbackException:Errorwhilecommittingthetransactionatcom.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)atjavax.faces.component.UICommand.broadcast(UICommand.java:315)atjavax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)atjavax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)atcom.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)atcom.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)atcom.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)atjavax.faces.webapp.FacesServlet.service(FacesServlet.java:658)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)atorg.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)atorg.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)atorg.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)atorg.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)atorg.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)atorg.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)atorg.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)atjava.util.concurrent.ThreadPoolExecutor.runWorker(UnknownSource)atjava.util.concurrent.ThreadPoolExecutor$Worker.run(UnknownSource)atorg.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)atjava.lang.Thread.run(UnknownSource)Causedby:javax.faces.el.EvaluationException:javax.persistence.RollbackException:Errorwhilecommittingthetransactionatjavax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)atcom.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)...29moreCausedby:javax.persistence.RollbackException:Errorwhilecommittingthetransactionatorg.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:92)atutil.jpa.TransactionInterceptor.invoke(TransactionInterceptor.java:47)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)atjava.lang.reflect.Method.invoke(UnknownSource)atorg.jboss.weld.interceptor.proxy.SimpleMethodInvocation.invoke(SimpleMethodInvocation.java:30)atorg.jboss.weld.interceptor.proxy.SimpleInterceptionChain.invokeNextInterceptor(SimpleInterceptionChain.java:69)atorg.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:112)atorg.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:88)atorg.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:55)atservice.CadastroClienteService$Proxy$_$$_WeldSubclass.salvar(CadastroClienteService$Proxy$_$$_WeldSubclass.java)atController.CadastroClienteBean.salvar(CadastroClienteBean.java:43)atController.CadastroClienteBean$Proxy$_$$_WeldClientProxy.salvar(CadastroClienteBean$Proxy$_$$_WeldClientProxy.java)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)atjava.lang.reflect.Method.invoke(UnknownSource)atorg.apache.el.parser.AstValue.invoke(AstValue.java:247)atorg.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)atorg.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)atorg.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)atcom.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)atjavax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)...30moreCausedby:javax.validation.ConstraintViolationException:Validationfailedforclasses[model.Veiculo]duringpersisttimeforgroups[javax.validation.groups.Default,]Listofconstraintviolations:[ConstraintViolationImpl{interpolatedMessage='nãopodesernulo',propertyPath=placa,rootBeanClass=classmodel.Veiculo,messageTemplate='{javax.validation.constraints.NotNull.message}'}]atorg.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159)atorg.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)atorg.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:181)atorg.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:81)atorg.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)atorg.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)atorg.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:286)atorg.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)atorg.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)atorg.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)atorg.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)atorg.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)atorg.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)atorg.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)...53more
Savemethodofmyserviceclass
@InjectprivateClientesclientes;@TransactionalpublicClientesalvar(Clientecliente){ClienteclienteExisente=clientes.porNome(cliente.getNome());if(clienteExisente!=null&&clienteExisente.equals(cliente)){thrownewNegocioException("Ja existe um cliente com esse mesmo nome!");
}
return clientes.guardar(cliente);
}
Repository
@Inject
private EntityManager manager;
public Cliente guardar(Cliente cliente) {
return manager.merge(cliente);
}