Announcement Announcement Module
Collapse
No announcement yet.
Detached entity passed ERROR while creating entity using JSON with MANY_TO_MANY rel Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Detached entity passed ERROR while creating entity using JSON with MANY_TO_MANY rel

    Hi I am new to Spring roo. I am getting "org.hibernate.PersistentObjectException: detached entity passed to persist" exception, when I post JSON for creating entity with MANY_TO_MANY relation. Here are details

    .roo file

    Code:
    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

    Code:
    // curl -i -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{title:'cat1'}' http://localhost:8080/bookmanager/categorys
    cat1 Category was created sucessfully

    Code:
    // 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
    Following Exception comes while creating "bookname1"

    Code:
    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)


    When I create using UI generated by spring roo, it works fine but it doesn't when I attempt same with JSON. I think I may not be passing json object in correct format. Please can anyone tell me how to post JSON object to create new Book entity associated with a specific Categories.

    Thank you
    Last edited by dattu; Oct 28th, 2012, 04:29 PM.
Working...
X