Trouble using Data Value Class and CMR Together

Discussions

EJB programming & troubleshooting: Trouble using Data Value Class and CMR Together

  1. Hi,
    I have defined a DVC for Name as exactely defined in Richard Monson EJB (Oreally's) book I am receiving a message that for name field there is no abstract method and the name field is actually a DVC i have defined it as a cmp in descriptor , it is correct that i dont have any abstract method for name but name methods that are
    1. public void setName(Name name) &
    2. public Name getName();
    are defined to use as a Data Value Class what should i do to tell the container that this name field is a dvc. Please if i have left something unclear let me know .
    Regards
    Arain
  2. Most likely, you are getting the error because you have Name specified as a CMP field, but you don't have the abstract methods in place that would set it, and with a standard relational database you really can't, since it isn't a basic type like a string.

    One thing that he mentions in the book is that the methods used on the EJB and the persistence requirements of the EJB don't have to be the same. For instance, you could have fields in the database for things like First Name and Last Name, but have a method that just retrieves a Name class that has these values available as members. This really is the context of what you're trying.

    So, with that in mind, you aren't retrieving a Name object from the database, you're retrieving First Name and Last Name. So, these would be your CMP fields. You would need to have something like this in your bean class:

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

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

    Note that you wouldn't have these defined in either the local or remote interfaces, since you don't want people to get at these values directly. You want them to get the Name object instead. So, still in the bean class, you would define methods that would get and set the Name class, like so:

    public Name getName() {
    Name name = new Name(getLastName(),
                                         getFirstName());
    return name;
    }

    public void setName(Name name) {
    setLastName(name.getLastName());
    setFirstName(name.getFirstName());
    }

    In the remote interface, you would then define these methods to make them accessible, like so:

    public Name getName()
                   throws RemoteException;
    public void setName(Name name)
                   throws RemoteException;

    The local interface would have similar definitions.

    Then, in the deployment descriptor, you would have references to the firstName and lastName fields:

                <cmp-field>
                     <field-name>lastName</field-name>
                </cmp-field>
                <cmp-field>
                     <field-name>firstName</field-name>
                </cmp-field>

    At this point, then, you have it set up so that the database talks in terms of lastName and firstName but the client would ask for a Name. Then, you just need to provide the Name class that will be used.

    Note that this code is from the Monson-Haefel book (credit where credit is due).

    Mark