1 Reply Latest reply on Oct 19, 2004 2:47 PM by kabirkhan

    get/set methods in bean ?

    tjclifford

      Forgive me if this is covered in another spot.....I have searched
      the forums, but did not find an article that covers this.

      I'm trying to implement Brett McLaughlin's multiple-ejb
      database system, with his Office, User, Fund, etc beans, and
      have tried implementing them one at a time.
      Got Office to work, now trying to set up User access.

      UserBean.java:
      --------------------------------------------

      package com.sample.ejb.user;

      import java.rmi.RemoteException;
      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.FinderException;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.rmi.PortableRemoteObject;

      import com.sample.ejb.util.EntityAdapter;

      // Sequence Bean
      import com.sample.ejb.sequence.SequenceException;
      import com.sample.ejb.sequence.SequenceLocal;
      import com.sample.ejb.sequence.SequenceLocalHome;

      // Office Bean
      import com.sample.ejb.office.Office;
      import com.sample.ejb.office.OfficeHome;
      import com.sample.ejb.office.OfficeInfo;
      import com.sample.ejb.office.OfficeLocal;
      import com.sample.ejb.office.OfficeLocalHome;

      // UserType Bean
      import com.sample.ejb.userType.UnknownUserTypeException;
      import com.sample.ejb.userType.UserTypeLocal;
      import com.sample.ejb.userType.UserTypeLocalHome;

      public abstract class UserBean extends EntityAdapter {

      public Integer ejbCreate(String userDn, String type,
      String firstName, String lastName, Office office)
      throws CreateException, UnknownUserTypeException {

      // Get the next primary key value
      try {
      Context context = new InitialContext(System.getProperties());
      // Note that RMI-IIOP narrowing is not required
      SequenceLocalHome home = (SequenceLocalHome)
      //context.lookup("java:comp/env/ejb/SequenceLocalHome");
      context.lookup("local/SequenceBean");
      SequenceLocal sequence = home.create();
      String userKey =
      (String)context.lookup("java:comp/env/constants/UserKey");
      Integer id = sequence.getNextValue(userKey);

      // Set values
      setId(id);
      setUserDn(userDn);
      setFirstName(firstName);
      setLastName(lastName);

      return null;
      } catch (NamingException e) {
      throw new CreateException("Could not obtain an " +
      "InitialContext: " + e.getMessage());
      } catch (SequenceException e) {
      throw new CreateException("Error getting primary key value: " +
      e.getMessage());
      }
      }

      public void ejbPostCreate(String userDn, String type,
      String firstName, String lastName,
      Office office)
      throws CreateException, UnknownUserTypeException {

      // Handle CMP relationships
      setOffice(office);
      setType(type);
      }

      public UserInfo getInfo() throws RemoteException {
      OfficeInfo officeInfo = null;
      Office office = getOffice();
      if (office != null) {
      officeInfo = office.getInfo();
      }

      UserInfo userInfo =
      new UserInfo(getId().intValue(), getUserDn(),
      getUserTypeLocal().getType(),
      getFirstName(), getLastName(),
      officeInfo);

      return userInfo;
      }

      public void setInfo(UserInfo userInfo) throws UnknownUserTypeException {
      setUserDn(userInfo.getUserDn());
      setFirstName(userInfo.getFirstName());
      setLastName(userInfo.getLastName());
      setType(userInfo.getType());
      }

      public String getType() {
      return getUserTypeLocal().getType();
      }

      public void setType(String type) throws UnknownUserTypeException {
      try {
      Context context = new InitialContext();
      UserTypeLocalHome userTypeLocalHome =
      (UserTypeLocalHome)context.lookup(
      "java:comp/env/ejb/UserTypeLocalHome");
      UserTypeLocal userTypeLocal =
      userTypeLocalHome.findByType(type);
      setUserTypeLocal(userTypeLocal);
      } catch (NamingException e) {
      throw new EJBException("Error looking up UserType bean: " +
      e.getMessage());
      } catch (FinderException e) {
      // Couldn't find supplied type
      throw new UnknownUserTypeException(type);
      }

      public Office getOffice() {
      OfficeLocal officeLocal = getOfficeLocal();
      if (officeLocal == null) {
      return null;
      }

      // Construct primary key for this office
      Integer officeID = getOfficeLocal().getId();

      try {
      // Find the remote interface for this office
      Context context = new InitialContext();
      OfficeHome officeHome =
      (OfficeHome)context.lookup(
      "java:comp/env/ejb/OfficeHome");
      Office office = officeHome.findByPrimaryKey(officeID);
      return office;
      } catch (NamingException e) {
      throw new EJBException("Error looking up Office bean: " +
      e.getMessage());
      } catch (RemoteException e) {
      throw new EJBException("Error looking up Office bean: " +
      e.getMessage());
      } catch (FinderException shouldNeverHappen) {
      // This should never happen; the ID from an office's remote
      // interface should match an office's ID in a local interface
      throw new EJBException("Error matching remote Office to " +
      "local Office: " + shouldNeverHappen.getMessage());
      }
      }

      public void setOffice(Office office) {
      try {
      // Handle case where no office supplied
      if (office == null) {
      setOfficeLocal(null);
      return;
      }

      // Construct primary key for this office
      Integer officeID = office.getId();

      // Find the local interface for this office
      Context context = new InitialContext();
      OfficeLocalHome officeLocalHome =
      (OfficeLocalHome)context.lookup(
      "java:comp/env/ejb/OfficeLocalHome");
      OfficeLocal officeLocal =
      officeLocalHome.findByPrimaryKey(officeID);
      setOfficeLocal(officeLocal);
      } catch (NamingException e) {
      throw new EJBException("Error looking up Office bean: " +
      e.getMessage());
      } catch (RemoteException e) {
      throw new EJBException("Error looking up Office bean: " +
      e.getMessage());
      } catch (FinderException shouldNeverHappen) {
      // This should never happen; the ID from an office's remote
      // interface should match an office's ID in a local interface
      throw new EJBException("Error matching remote Office to " +
      "local Office: " + shouldNeverHappen.getMessage());
      }
      }

      public abstract Integer getId();
      public abstract void setId(Integer id);

      public abstract String getUserDn();
      public abstract void setUserDn(String userDn);

      public abstract UserTypeLocal getUserTypeLocal();
      public abstract void setUserTypeLocal(UserTypeLocal userTypeLocal);

      public abstract OfficeLocal getOfficeLocal();
      public abstract void setOfficeLocal(OfficeLocal officeLocal);

      public abstract String getFirstName();
      public abstract void setFirstName(String firstName);

      public abstract String getLastName();
      public abstract void setLastName(String lastName);

      }

      }

      --------------------------------

      This will compile, but when I deploy the jar file, it tells me
      that :

      10:18:08,280 ERROR [EntityContainer] Starting failed jboss.j2ee:jndiName=OfficeB
      ean,service=EJB
      org.jboss.deployment.DeploymentException: No abstract accessors for field named
      'type' found in entity class com.sample.ejb.user.UserBean
      at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData.loadFiel
      dType(JDBCCMPFieldMetaData.java:857)
      at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData.(J
      DBCCMPFieldMetaData.java:161)
      at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityMetaData.(JDB
      CEntityMetaData.java:375)
      ........more.........

      Don't the setType and getType methods belong in the bean if
      they are going to actually do something ? I'm still a bit
      confused in what belongs where, and this seems strange
      that you would need to define the methods as abstract in
      the bean but need to have them do work.

      Thanks.....

        • 1. Re: get/set methods in bean ?
          kabirkhan

          You have probably got the following in the entry for this bean in ejb-jar.xml:

          <cmp-field>
           <field-name>type</field-name>
           </cmp-field>
          


          Remove that. Each cmp-field entry must have matching abstract set/get methods in the bean class as per the message, and these correspond to the columns of the db table.