Hibernate-OGM mongodb,无法删除实体 - 分离

时间:2014-07-10 02:05:09

标签: java mongodb jboss hibernate-ogm

所以我试图将Hibernate OGM(4.1.0.Beta4)与MongoDB一起使用,现在我遇到了这个令我困惑的问题,所以我得到了我创建的实体列表,然后我试着循环列出并删除每一个 - 但是当我尝试时,我得到以下异常“删除一个分离的实例”,我假设我已经错过了持久性设置。

  

Context Path:/ TestApp Servlet Path:/ rest Path   信息:/ deleteallplease查询   String:null Stack Trace org.jboss.resteasy.spi.UnhandledException:   javax.ejb.EJBException:java.lang.IllegalArgumentException:删除   分离的实例   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(未知来源)   java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)   java.lang.Thread.run(未知来源)

这是从我的休息服务中调用的

@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类

@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>

我想我很困惑的主要事情是使用deleteAllTests()我得到实体列表,然后尝试删除它们但是它们已经分离了吗?所以我显然也缺少一些基本的东西,我的怀疑是它的mongo-ogm和JTA不好玩,因为mongo不是交易性的,但我认为ogm会为我抽象。

1 个答案:

答案 0 :(得分:0)

没有看到你的delete()方法,但我认为你正在使用em.remove()来删除一个实体。

您可以尝试使用em.remove(em.merge(entity))删除。