Table relationship - Spring Boot Hibernate: Error accessing field [private int

Asked

Viewed 371 times

0

Good morning everyone! I am having problems in the relationship of entities in the api, the bank was created correctly by Spring as can be seen below:

Relationship Manytomany in trip table:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@ManyToMany(targetEntity = Passageiro.class)
@JoinTable(name="viagem_has_passageiros", joinColumns=
        {@JoinColumn(name="viagem_id")}, inverseJoinColumns=
        {@JoinColumn(name="passageiro_id")})
private List passageiros;

@ManyToMany(targetEntity = Motorista.class)
@JoinTable(name="viagem_has_motoristas", joinColumns=
        {@JoinColumn(name="viagem_id")}, inverseJoinColumns=
        {@JoinColumn(name="motorista_id")})
private List motoristas;

I’m focusing on getting the passengers to work first, so after booting the api, I try it on Postman: inserir a descrição da imagem aqui I tested both with "viagem_id" and without, because I thought he could increment directly. It inserts the trip normally if it is without the passengers/drivers, but with them, either as GET or PUT, gives the following error:

"status": 500,
"error": "Internal Server Error",
"message": "Error accessing field [private int com.sambaonibus.samba_onibus.domain.Passageiro.id] by reflection for persistent property [com.sambaonibus.samba_onibus.domain.Passageiro#id] : {viagem_id=2, passageiro_id=1}; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int com.sambaonibus.samba_onibus.domain.Passageiro.id] by reflection for persistent property [com.sambaonibus.samba_onibus.domain.Passageiro#id] : {viagem_id=2, passageiro_id=1}",
"trace": "org.springframework.orm.jpa.JpaSystemException: Error accessing field [private int com.sambaonibus.samba_onibus.domain.Passageiro.id] by reflection for persistent property [com.sambaonibus.samba_onibus.domain.Passageiro#id] : {viagem_id=2, passageiro_id=1}; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int com.sambaonibus.samba_onibus.domain.Passageiro.id] by reflection for persistent property [com.sambaonibus.samba_onibus.domain.Passageiro#id] : {viagem_id=2, passageiro_id=1}\r\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:351)\r\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)\r\n\tat org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)\r\n\tat org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)\r\n\tat org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)\r\n\tat org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:138)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)\r\n\tat com.sun.proxy.$Proxy107.save(Unknown Source)\r\n\tat com.sambaonibus.samba_onibus.controller.ViagensController.edit(ViagensController.java:56)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:919)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:663)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat com.sambaonibus.samba_onibus.cors.WebConfig.doFilter(WebConfig.java:34)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\r\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.lang.Thread.run(Thread.java:748)\r\nCaused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private int com.sambaonibus.samba_onibus.domain.Passageiro.id] by reflection for persistent property [com.sambaonibus.samba_onibus.domain.Passageiro#id] : {viagem_id=2, passageiro_id=1}\r\n\tat org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:75)\r\n\tat org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:224)\r\n\tat org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4933)\r\n\tat org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:148)\r\n\tat org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:922)\r\n\tat org.hibernate.internal.SessionImpl.merge(SessionImpl.java:892)\r\n\tat org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:261)\r\n\tat org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:490)\r\n\tat org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:415)\r\n\tat org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:216)\r\n\tat org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:523)\r\n\tat org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:455)\r\n\tat org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:418)\r\n\tat org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:216)\r\n\tat org.hibernate.engine.internal.Cascade.cascade(Cascade.java:149)\r\n\tat org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:460)\r\n\tat org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:326)\r\n\tat org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:170)\r\n\tat org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:69)\r\n\tat org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:900)\r\n\tat org.hibernate.internal.SessionImpl.merge(SessionImpl.java:886)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350)\r\n\tat com.sun.proxy.$Proxy100.merge(Unknown Source)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:308)\r\n\tat com.sun.proxy.$Proxy100.merge(Unknown Source)\r\n\tat org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:492)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)\r\n\tat org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)\r\n\tat org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)\r\n\tat org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)\r\n\tat org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)\r\n\tat org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)\r\n\t... 67 more\r\nCaused by: java.lang.IllegalArgumentException: Can not set int field com.sambaonibus.samba_onibus.domain.Passageiro.id to java.util.LinkedHashMap\r\n\tat sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)\r\n\tat sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)\r\n\tat sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)\r\n\tat sun.reflect.UnsafeIntegerFieldAccessorImpl.getInt(UnsafeIntegerFieldAccessorImpl.java:56)\r\n\tat java.lang.reflect.Field.getInt(Field.java:574)\r\n\tat org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:62)\r\n\t... 118 more\r\n",
"path": "/viagens"

Anyone who’s ever been through this mistake... or has any idea, I really appreciate the help!

1 answer

1

Hello.

It’s a little difficult to visualize the error by Postman’s reply message and without all entity classes, but let’s go. Try to replace your relationship mapping to the following way:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@ManyToMany
@JoinTable(name = "viagem_has_passageiros",
          joinColumns = @JoinColumn(name="viagem_id"),
          inverseJoinColumns = @JoinColumn(name="passageiro_id")
)
private List<Passageiro> passageiros;

@ManyToMany
@JoinTable(name = "viagem_has_motoristas",
          joinColumns = @JoinColumn(name="viagem_id"),
          inverseJoinColumns = @JoinColumn(name="motorista_id")
)
private List<Motorista> motoristas;

In your Passenger Class:

@ManyToMany(mappedBy = "passageiros")
private List<Viagem> viagens;

In your class Driver:

@ManyToMany(mappedBy = "motoristas")
private List<Viagem> viagens;

Ah! And maybe you have problems with Lazyexception in the future. But let’s take it easy.

  • Thanks for the help! By making these modifications I got the following error: "status": 500, "error": "Internal Server Error", "message": "org.hibernate.Transientobjectexception: Object References an Unsaved Transient instance - save the Transient instance before Flushing: com.sambaonibus.samba_onibus.domain.Passenger; nested Exception is java.lang.Illegalstateexception: org.hibernate.Transientobjectexception: Object References an Unsaved Transient instance - save the Transient instance before Flushing: com.sambaonibus.samba_onibus.domain.Passenger"

  • Blz. You need to add the Scade now, getting like this: @ManyToMany(cascade = CascadeType.ALL)

  • "status": 500, "error": "Internal Server Error", "message": "Could not commit JPA transaction; nested Exception is javax.persistence.Rollbackexception: Error while committing the transaction",

  • I’m looking here, it looks like there’s a @Transaction annotation missing

  • I believe that there the problem is not related to mapping. This means that there is some error when making changes in the BD. Edit your question by placing the classes you are using the JPA functions please and the other entity classes. Look in the error log tbm for the pq of this error (The last Caused by would be a good start)

Browser other questions tagged

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