1

花了几个小时后,我终于决定发布我的问题:

作为 WebServices 的新手,在决定使用 netbeans 和 Glassfish 进行更复杂的测试后,我做了很多教程。...但最后我碰壁了:-)

我有 Netbeans IDE 创建的简单类:

    *********** My Entity ******

    @Entity
    @Table(name = "user")
    @XmlRootElement
    @NamedQueries({
        @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
        @NamedQuery(name = "User.findByUserId", query = "SELECT u FROM User u WHERE u.userId = :userId"),
        @NamedQuery(name = "User.findByLoginName", query = "SELECT u FROM User u WHERE u.loginName = :loginName"),
        @NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email"),
        @NamedQuery(name = "User.findByOpenID", query = "SELECT u FROM User u WHERE u.openID = :openID"),
        @NamedQuery(name = "User.findByCreated", query = "SELECT u FROM User u WHERE u.created = :created"),
        @NamedQuery(name = "User.findByStatus", query = "SELECT u FROM User u WHERE u.status = :status")})
    public class User implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "userId")
        private Long userId;
        @Column(name = "loginName")
        private String loginName;
        @Column(name = "email")
        private String email;
        @Column(name = "openID")
        private String openID;
        @Column(name = "created")
        @Temporal(TemporalType.TIMESTAMP)
        private Date created;
        @Column(name = "status")
        private String status;
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "useruserId")
        private Collection<Userextras> userextrasCollection;
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "useruserId")
        private Collection<Watch> watchCollection;
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "useruserId")
        private Collection<Billinginfo> billinginfoCollection;
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "useruserId")
        private Collection<Good> goodCollection;
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "useruserId")
        private Collection<Purchase> purchaseCollection;

        public User() {
        }

        public User(Long userId) {
            this.userId = userId;
        }

        public Long getUserId() {
            return userId;
        }

        public void setUserId(Long userId) {
            this.userId = userId;
        }

        public String getLoginName() {
            return loginName;
        }

        public void setLoginName(String loginName) {
            this.loginName = loginName;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getOpenID() {
            return openID;
        }

        public void setOpenID(String openID) {
            this.openID = openID;
        }

        public Date getCreated() {
            return created;
        }

        public void setCreated(Date created) {
            this.created = created;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        @XmlTransient
        public Collection<Userextras> getUserextrasCollection() {
            return userextrasCollection;
        }

        public void setUserextrasCollection(Collection<Userextras> userextrasCollection) {
            this.userextrasCollection = userextrasCollection;
        }

        @XmlTransient
        public Collection<Watch> getWatchCollection() {
            return watchCollection;
        }

        public void setWatchCollection(Collection<Watch> watchCollection) {
            this.watchCollection = watchCollection;
        }

        @XmlTransient
        public Collection<Billinginfo> getBillinginfoCollection() {
            return billinginfoCollection;
        }

        public void setBillinginfoCollection(Collection<Billinginfo> billinginfoCollection) {
            this.billinginfoCollection = billinginfoCollection;
        }

        @XmlTransient
        public Collection<Good> getGoodCollection() {
            return goodCollection;
        }

        public void setGoodCollection(Collection<Good> goodCollection) {
            this.goodCollection = goodCollection;
        }

        @XmlTransient
        public Collection<Purchase> getPurchaseCollection() {
            return purchaseCollection;
        }

        public void setPurchaseCollection(Collection<Purchase> purchaseCollection) {
            this.purchaseCollection = purchaseCollection;
        }

        @Override
        public int hashCode() {
            int hash = 0;
            hash += (userId != null ? userId.hashCode() : 0);
            return hash;
        }

        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof User)) {
                return false;
            }
            User other = (User) object;
            if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
                return false;
            }
            return true;
        }
    }



    ****** My EJB facade that implements Abstract facade. Created from Entity class *******

    @Stateless
    public class UserFacade extends AbstractFacade<User> implements UserFacadeRemote {
        @PersistenceContext(unitName = "AppEE-ejbPU")
        private EntityManager em;

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

        public UserFacade() {
            super(User.class);
        }

    }



    ************* Remote interface create along with EJBs ****

    @Remote
    public interface UserFacadeRemote {

        void create(User user);

        void edit(User user);

        void remove(User user);

        User find(Object id);

        List<User> findAll();

        List<User> findRange(int[] range);

        int count();

    }


******* A web service Created from EJB *******

@WebService(serviceName = "UserService")
@Stateless()
public class UserService {
    @EJB
    private UserFacadeRemote ejbRef;// Add business logic below. 

    @WebMethod(operationName = "create")
    @Oneway
    public void create(@WebParam(name = "user") User user) {
        System.out.println("User received WS:" + user); **<<-- user is null @ this point**
        ejbRef.create(user); **<<--exception is thrown here**
    }

    @WebMethod(operationName = "edit")
    @Oneway
    public void edit(@WebParam(name = "user") User user) {
        ejbRef.edit(user);
    }

    @WebMethod(operationName = "remove")
    @Oneway
    public void remove(@WebParam(name = "user") User user) {
        ejbRef.remove(user);
    }

