Hibernate Error Persist

Asked

Viewed 144 times

1

I have a simple system of opening / calling query. I am in error when trying to insert a new call.

I started using Hibernate and I have a problem with "persist" to add a call to a certain call queue.

Below the codes and log:

So-called Class

@Entity
public class Chamado {

    /**
     * 
     */
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID_CHAMADO")
    private int id;

    @NotNull
    @Column(name="DESCRICAO")
    @Size(max=100)
    private String descricao;


    @NotNull
    @Column(name="STATUS")
    private String status;

    @NotNull
    @Column(name="DT_ABERTURA")
    private Date dataAbertura;

    @Column(name="DT_FECHAMENTO")
    private Date dataFechamento;


    @NotNull(message="Não pode ser vazio")
    @ManyToOne
    @JoinColumn(name="ID_FILA")
    private Fila fila;

Chamadodao class

public Chamado salvarNovoChamado(Fila fila, Chamado chamado) throws IOException{
        manager.persist(chamado);
        return chamado;
    }

Note: I know I do not use the "queue" parameter for anything, because it is the old model, but I still have doubts about how the Hibernate will pull the ID_FILA to link.

Stacktracer

abr 02, 2018 1:18:51 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [Spring MVC Dispatcher Servlet] in context with path [/arqsw_sdesk_a1] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: br.usjt.arqsw.entity.Chamado] with root cause
org.hibernate.PersistentObjectException: detached entity passed to persist: br.usjt.arqsw.entity.Chamado
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:293)
    at com.sun.proxy.$Proxy58.persist(Unknown Source)
    at br.usjt.arqsw.dao.ChamadoDAO.salvarNovoChamado(ChamadoDAO.java:64)
    at br.usjt.arqsw.service.ChamadoService.salvarNovoChamado(ChamadoService.java:28)
    at br.usjt.arqsw.controller.ManterChamadosController.salvarNovoChamado(ManterChamadosController.java:105)
    at br.usjt.arqsw.controller.ManterChamadosController$$FastClassBySpringCGLIB$$28d0cc1b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
    at br.usjt.arqsw.controller.ManterChamadosController$$EnhancerBySpringCGLIB$$d45355a0.salvarNovoChamado(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
    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 org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
    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:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    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:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    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)
  • 1

    I believe you are passing the object by filling its id, hence the error. It is being created or updated?

  • @Giulianabezerra then, I have the FILA ID of who will belong to the call, this FILA is another object, I realized that the same FILA ID is going to the CALLED ID also.

  • This is what cannot happen. The id of the called object is generated by Hibernate, depending on how you set up your PK settings. See who adds this information to the object and clear the value.

  • Only myself I pass the value of the ID, because Hibernate is taking the value of my select that I have in JSP. That is named "id". Which in this case is the same name in both classes, Call and Queue.

  • The object is being updated then, and not created?

  • Yes, sorry I said wrong earlier. I only assign values to classes and use persist.

  • Oh my problem now is to pass the queue object next to the called object. List of Constraint violations:[ ConstraintViolationImpl{interpolatedMessage='Não pode ser vazio', propertyPath=fila, rootBeanClass=class br.usjt.arqsw.entity.Chamado, messageTemplate='Não pode ser vazio'}&#xA;]] with root cause

Show 3 more comments

1 answer

0


Solved

Problem: Attribute "queue" which is an object, was being passed as null.

Workaround: Changed select name in "queue" JSP to "queue.id"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.