"web container has not yet been started" 关于通过 jax-rs 持久化实体
"web container has not yet been started" on persisting an entity via jax-rs
问题:
我正在尝试实现一个简单的 jax-rs 服务,该服务将持久化作为 json 传递的实体。
我正在使用的堆栈的一部分是:
- GlassFish 4.0
- 球衣 2.0
- 休眠 5.1.0.Final
- 甲骨文jdbc12.1.0.2.0
目前我收到 IllegalStateException: This web container has not yet been started
尝试向服务发送 任何 有意义的 http 请求:
StandardWrapperValve[com.my.lab.web.BookStoreApplication]: Servlet.service() for servlet com.my.lab.web.BookStoreApplication threw exception java.lang.IllegalStateException: This web container has not yet been started
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1652)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
at oracle.jdbc.driver.T4CDriverExtension.allocatePreparedStatement(T4CDriverExtension.java:69)
at oracle.jdbc.driver.PhysicalConnection.prepareStatementInternal(PhysicalConnection.java:2013)
at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:1960)
at oracle.jdbc.OracleConnectionWrapper.prepareStatement(OracleConnectionWrapper.java:215)
at com.sun.gjc.spi.base.ConnectionHolder.prepareStatement(ConnectionHolder.java:586)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:255)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:52)
at com.sun.gjc.spi.ManagedConnectionImpl.prepareCachedStatement(ManagedConnectionImpl.java:992)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareStatement(ConnectionWrapper40.java:173)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.doPrepare(StatementPreparerImpl.java:87)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:72)
at org.hibernate.id.IncrementGenerator.initializePreviousValueHolder(IncrementGenerator.java:111)
at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:52)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:778)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:751)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:756)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:287)
at com.my.lab.dao.db.AbstractDbDao.saveEntity(AbstractDbDao.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at com.sun.proxy.$Proxy352.saveEntity(Unknown Source)
at com.my.lab.dao.db.__EJB31_Generated__DbBookDao__Intf____Bean__.saveEntity(Unknown Source)
at com.my.lab.web.resource.BookResource.saveNewBook(BookResource.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:125)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:91)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:346)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:341)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:101)
at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:224)
at org.glassfish.jersey.internal.Errors.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at com.my.lab.web.setting.swagger.SwaggerFilter.doFilter(SwaggerFilter.java:20)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)
实施:
到目前为止,我有一个资源 class,dao bean 作为 EJB 注入(您还可以在那里看到一些 swagger 注释):
@Path("/book")
@Produces({MediaType.APPLICATION_JSON})
@Api(value = "book")
public class BookResource {
@EJB
private DbBookDao bookDao;
@POST
@Consumes({MediaType.APPLICATION_JSON})
@ApiOperation(value = "Post a book", response = Book.class)
@ApiResponses(value = {
@ApiResponse(code = 409, message = "A book with such id is already exist"),
@ApiResponse(code = 500, message = "Internal Server Error")})
public Response saveNewBook(Book book) {
Integer id;
if (id = book.getId()) {
throw new WebApplicationException(String.format("A book with id %d is already exist", id),
Response.Status.CONFLICT);
}
return Response.status(Response.Status.CREATED).entity(bookDao.saveEntity(book)).build();
}
}
资源是这样注册的:
@ApplicationPath("/bookstore/api")
public class BookStoreApplication extends ResourceConfig {
public BookStoreApplication() {
initSwagger();
packages("com.my.lab","io.swagger.jaxrs.json", "io.swagger.jaxrs.listing");
}
private void initSwagger() {
BeanConfig swaggerConfig = new BeanConfig();
swaggerConfig.setSchemes(new String[]{"http", "https"});
// other BeanConfig methods
}
}
这是 dao bean class:
@Stateless
@LocalBean
public class DbBookDao extends AbstractDbDao<Book> {
@Override
protected Class<Book> getEntityClass() {
return Book.class;
}
}
扩展抽象 class,包含实体管理器:
public abstract class AbstractDbDao<T extends Entity> implements DAO<T> {
@PersistenceContext(unitName = "demo-persistence-unit")
private EntityManager entityManager;
protected abstract Class<T> getEntityClass();
@Override
public T saveEntity(T entity) {
entityManager.persist(entity);
return entity;
}
}
DAO 接口是一个简单的合约,没有任何注释。
最后是 Book json/hibernate 实体 class:
@javax.persistence.Entity
@Table(name = "BOOKS")
public class Book implements Entity {
@Id
@GeneratedValue(generator = "book_counter")
@GenericGenerator(name = "book_counter", strategy = "increment")
@Column(name = "BOOK_ID")
private Integer bookId;
@NotNull
@Column(name = "BOOK_NAME")
@Basic(optional = false)
private String name;
@ManyToOne
@JoinColumn(name = "AUTHOR", foreignKey = @ForeignKey(name = "AUTHOR_FK"))
private Author author;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "BOOK_GENRES",
joinColumns = @JoinColumn(name = "GENRE_ID"))
@Enumerated(EnumType.STRING)
private List<Genre> genres;
@Temporal(TemporalType.DATE)
private Date written;
public Book(String name) {
this.name = name;
}
public Book() {
// Default empty constructor for hibernate and JSON data binding
}
public Integer getId() {
return bookId;
}
public void setId(Integer id) {
this.bookId = id;
}
// other getters and setters
}
实现实体接口:
public interface Entity {
Integer getId();
void setId(Integer id);
}
配置:
以下是我的persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="demo-persistence-unit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/oracleDB</jta-data-source>
<class>com.my.lab.business.entity.Book</class>
<class>com.my.lab.business.entity.Author</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect"/>
<property name="hibernate.classloading.use_current_tccl_as_parent" value="false" />
<property name="hibernate.flushMode" value="FLUSH_AUTO" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name='hibernate.hbm2ddl.auto' value="create"/>
</properties>
</persistence-unit>
</persistence>
和web.xml:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>SwaggerFilter</filter-name>
<filter-class>com.my.lab.web.setting.swagger.SwaggerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SwaggerFilter</filter-name>
<url-pattern>/bookstore/api/*</url-pattern>
</filter-mapping>
</web-app>
应用程序部署顺利,所有需要的数据库表都已成功创建。但是在任何请求上,hibernate 生成的查询在这个地方执行失败:
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1652)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
at oracle.jdbc.driver.T4CDriverExtension.allocatePreparedStatement(T4CDriverExtension.java:69)
为了避免这种情况,我添加到 persistence.xml
<property name="hibernate.classloading.use_current_tccl_as_parent" value="false" />
这没什么帮助。
此外,我所有的代码:资源、实体和 dao bean,以及所有 xml 配置都打包在一个 war 中。此外,此war包含WEB-INF/lib下的jdbs.jar。我试着把它添加到glassfish/domains/domain1/lib/databases,这似乎没有任何区别。
据我所知,问题出在 GlassFish class 加载过程中,尽管我可能错了。我完全被困在这里,非常感谢解决这个问题的任何帮助。
我设法通过将休眠版本降级到 4.3.11.Final
来绕过这个问题,因此我不得不将 hibernate.dialect
更改为 org.hibernate.dialect.Oracle10gDialect
。
之后我遇到了一个问题,将新数据持久化到旧表,删除表修复了它(有一些数据类型不匹配,可能是方言差异造成的)。
问题:
我正在尝试实现一个简单的 jax-rs 服务,该服务将持久化作为 json 传递的实体。
我正在使用的堆栈的一部分是:
- GlassFish 4.0
- 球衣 2.0
- 休眠 5.1.0.Final
- 甲骨文jdbc12.1.0.2.0
目前我收到 IllegalStateException: This web container has not yet been started
尝试向服务发送 任何 有意义的 http 请求:
StandardWrapperValve[com.my.lab.web.BookStoreApplication]: Servlet.service() for servlet com.my.lab.web.BookStoreApplication threw exception java.lang.IllegalStateException: This web container has not yet been started
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1652)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
at oracle.jdbc.driver.T4CDriverExtension.allocatePreparedStatement(T4CDriverExtension.java:69)
at oracle.jdbc.driver.PhysicalConnection.prepareStatementInternal(PhysicalConnection.java:2013)
at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:1960)
at oracle.jdbc.OracleConnectionWrapper.prepareStatement(OracleConnectionWrapper.java:215)
at com.sun.gjc.spi.base.ConnectionHolder.prepareStatement(ConnectionHolder.java:586)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:255)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:52)
at com.sun.gjc.spi.ManagedConnectionImpl.prepareCachedStatement(ManagedConnectionImpl.java:992)
at com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareStatement(ConnectionWrapper40.java:173)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.doPrepare(StatementPreparerImpl.java:87)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:72)
at org.hibernate.id.IncrementGenerator.initializePreviousValueHolder(IncrementGenerator.java:111)
at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:52)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:778)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:751)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:756)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:287)
at com.my.lab.dao.db.AbstractDbDao.saveEntity(AbstractDbDao.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at com.sun.proxy.$Proxy352.saveEntity(Unknown Source)
at com.my.lab.dao.db.__EJB31_Generated__DbBookDao__Intf____Bean__.saveEntity(Unknown Source)
at com.my.lab.web.resource.BookResource.saveNewBook(BookResource.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:125)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:91)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:346)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:341)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:101)
at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:224)
at org.glassfish.jersey.internal.Errors.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at com.my.lab.web.setting.swagger.SwaggerFilter.doFilter(SwaggerFilter.java:20)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)
实施:
到目前为止,我有一个资源 class,dao bean 作为 EJB 注入(您还可以在那里看到一些 swagger 注释):
@Path("/book")
@Produces({MediaType.APPLICATION_JSON})
@Api(value = "book")
public class BookResource {
@EJB
private DbBookDao bookDao;
@POST
@Consumes({MediaType.APPLICATION_JSON})
@ApiOperation(value = "Post a book", response = Book.class)
@ApiResponses(value = {
@ApiResponse(code = 409, message = "A book with such id is already exist"),
@ApiResponse(code = 500, message = "Internal Server Error")})
public Response saveNewBook(Book book) {
Integer id;
if (id = book.getId()) {
throw new WebApplicationException(String.format("A book with id %d is already exist", id),
Response.Status.CONFLICT);
}
return Response.status(Response.Status.CREATED).entity(bookDao.saveEntity(book)).build();
}
}
资源是这样注册的:
@ApplicationPath("/bookstore/api")
public class BookStoreApplication extends ResourceConfig {
public BookStoreApplication() {
initSwagger();
packages("com.my.lab","io.swagger.jaxrs.json", "io.swagger.jaxrs.listing");
}
private void initSwagger() {
BeanConfig swaggerConfig = new BeanConfig();
swaggerConfig.setSchemes(new String[]{"http", "https"});
// other BeanConfig methods
}
}
这是 dao bean class:
@Stateless
@LocalBean
public class DbBookDao extends AbstractDbDao<Book> {
@Override
protected Class<Book> getEntityClass() {
return Book.class;
}
}
扩展抽象 class,包含实体管理器:
public abstract class AbstractDbDao<T extends Entity> implements DAO<T> {
@PersistenceContext(unitName = "demo-persistence-unit")
private EntityManager entityManager;
protected abstract Class<T> getEntityClass();
@Override
public T saveEntity(T entity) {
entityManager.persist(entity);
return entity;
}
}
DAO 接口是一个简单的合约,没有任何注释。
最后是 Book json/hibernate 实体 class:
@javax.persistence.Entity
@Table(name = "BOOKS")
public class Book implements Entity {
@Id
@GeneratedValue(generator = "book_counter")
@GenericGenerator(name = "book_counter", strategy = "increment")
@Column(name = "BOOK_ID")
private Integer bookId;
@NotNull
@Column(name = "BOOK_NAME")
@Basic(optional = false)
private String name;
@ManyToOne
@JoinColumn(name = "AUTHOR", foreignKey = @ForeignKey(name = "AUTHOR_FK"))
private Author author;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "BOOK_GENRES",
joinColumns = @JoinColumn(name = "GENRE_ID"))
@Enumerated(EnumType.STRING)
private List<Genre> genres;
@Temporal(TemporalType.DATE)
private Date written;
public Book(String name) {
this.name = name;
}
public Book() {
// Default empty constructor for hibernate and JSON data binding
}
public Integer getId() {
return bookId;
}
public void setId(Integer id) {
this.bookId = id;
}
// other getters and setters
}
实现实体接口:
public interface Entity {
Integer getId();
void setId(Integer id);
}
配置:
以下是我的persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="demo-persistence-unit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/oracleDB</jta-data-source>
<class>com.my.lab.business.entity.Book</class>
<class>com.my.lab.business.entity.Author</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect"/>
<property name="hibernate.classloading.use_current_tccl_as_parent" value="false" />
<property name="hibernate.flushMode" value="FLUSH_AUTO" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name='hibernate.hbm2ddl.auto' value="create"/>
</properties>
</persistence-unit>
</persistence>
和web.xml:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>SwaggerFilter</filter-name>
<filter-class>com.my.lab.web.setting.swagger.SwaggerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SwaggerFilter</filter-name>
<url-pattern>/bookstore/api/*</url-pattern>
</filter-mapping>
</web-app>
应用程序部署顺利,所有需要的数据库表都已成功创建。但是在任何请求上,hibernate 生成的查询在这个地方执行失败:
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1652)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
at oracle.jdbc.driver.T4CDriverExtension.allocatePreparedStatement(T4CDriverExtension.java:69)
为了避免这种情况,我添加到 persistence.xml
<property name="hibernate.classloading.use_current_tccl_as_parent" value="false" />
这没什么帮助。
此外,我所有的代码:资源、实体和 dao bean,以及所有 xml 配置都打包在一个 war 中。此外,此war包含WEB-INF/lib下的jdbs.jar。我试着把它添加到glassfish/domains/domain1/lib/databases,这似乎没有任何区别。
据我所知,问题出在 GlassFish class 加载过程中,尽管我可能错了。我完全被困在这里,非常感谢解决这个问题的任何帮助。
我设法通过将休眠版本降级到 4.3.11.Final
来绕过这个问题,因此我不得不将 hibernate.dialect
更改为 org.hibernate.dialect.Oracle10gDialect
。
之后我遇到了一个问题,将新数据持久化到旧表,删除表修复了它(有一些数据类型不匹配,可能是方言差异造成的)。