    @WebMethod(operationName = "find")
    public User find(@WebParam(name = "id") Object id) {
        return ejbRef.find(id);
    }

    @WebMethod(operationName = "findAll")
    public List<User> findAll() {
        return ejbRef.findAll();
    }

    @WebMethod(operationName = "findRange")
    public List<User> findRange(@WebParam(name = "range") int[] range) {
        return ejbRef.findRange(range);
    }

    @WebMethod(operationName = "count")
    public int count() {
        return ejbRef.count();
    }

}

**** And Finally A Very very simple WS Desktop Client  *****


public static void main(String[] args) {
        System.out.println("Count" + count());   // <<<- works great!
        User newUser = new User();
        newUser.setLoginName("Dummy login WS");
        newUser.setStatus("WS");
        **create(newUser);                     // <<<----Causes the exception**
        newUser = find(new Long(1L));
        System.out.println("Find User  1: " + newUser.getLoginName());  // <<<- works great!

        System.out.println("Done..");
    }

    private static int count() {
        com.wixia.webservices.UserService_Service service = new com.wixia.webservices.UserService_Service();
        com.wixia.webservices.UserService port = service.getUserServicePort();
        return port.count();
    }

    private static void create(com.wixia.webservices.User user) {
        com.wixia.webservices.UserService_Service service = new com.wixia.webservices.UserService_Service();
        com.wixia.webservices.UserService port = service.getUserServicePort();
        port.create(user);
    }

    private static User find(java.lang.Object id) {
        com.wixia.webservices.UserService_Service service = new com.wixia.webservices.UserService_Service();
        com.wixia.webservices.UserService port = service.getUserServicePort();
        return port.find(id);
    }
}



********** Relevant part of the stacktrace ***********


WARNING:   EJB5184:A system exception occurred during an invocation on EJB UserFacade, method: public void com.wixia.beans.entities.UserFacade.create(com.wixia.model.entities.User)
WARNING:   javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean
    at com.sun.ejb.containers.EJBContainerTransactionManager.checkExceptionClientTx(EJBContainerTransactionManager.java:662)
    at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:507)
    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.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:212)
    at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
    at com.sun.proxy.$Proxy532.create(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:239)
    at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:150)
    at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:226)
    at com.wixia.beans.entities.__UserFacadeRemote_Remote_DynamicStub.create(com/wixia/beans/entities/__UserFacadeRemote_Remote_DynamicStub.java)
    at com.wixia.beans.entities._UserFacadeRemote_Wrapper.create(com/wixia/beans/entities/_UserFacadeRemote_Wrapper.java)
    at com.wixia.webservices.UserService.create(UserService.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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.WebServiceInvocationHandler.invoke(WebServiceInvocationHandler.java:193)
    at com.sun.proxy.$Proxy533.create(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.webservices.InvokerImpl.invoke(InvokerImpl.java:82)
    at org.glassfish.webservices.EjbInvokerImpl.invoke(EjbInvokerImpl.java:82)
    at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:149)
    at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:88)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1136)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1050)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:1019)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:877)
    at com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl.process(AbstractTubeImpl.java:136)
    at org.glassfish.webservices.MonitoringPipe.process(MonitoringPipe.java:142)
    at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:119)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1136)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1050)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:1019)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:877)
    at com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl.process(AbstractTubeImpl.java:136)
    at com.sun.enterprise.security.webservices.CommonServerSecurityPipe.processRequest(CommonServerSecurityPipe.java:210)
    at com.sun.enterprise.security.webservices.CommonServerSecurityPipe.process(CommonServerSecurityPipe.java:142)
    at com.sun.xml.ws.api.pipe.helper.PipeAdapter.processRequest(PipeAdapter.java:119)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1136)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1050)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:1019)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:877)
    at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:420)
    at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:687)
    at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:266)
    at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:169)
    at org.glassfish.webservices.Ejb3MessageDispatcher.handlePost(Ejb3MessageDispatcher.java:110)
    at org.glassfish.webservices.Ejb3MessageDispatcher.invoke(Ejb3MessageDispatcher.java:80)
    at org.glassfish.webservices.EjbWebServiceServlet.dispatchToEjbEndpoint(EjbWebServiceServlet.java:203)
    at org.glassfish.webservices.EjbWebServiceServlet.service(EjbWebServiceServlet.java:146)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:242)
    at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:193)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)
    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:724)
Caused by: java.lang.IllegalArgumentException: Object: null is not a known entity type.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:493)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:287)
    at com.wixia.beans.entities.AbstractFacade.create(AbstractFacade.java:25)
    at com.wixia.beans.entities.UserFacade.create(UserFacade.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:205)
    ... 99 more

** * ** * ***问题** * ** * ** * ** * **

当我调用 create 方法时,接收到的用户为空!使用:Glassfish 4.0 MySQL 5.6.13

希望我说清楚了,任何人都可以帮助我了解发生了什么。

谢谢!

何塞

4

0 回答 0