1

我是学习 Spring roo 的新手。当我发布 JSON 以创建具有关系的实体时,我遇到了org.hibernate.PersistentObjectException: detached entity passed to persist异常。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 对象以创建与特定类别关联的新书实体。

4

1 回答 1

1

我认为 ROO 不知道您正在通过您的 json 请求传入一个新类别。因此,它假定该类别已经存在,这会导致分离实体异常。

于 2013-01-21T22:07:15.720 回答