Я пытаюсь сохранить данные в WorkItem Table, используя RestController с JSON ниже:
{
"wrkitmid": "123",
"linkingApplRef": [
{
"linkingApplRefid": "1",
"documentPage": [
{
"docpageid": 123
}
]
}
]
}
Но я получаю эту ошибку
Невозможно найти com.example.demo.LinkingApplRef с идентификатором 1
Редактировать:
В соответствии с предложением Клавдия я пытался с linkingApplRef в JSON, который присутствовал в БД, и он успешно выполнен,Но когда я применил CASCADE в WorkItem Entity:
@OneToMany(CASCADE)
@JoinColumn(name = "WRK_ITM_ID")
private List<LinkingApplRef> linkingApplRef;
& CASCADE в LinkingApplRef
@OneToMany(CASCADE)
@JoinColumn(name = "LINK_APP_REF_ID")
private List<DocumentPage> documentPage;
с тем же JSON (со связыванием ApplRef с ID 1), он работал без сбоев
Restcontroller выглядит следующим образом:
@RequestMapping(value="/saveWorkItem", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public String savebiWorkItem (@RequestBody WorkItem w1) {
System.out.println(w1);
for (LinkingApplRef linkingApplRef : w1.getLinkingApplRef()) {
linkingApplRef.setWorkItem(w1.getWrkitmid());
for ( DocumentPage documentPage : linkingApplRef.getDocumentPage()) {
documentPage.setLinkingApplRefid(linkingApplRef.getLinkingApplRefid());
documentPage.setWorkItem(w1.getWrkitmid());
}
}
try {
workItemRepository.save(w1);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println(w1);
return "success";
}
Объект WorkItem:
@Entity
@Table(name="WRK_ITM", schema="cf_2583f365_c3c6_499a_a60d_138e7e7023eb")
public class WorkItem {
@Id
@Column(name = "WRK_ITM_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int wrkitmid;
@OneToMany()
@JoinColumn(name = "WRK_ITM_ID")
private List<LinkingApplRef> linkingApplRef;
public WorkItem() {
super();
}
public int getWrkitmid() {
return wrkitmid;
}
public void setWrkitmid(int wrkitmid) {
this.wrkitmid = wrkitmid;
}
public List<LinkingApplRef> getLinkingApplRef() {
return linkingApplRef;
}
public void setLinkingApplRef(List<LinkingApplRef> linkingApplRef) {
this.linkingApplRef = linkingApplRef;
}
Ссылочный объект приложения ссылки:
@Entity
@Table(name="LINK_APP_REF", schema="cf_2583f365_c3c6_499a_a60d_138e7e7023eb")
public class LinkingApplRef {
@Id
@Column(name = "LINK_APP_REF_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int linkingApplRefid;
@Column(name = "WRK_ITM_ID")
private int workItem;
@OneToMany()
@JoinColumn(name = "LINK_APP_REF_ID")
private List<DocumentPage> documentPage;
public LinkingApplRef() {
super();
}
public int getLinkingApplRefid() {
return linkingApplRefid;
}
public void setLinkingApplRefid(int linkingApplRefid) {
this.linkingApplRefid = linkingApplRefid;
}
public int getWorkItem() {
return workItem;
}
public void setWorkItem(int workItem) {
this.workItem = workItem;
}
public List<DocumentPage> getDocumentPage() {
return documentPage;
}
public void setDocumentPage(List<DocumentPage> documentPage) {
this.documentPage = documentPage;
}
}
Объект страницы документа:
@Entity
@Table(name="DOC_PAGE", schema="cf_2583f365_c3c6_499a_a60d_138e7e7023eb")
public class DocumentPage {
@Id
@Column(name = "DOC_PAGE_ID")
private int docpageid;
@Column(name = "WRK_ITM_ID")
private int workItemid;
@Column(name = "LINK_APP_REF_ID")
private int linkingApplRefid;
public DocumentPage() {
super();
}
public int getDocpageid() {
return docpageid;
}
public void setDocpageid(int docpageid) {
this.docpageid = docpageid;
}
public int getWorkItem() {
return workItemid;
}
public void setWorkItem(int workItem) {
this.workItemid = workItem;
}
public int getLinkingApplRefid() {
return linkingApplRefid;
}
public void setLinkingApplRefid(int linkingApplRefid) {
this.linkingApplRefid = linkingApplRefid;
}
}
Stacktrace показывает следующую ошибку:
select
workitem0_.WRK_ITM_ID as WRK_ITM_1_5_0_
from
WRK_ITM workitem0_
where
workitem0_.WRK_ITM_ID=?
2019-02-04 00:49:46.845 TRACE 6232 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [123]
2019-02-04 00:49:46.924 DEBUG 6232 --- [nio-8080-exec-2] org.hibernate.SQL :
insert
into
WRK_ITM
values
( )
2019-02-04 00:49:46.936 DEBUG 6232 --- [nio-8080-exec-2] org.hibernate.SQL :
select
linkingapp0_.LINK_APP_REF_ID as LINK_APP1_1_0_,
linkingapp0_.WRK_ITM_ID as WRK_ITM_2_1_0_
from
LINK_APP_REF linkingapp0_
where
linkingapp0_.LINK_APP_REF_ID=?
2019-02-04 00:49:46.936 TRACE 6232 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as
[INTEGER] - [1]
org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.example.demo.LinkingApplRef with id 1; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.example.demo.LinkingApplRef with id 1
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:373)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:227)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:507)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy92.save(Unknown Source)
at com.example.demo.StudentsController.savebiWorkItem(StudentsController.java:133)
at com.example.demo.StudentsController$$FastClassBySpringCGLIB$$3d32846e.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685)
at com.example.demo.StudentsController$$EnhancerBySpringCGLIB$$13749923.savebiWorkItem(<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:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:870)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
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.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
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.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
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.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
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.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
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:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
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:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
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)
Caused by: javax.persistence.EntityNotFoundException: Unable to find com.example.demo.LinkingApplRef with id 1
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:159)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1239)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1122)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:672)
at org.hibernate.type.EntityType.resolve(EntityType.java:457)
at org.hibernate.type.EntityType.replace(EntityType.java:356)
at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:544)
at org.hibernate.type.CollectionType.replace(CollectionType.java:692)
at org.hibernate.type.AbstractType.replace(AbstractType.java:144)
at org.hibernate.type.TypeHelper.replaceAssociations(TypeHelper.java:247)
at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:420)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:240)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:301)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:170)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:69)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:883)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:869)
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:497)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350)
at com.sun.proxy.$Proxy86.merge(Unknown Source)
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:497)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:304)
at com.sun.proxy.$Proxy86.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:493)
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:497)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:377)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:629)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:593)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
... 68 more