首页 文章

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

提问于
浏览
0

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

上下文路径:/ TestApp Servlet路径:/ rest路径信息:/ deleteallplease查询字符串:null堆栈跟踪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.Ht tpServletDispatcher.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.ser vlet.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(ServletInitialHand ler.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)

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

@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

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

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

相关问题