0

So I am trying to use Hibernate OGM (4.1.0.Beta4) with MongoDB, now I have run into this issue which is confusing me, so I get a list of my entities I created, then I try to cycle through that list and delete each one - however when I try that I get the following exception "Removing a detached instance", I am assuming I have missed something with the persistence setup.

Context Path:/TestApp Servlet Path:/rest Path Info:/deleteallplease Query String:null Stack Trace org.jboss.resteasy.spi.UnhandledException: javax.ejb.EJBException: java.lang.IllegalArgumentException: Removing a detached instance com.testapp.jpa.model.TestEntity#402881e546fa0f740146fa10cf210000 org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76) org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212) org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:149) org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:372) org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) javax.servlet.http.HttpServlet.service(HttpServlet.java:790) io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:52) io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61) io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73) io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146) io.undertow.server.Connectors.executeRootHandler(Connectors.java:168) io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:687) java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) java.lang.Thread.run(Unknown Source)

This is called from my rest service

@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RequestScoped
public class TestService {

@Inject
    private TestRepository testRepo;

    @GET
    @Path("/deleteallplease")
    public String deleteAllTests(){

        for( TestEntity rp : testRepo.findAll() ){
            testRepo.delete( rp );
        }

        return "done done done";
    }


}

TestRepo class

@Stateless
@LocalBean
public class TestRepository {

@Inject
private EntityManager em;

@Inject 
private Logger log;

public List<TestEntity> findAll() {
    return em.createQuery("FROM TestEntity", TestEntity.class).getResultList();
}

persistence.xml

<persistence version="2.0"
         xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
    http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="primary" >
    <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
    <class>com.test.TestEntity</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.ogm.datastore.provider" value="mongodb"/>
        <property name="hibernate.ogm.datastore.database" value="db"/>
        <property name="hibernate.ogm.datastore.host" value="127.0.0.1"/>
        <property name="hibernate.ogm.datastore.port" value="59541"/>
        <property name="hibernate.ogm.datastore.username" value="user"/>
        <property name="hibernate.ogm.datastore.password" value="password"/>
        <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
    </properties>
</persistence-unit>

</persistence>

I guess the major thing I am confused about is withing deleteAllTests() I get the list of entities, then try to delete them but they are detached already? So I am clearly missing something fundamental as well, my suspicion is that its mongo-ogm and JTA not playing nice because mongo is not transactional, but I figured ogm would abstract that for me.

qorpus
  • 3
  • 1

1 Answers1

0

didn't see your delete() method , but i think you are using em.remove() to delete an entity .

you can try to use em.remove(em.merge(entity)) to delete.

  • thanks, I actually gave up using OGM - reattaching it to the session might have helped as well. – qorpus Aug 28 '15 at 15:34