JEE,eclipselink,glassfish4.0不能持久化实体

时间:2015-10-23 17:20:13

标签: java hibernate jpa eclipselink glassfish-4

我正在开发的休息应用程序有一个奇怪的问题。

我创建了实体类以及dao类来管理它们。现在,当我将应用程序部署到服务器时,我可以看到我的数据库架构正在更新。现在我有了这个:


    @Stateless
    @Produces("text/plain")
    @Path("cc")
    public class HelloService {

        @EJB
        private DomainDao dao;

        @GET
        @Path("/put")
        public String put() {

            Domain d = new Domain();
            d.setName("name");
            d.setActive(true);
            d.setCreationDate(new Date());
            d.setLastModificationDate(new Date());

            dao.create(d);

            return "completed";
        }

        @GET
        @Path("/get")
        public String get() {
            return dao.find(1l).getName();
        }

        @GET
        @Path("/hello")
        public String message() {
            return "hi";
        }
    }

部署后,我手动在表格中插入一行。当我通过浏览器调用get()方法时,我可以看到它正常工作,因为显示了正确的信息。 但是,当我调用put()方法时,我得到一个异常。

[2015-10-23T18:53:47.137+0200] [glassfish 4.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=24 _ThreadName=http-listener-1(5)] [timeMillis: 1445619227137] [levelValue: 900] [[
  StandardWrapperValve[pl.kacperb333.restApp.MainApp]: Servlet.service() for servlet pl.kacperb333.restApp.MainApp threw exception
javax.transaction.xa.XAException: java.lang.IllegalStateException: This web container has not yet been started
    at com.sun.enterprise.resource.ConnectorXAResource.handleResourceException(ConnectorXAResource.java:115)
    at com.sun.enterprise.resource.ConnectorXAResource.rollback(ConnectorXAResource.java:215)
    at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:488)
    at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:854)
    at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:719)
    at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503)
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4475)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2009)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1979)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:220)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at com.sun.proxy.$Proxy315.test(Unknown Source)
    at pl.kacperb333.restApp.webservices.__EJB31_Generated__HelloService__Intf____Bean__.test(Unknown Source)
    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$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:125)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
    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$1.run(ServerRuntime.java:224)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at 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)
    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.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    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$9.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.access$100(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)
]]

我现在已经在这个问题上工作了2天,而且我没有想法。

以下是实体和dao类:

AbstractEntity:


    @MappedSuperclass
    public abstract class AbstractEntity implements IAbstractEntity {

        private Boolean active;

        @Temporal(TemporalType.DATE)
        private Date creationDate;
        @Temporal(TemporalType.DATE)
        private Date lastModificationDate;

        @Override
        public Boolean getActive() {
            return active;
        }

        @Override
        public void setActive(Boolean active) {
            this.active = active;
        }

        @Override
        public Date getCreationDate() {
            return creationDate;
        }

        @Override
        public void setCreationDate(Date creationDate) {
            this.creationDate = creationDate;
        }

        @Override
        public Date getLastModificationDate() {
            return lastModificationDate;
        }

        @Override
        public void setLastModificationDate(Date lastModificationDate) {
            this.lastModificationDate = lastModificationDate;
        }
    }

实体:


    @Entity
    public class Domain extends AbstractEntity implements IDomain {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long domainId;

        @ManyToMany(targetEntity = Company.class, mappedBy = "domains")
        private Set companies;

        @OneToMany(targetEntity = Offer.class)
        @JoinColumn(name = "offerId")
        private List offers;

        private String name;

        @Override
        public Long getDomainId() {
            return domainId;
        }

        @Override
        public void setDomainId(Long domainId) {
            this.domainId = domainId;
        }

        @Override
        public Set getCompanies() {
            return companies;
        }

        @Override
        public void setCompanies(Set companies) {
            this.companies = companies;
        }

        @Override
        public List getOffers() {
            return offers;
        }

        @Override
        public void setOffers(List offers) {
            this.offers = offers;
        }

        @Override
        public String getName() {
            return name;
        }

        @Override
        public void setName(String name) {
            this.name = name;
        }
    }

摘要道:


    public abstract class AbstractDao {

        private Class entityClass;

        public AbstractDao(Class entityClass) {
            this.entityClass = entityClass;
        }

        protected abstract EntityManager getEntityManager();

        public void create(T entity) {
            getEntityManager().persist(entity);
        }

        public void edit(T entity) {
            getEntityManager().merge(entity);
        }

        public void remove(T entity) {
            getEntityManager().remove(getEntityManager().merge(entity));
        }

        public T find(Object id) {
            return getEntityManager().find(entityClass, id);
        }

        public List findAll() {
            javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
            cq.select(cq.from(entityClass));
            return getEntityManager().createQuery(cq).getResultList();
        }

        public List findRange(int[] range) {
            javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
            cq.select(cq.from(entityClass));
            javax.persistence.Query q = getEntityManager().createQuery(cq);
            q.setMaxResults(range[1] - range[0] + 1);
            q.setFirstResult(range[0]);
            return q.getResultList();
        }

        public int count() {
            javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
            javax.persistence.criteria.Root rt = cq.from(entityClass);
            cq.select(getEntityManager().getCriteriaBuilder().count(rt));
            javax.persistence.Query q = getEntityManager().createQuery(cq);
            `enter code here`return ((Long) q.getSingleResult()).intValue();
        }
    }

DomainDao:


    @Stateless
    @LocalBean
    public class DomainDao extends AbstractDao {

        @PersistenceContext(unitName = "rekruterPU")
        private EntityManager em;

        @Override
        protected EntityManager getEntityManager() {
            return em;
        }

        public DomainDao() {
            super(Domain.class);
        }
    }

我认为问题不在于实体或dao类,因为至少有一个EntityManager(find())方法可以工作。我很感激和帮助。

1 个答案:

答案 0 :(得分:0)

正如@ kacperb333所建议的那样,标准持久性属性是EclipseLink中此错误的原因。

不要使用:

<property name="javax.persistence.schema-generation.database.action" value="create"/>

改为使用:

<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>