3 Replies Latest reply on Feb 4, 2005 12:58 AM by starksm64

    CMP EJB findall returns a Proxy object

    pvanonselen

      I am working with XDoclet and Eclipse 3.1 on a JBoss 4.1 server.

      I have created a CMP Entity Bean that deploys fine and I can findByPrimaryKey with success but when I do a findAll I get a collection of Proxy objects instead of a Remote object.

      I am doing something wrong? Here is my Bean Code:

      /*
       * Created on Jan 21, 2005
       *
       * TODO To change the template for this generated file go to
       * Window - Preferences - Java - Code Style - Code Templates
       */
      package engineparts.creditors.cmp.ejb;
      
      
      
      
      /** Represents the company table.
       * This is the company that a placement is made for
       *
       * @ejb.bean
       * type="CMP"
       * cmp-version="2.x"
       * name="CreditorBean"
       * jndi-name="ejb/remote/CreditorBean"
       * local-jndi-name="ejb/local/CreditorBean"
       * view-type="both"
       * primkey-field="accountCode"
       * schema="crCreditorSchema"
       *
       * @jboss.container-configuration name="Standard CMP 2.x EntityBean"
       *
       * @ejb.resource-ref
       * res-ref-name="jdbc/epTest"
       * res-type="javax.sql.DataSource"
       * res-auth="Container"
       * res-sharing-scope="Shareable"
       * jndi-name="epTest"
       *
       * @ejb.finder
       * signature="java.util.Collection findAll()"
       * method-intf="Home"
       * query="SELECT OBJECT(o) FROM crCreditorSchema o"
       *
       * @ejb.finder
       * signature="java.util.Collection findByName(java.lang.String companyName)"
       * method-intf="Home"
       * query="SELECT DISTINCT OBJECT(o) FROM crCreditorSchema o WHERE o.companyName like ?1"
       * description="name is not indexed."
       *
       * @ejb.finder
       * signature="java.util.Collection findByAccountCode(java.lang.String accountCode)"
       * method-intf="Home"
       * query="SELECT OBJECT(o) FROM crCreditorSchema o WHERE o.accountCode = ?1"
       * description="name is not indexed."
       *
       * @ejb.value-object
       *
       *
       * @ejb.persistence
       * table-name="crCreditor"
       *
       * JBoss specific
       *
       * @jboss.persistence create-table="false"
       * remove-table="false"
       *
       *
       */
      public abstract class CreditorBean implements javax.ejb.EntityBean {
       /**
       * Sets the companyId
       *
       * @param java.lang.String the new companyId value
       */
       public abstract void setAccountCode(java.lang.String accountCode);
      
       /**
       * Returns the companyId
       *
       * @return the companyId
       *
       * @ejb.pk-field
       * @ejb.interface-method view-type="both"
       * @ejb.persistence column-name="AccCode"
       */
       public abstract java.lang.String getAccountCode();
      
       /**
       * Sets the addressLine1
       *
       * @param java.lang.String the new addressLine1 value
       * @ejb.interface-method view-type="both"
       */
       public abstract void setAddressLine1(java.lang.String addressLine1);
      
       /**
       * Returns the addressLine1
       *
       * @return the addressLine1
       *
       * @ejb.interface-method view-type="both"
       * @ejb.persistence column-name="PostAddr1"
       */
       public abstract java.lang.String getAddressLine1();
      
       /**
       * Sets the addressLine2
       *
       * @param java.lang.String the new addressLine2 value
       * @ejb.interface-method view-type="both"
       */
       public abstract void setAddressLine2(java.lang.String addressLine2);
      
       /**
       * Returns the addressLine2
       *
       * @return the addressLine2
       *
       * @ejb.interface-method view-type="both"
       * @ejb.persistence column-name="PostAddr2"
       */
       public abstract java.lang.String getAddressLine2();
      
       /**
       * Sets the addressLine3
       *
       * @param java.lang.String the new addressLine3 value
       * @ejb.interface-method view-type="both"
       */
       public abstract void setAddressLine3(java.lang.String addressLine3);
      
       /**
       * Returns the addressLine3
       *
       * @return the addressLine3
       *
       * @ejb.interface-method view-type="both"
       * @ejb.persistence column-name="PostAddr3"
       */
       public abstract java.lang.String getAddressLine3();
      
       /**
       * Sets the name
       *
       * @param java.lang.String the new name value
       * @ejb.interface-method view-type="both"
       */
       public abstract void setCompanyName(java.lang.String companyName);
      
       /**
       * Returns the name
       *
       * @return the name
       *
       * @ejb.interface-method view-type="both"
       * @ejb.persistence column-name="CoName"
       */
       public abstract java.lang.String getCompanyName();
      
       /**
       * Sets the contactPerson
       *
       * @param java.lang.String the new contactPerson value
       * @ejb.interface-method view-type="both"
       */
       public abstract void setContactPerson(java.lang.String contactPerson);
      
       /**
       * Returns the contactPerson
       *
       * @return the contactPerson
       *
       * @ejb.interface-method view-type="both"
       * @ejb.persistence column-name="ContactName"
       */
       public abstract java.lang.String getContactPerson();
      
       /**
       * @ejb.interface-method view-type="both"
       * @ejb.transaction type="Required"
       */
       public abstract void setCreditorEJBValue(engineparts.creditors.vo.CreditorBeanValue value);
      
       /**
       * @ejb.interface-method view-type="both"
       * @ejb.transaction type="Required"
       */
       public abstract engineparts.creditors.vo.CreditorBeanValue getCreditorEJBValue();
      
       /**
       * Sets the emailAddress
       *
       * @param java.lang.String the new emailAddress value
       * @ejb.interface-method view-type="both"
       */
       public abstract void setEmailAddress(java.lang.String emailAddress);
      
       /**
       * Returns the emailAddress
       *
       * @return the emailAddress
       *
       * @ejb.interface-method view-type="both"
       * @ejb.persistence column-name="EMail"
       */
       public abstract java.lang.String getEmailAddress();
      
       /**
       * Sets the postalCode
       *
       * @param java.lang.String the new postalCode value
       * @ejb.interface-method view-type="both"
       */
       public abstract void setPostalCode(java.lang.String postalCode);
      
       /**
       * Returns the postalCode
       *
       * @return the postalCode
       *
       * @ejb.interface-method view-type="both"
       * @ejb.persistence column-name="PostCd"
       */
       public abstract java.lang.String getPostalCode();
      
       /**
       * Sets the directPhone
       *
       * @param java.lang.String the new directPhone value
       * @ejb.interface-method view-type="both"
       */
       public abstract void setTelephone(java.lang.String directPhone);
      
       /**
       * Returns the directPhone
       *
       * @return the directPhone
       *
       * @ejb.interface-method view-type="both"
       * @ejb.persistence column-name="Tel"
       */
       public abstract java.lang.String getTelephone();
      
       /**
       * This create method takes all parameters (both nullable and not nullable).
       * @param value object representing the bean to be created
       * @return null (per CMP Spec 2)
       *
       * @ejb.create-method
       *
       */
       public java.lang.String ejbCreate(engineparts.creditors.vo.CreditorBeanValue value)
       throws javax.ejb.CreateException {
       // Use Middlegen's Sequence Block PK generator. Only works for numeric fields
       try {
       value.setAccountCode(this.getAccountCode());
       } catch (Exception e) {
       throw new javax.ejb.CreateException(e.getMessage());
       }
       return null;
       }
      
       /**
       * The container invokes this method immediately after it calls ejbCreate.
       *
       */
       public void ejbPostCreate(engineparts.creditors.vo.CreditorBeanValue value)
       throws javax.ejb.CreateException {
       // Set CMR fields
       }
      }
      </code>
      
      
      Here is my ant script:
      
      
      
       <ejbdoclet destdir="${generated.java.dir}" ejbspec="2.0" excludedtags="@version,@author" force="${xdoclet.force}">
       <fileset dir="${ejb.src.dir}">
       <include name="**/*Bean.java"/>
       <include name="**/*Task*.java"/>
       </fileset>
      
       <remoteinterface>
       <packageSubstitution packages="cmp.ejb" substituteWith="cmp.interfaces"/>
       <packageSubstitution packages="facade" substituteWith="facade.interfaces"/>
       </remoteinterface>
      
       <homeinterface>
       <packageSubstitution packages="cmp.ejb" substituteWith="cmp.interfaces"/>
       <packageSubstitution packages="facade" substituteWith="facade.interfaces"/>
       </homeinterface>
      
       <localinterface>
       <packageSubstitution packages="cmp.ejb" substituteWith="cmp.interfaces"/>
       </localinterface>
      
       <localhomeinterface>
       <packageSubstitution packages="cmp.ejb" substituteWith="cmp.interfaces"/>
       </localhomeinterface>
      
       <entitycmp>
       <packageSubstitution packages="cmp.ejb" substituteWith="cmp.ejb.impl"/>
       </entitycmp>
      
       <utilobject >
       <packageSubstitution packages="cmp.ejb" substituteWith="cmp.util"/>
       <packageSubstitution packages="facade" substituteWith="facade.util"/>
       </utilobject>
      
       <valueobject>
       <packageSubstitution packages="cmp.ejb" substituteWith="vo"/>
       </valueobject>
      
       <entityfacade >
       <packageSubstitution packages="cmp.ejb" substituteWith="facade"/>
       </entityfacade>
      
       <entitypk>
       <packageSubstitution packages="cmp.ejb" substituteWith="cmp.interfaces"/>
       </entitypk>
      
       <deploymentdescriptor destdir="${ejb.src.dd}"/>
       <jboss version="4.0" datasource="java:/eptest-ds"
       datasourcemapping="MS SQLSERVER"
       destdir="${ejb.src.dd}"
       xmlencoding="UTF-8"/>
      
      
       </ejbdoclet>
      
      
      
      And finally here is my CLient script:
      
       java.util.Collection creditorList= null;
       engineparts.creditors.cmp.interfaces.CreditorBeanLocal cr = null;
       String accountCode = null;
       String companyName = null;
       try {
      
       String jndiName="ejb/local/CreditorBean";
       java.util.Hashtable env = new java.util.Hashtable();
       env.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
       env.put("java.naming.provider.url", "jnp://192.6.1.122:1099");
       javax.naming.InitialContext context = new javax.naming.InitialContext(env);
       Object home = context.lookup(jndiName);
       Object narrow = javax.rmi.PortableRemoteObject.narrow(home, engineparts.creditors.cmp.interfaces.CreditorBeanLocalHome.class);
       engineparts.creditors.cmp.interfaces.CreditorBeanLocalHome creditorHome = (engineparts.creditors.cmp.interfaces.CreditorBeanLocalHome)narrow;
       System.out.println("CreditorEJBHome "+creditorHome);
      
       //engineparts.creditors.cmp.interfaces.CreditorBeanLocalHome creditorHome = engineparts.creditors.cmp.util.CreditorBeanUtil.getLocalHome();
      
       //creditorList = creditorHome.findByName("Eng%");
       accountCode = request.getParameter("accountCode");
       companyName = request.getParameter("companyName");
      
       System.out.println("acc "+accountCode+".");
      
       if(accountCode!= null && accountCode.length()>0){
       cr = creditorHome.findByPrimaryKey(accountCode);
       accountCode = cr.getAccountCode();
       companyName = cr.getCompanyName();
       }else{
       if(companyName==null || companyName.length() ==0)
       companyName = "Engine";
       creditorList = creditorHome.findByName("%"+companyName+"%");
       accountCode = "";
       companyName = "";
       System.out.println("Creditorlist "+creditorList);
       for (java.util.Iterator iter = creditorList.iterator(); iter.hasNext();) {
       //engineparts.creditors.cmp.interfaces.CreditorBean element = (engineparts.creditors.cmp.interfaces.CreditorBean) iter.next();
       System.out.println("Class "+iter.next().getClass().getName());
      
       }
       }
      
      


        • 1. Re: CMP EJB findall returns a Proxy object
          pvanonselen

          Sorry I am using JBoss 4.0.1

          • 2. Re: CMP EJB findall returns a Proxy object
            pvanonselen

            Solution is to use java.lang.reflect.Proxy like this:

            
            Proxy element = (Proxy) iter.next();
            InvocationHandler handler = Proxy.getInvocationHandler(element);
            CreditorBean bean = (CreditorBean) Proxy.newProxyInstance(CreditorBean.class.getClassLoader(),
             new Class[] { CreditorBean.class }, handler);
            



            Any reason why I need to do this?

            Other examples do not show this code

            • 3. Re: CMP EJB findall returns a Proxy object
              starksm64

              The class of the object in the collection is irrelevant. It implements the required interfaces as defined by the spec. Look at the interfaces it implements, not the class type.


              Some finder methods are designed to return multiple entity objects. For multi-object finders defined on
              the entity bean?s local home interface, the result type of the find<METHOD>(...)method is a collection
              of objects implementing the entity bean?s local interface. For multi-object finders defined on the
              entity bean?s remote home interface, the result type of the find<METHOD>(...)method is a collection
              of objects implementing the entity bean?s remote interface.