按照本教程使用 Guice 3.0:https ://code.google.com/p/google-guice/wiki/JPA
GuiceModule 类:
public class GuiceModule extends ServletModule {
@Override
protected void configureServlets() {
// start the service
bind(JPAInitializer.class).asEagerSingleton();
// PersistFilter
filter("/*").through(PersistFilter.class);
// jpa + guice
install(new JpaPersistModule("drone-cloud"));
// bindings
bind(UserService.class).to(UserServiceImpl.class);
bind(TwoFactorAuthenticator.class).to(GoogleAuthenticator.class);
bind(ImageQueue.class).asEagerSingleton();
bind(DroneCloudServerHandler.class).asEagerSingleton();
}
}
JPAInitializer.class
import javax.inject.Inject;
import com.google.inject.persist.PersistService;
public class JPAInitializer {
@Inject
JPAInitializer(PersistService service) {
service.start();
}
}
控制器类:
@Consumes(MediaType.APPLICATION_JSON)
@Path("/mission")
public class MissionController {
@Inject
Provider<EntityManager> _em;
@Path("/new")
@POST
@Transactional
public String newMission(SimpleMission mission) {
_em.get().persist(mission);
_em.get().flush();
_em.get().close();
return "{ \"mission_id\": \"" + mission.getId() + "\" }";
}
}
和堆栈跟踪:
javax.persistence.TransactionRequiredException:在 org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332) 的 org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171) 没有事务正在进行com.drone.mission.controller.MissionController.newMission(MissionController.java:37) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect。 DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81 ) 在 org.glassfish.jersey。server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171) at org.glassfish.jersey.server.model .internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195) 在 org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104) 在 org.glassfish.jersey.server.model.ResourceMethodInvoker .invoke(ResourceMethodInvoker.java:406) 在 org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:350) 在 org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)在 org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259) 在 org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) 在 org.glassfish.jersey.internal .Errors$1.call(Errors.java:267) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297)在 org.glassfish.jersey.internal.Errors.process(Errors.java:267) 在 org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:320) 在 org.glassfish.jersey.server.ServerRuntime .process(ServerRuntime.java:236) 在 org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028) 在 org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373) 在 org. glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381) 在 org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344) 在 org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219) 在 org.apache.catalina.core.ApplicationFilterChain .internalDoFilter(ApplicationFilterChain.java:303) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在 org.apache.catalina.core.StandardWrapperValve.invoke( StandardWrapperValve.java:220) 在 org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:122) 在 org.org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) 的 apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java) org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java: 170) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 在 org.apache.catalina.valves.AccessLogValve.invoke 的 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java) (AccessLogValve.java:950) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 在 org.apache。 coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler。进程(AbstractProtocol.java:607)在 org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java .util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 在 java.lang.Thread.run(Thread.java:744)
我究竟做错了什么?
谢谢