users@glassfish.java.net

Re: NB 6.9, GF 3.0.1. Newbe with Web Service

From: <glassfish_at_javadesktop.org>
Date: Wed, 04 Aug 2010 07:32:08 PDT

I have been thinking in the need to do persistent num1 and num2
and at the end I felt in a database as the solution.

I have created a schema with a table Numbers with two fields
num1 and num2 and the PK autoincrement as well. I have inserted
a row with values num1=0, num2=0 as default.

I generated the entity from database and the service leaves as follows,

@WebService()
@Stateless()
public class AddNumbers {

    @PersistenceContext(unitName = "AddingNumbersServicePU")
    protected EntityManager em;

    /**
     * Web service operation
     */
    @WebMethod(operationName = "setNum1")
    public void setNum1(@WebParam(name = "num1")
    int num1) {
        //TODO write your implementation code here:
        Numbers num = em.find(Numbers.class, 1);
        if (num == null)
            throw new EJBException();

        num.setNum1(num1);
        em.persist(num);
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "setNum2")
    public void setNum2(@WebParam(name = "num2")
    int num2) {
        //TODO write your implementation code here:
                Numbers num = em.find(Numbers.class, 1);
        if (num == null)
            throw new EJBException();

        num.setNum2(num2);
        em.persist(num);
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "addNumbers")
    public int addNumbers() {
        //TODO write your implementation code here:
                Numbers num = em.find(Numbers.class, 1);
        if (num == null)
            throw new EJBException();

        return num.getNum1() + num.getNum2();
    }

}

Now the client works fine.

Regards,
Jose
[Message sent by forum member 'josealvarezdelara']

http://forums.java.net/jive/thread.jspa?messageID=479338