所以我正在尝试将 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。服务(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)
这是从我的休息服务中调用的
@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();
}
持久性.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 会为我抽象。