私はSpringrooを学ぶのは初めてです。リレーションorg.hibernate.PersistentObjectException: detached entity passed to persist
を持つエンティティを作成するためにJSONを投稿すると、例外が発生します。MANY_TO_MANY
詳細は次のとおりです。
.rooファイル:
project --topLevelPackage com.test.book --projectName bookmanager --java 6 --packaging JAR
jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
entity jpa --class ~.domain.Category
field string --fieldName title --sizeMin 2 --notNull
entity jpa --class ~.domain.Book
field string --fieldName name --notNull --unique
field set --fieldName categories --type ~.domain.Category --cardinality MANY_TO_MANY
focus --class ~.domain.Category
field set --fieldName books --type ~.domain.Book --cardinality MANY_TO_MANY --mappedBy categories
web mvc setup
web mvc all --package ~.web
json all --deepSerialize
web mvc json all After project is created I used following script to create entities
プロジェクトが作成された後、次のスクリプトを使用してエンティティを作成しました。
// curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{title:'cat1'}' http:// localhost: 8080/bookmanager/categorys
うまくいきました。cat1
カテゴリが正常に作成されました。
だが ...
// curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{name:'bookname1',categories:[{"id":1,"title":"cat1","version":0}]}' http:// localhost: 8080/bookmanager/books
次の例外が返されました:
Data access failure
Sorry, a problem occurred while accessing the database.
Details
org.hibernate.PersistentObjectException: detached entity passed to persist: com.test.book.domain.Category; nested exception is javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.test.book.domain.Category
org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:326)
org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15)
com.test.book.domain.Book_Roo_Jpa_ActiveRecord.ajc$interMethod$com_test_book_domain_Book_Roo_Jpa_ActiveRecord$com_test_book_domain_Book$persist(Book_Roo_Jpa_ActiveRecord.aj:43)
com.test.book.domain.Book.persist(Book.java:1)
com.test.book.domain.Book_Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$com_test_book_domain_Book_Roo_Jpa_ActiveRecord$com_test_book_domain_Book$persist(Book_Roo_Jpa_ActiveRecord.aj)
com.test.book.web.BookController_Roo_Controller_Json.ajc$interMethod$com_test_book_web_BookController_Roo_Controller_Json$com_test_book_web_BookController$createFromJson(BookController_Roo_Controller_Json.aj:44)
com.test.book.web.BookController.createFromJson(BookController.java:1)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:88)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)
spring rooによって生成されたUIを使用してオブジェクトを作成すると、正常に機能しますが、JSONで同じようにしようとすると機能しません。jsonオブジェクトを正しい形式で渡していない可能性があると思います。JSONオブジェクトを投稿して、特定のカテゴリに関連付けられた新しいBookエンティティを作成する方法を教えてください。