Friday, March 02, 2007

JAVA 認證心得 -- SCBCD (4)

Entity Beans
##CONTINUE##

  • An Entity beans can only have Required, RequiresNew, and Mandatory as its transaction attribute.
  • The primary key type in CMP entity beans must be a legal Value Type in RMI-IIOP.
  • The primary key class must be public, and must have a public constructor with no parameters.
  • The return type of findByPrimaryKey(...) method in the CMP entity bean's home interface is the entity bean's component interface.
  • The finder methods defined in the home interface of CMP entity beans must return Entity bean's component interface or its collection.
  • The Bean Provider does not provide any ejbFind<METHOD> method corresponding to finder method in the home interface of the CMP entity bean.
  • The Container throws javax.ejb.ObjectNotFoundException only during single-entity finder methods.
  • A entity bean using CMP to create persistent relationships
    1. A local interface is required to have a bidirectional relationship with another entity bean.
    2. Relationships can be one-to-one, one-to-many, or many-to-many
    3. A get method return type can use Collection and Set
  • The bean’s primary key class must provide a suitable implementation of the hashCode and equals methods
  • Instead of defining separate primary key classes, CMP entity beans can share the same primary key class
  • Use equals() on a primary key class to determine if two keys refer to the same entity
  • The <primkey-field> element is used to specify the name of the primary key field for an entity with container-managed persistence.
  • The <primkey-field> must be one of the fields declared in the <cmp-field> element, and the type of the field must be the same as the primary key type.
  • The <primkey-field> element is not used if the primary key maps to multiple container-managed fields (i.e. the key is a compound key). In this case, the fields of the primary key class must be public, and their names must correspond to the field names of the entity bean class that comprise the key.
  • The prim-key-class element contains the fully-qualified name of an entity bean’s primary key class.
    If the definition of the primary key class is deferred to deployment time, the prim-key-class element should specify java.lang.Object.

    Used in: entity
    Examples:
    <prim-key-class>java.lang.String</prim-key-class>
    <prim-key-class>com.wombat.empl.EmployeeID</prim-key-class>
    <prim-key-class>java.lang.Object</prim-key-class>
  • A entity bean does survive the server crash
  • Finder methods throw the FinderException also application specific exceptions. All ejbFind<METHOD>() methods except the ejbFindByPrimaryKey() method need to be specified in the query DD element for the entity.
  • The names of the fields in the primary key class must be a subset of the names of the container-managed fields.
  • getCallerPrincipal() method of javax.ejb.EntityContext interface can be used by a CMP entity bean instance to obtain the invoker of its component interface. It returns the java.security.Principal that identifies the invoker of entity bean's component interface.
  • getEJBObject() returns the remote component interface of the entity bean instance.
  • An entity bean is associated with an object identity only when it is in ready state.
  • A CMP entity bean class must be abstract , provide abstract getters and setters for all persistent fields. Implement ejbCreate methods for all create methods declared in its home interface. All the finder methods are implemented by the container
  • Helper classes used by the bean class and Bean's local home and local component interfaces must be in an ejb-jar.
  • create method Must return a component interface type.
  • finder method May return a component interface type or return a collection of component interface types.
  • remove method Must return void
  • findByPrimaryKey method Must return a component interface type.
  • Session beans do not have an identity. Only Entity beans have an identity, which can be retrieved using EntityContext.getPrimaryKey().
  • Primary Key Class for an entity bean must follow these rules: 1. must implement the java.io.Serializable interface. 2. all its fields have to be made public, to allow the container to use the Relfection API during the synchronization with the database. 3. when using CMP Entity Beans, the fields of the Primary Key class must also be present in the Bean class, to allow the container to set the values using Reflection API. Although not necessary, it should also override the hashCode() and equals() methods to allow the class to be better handled inside Collections.
    public class MyKey implements java.io.Serializable {
    public String a, b;
    public String getA(){ return a;}
    public void setA(String pA){ a = pA; }
    public String getB(){ return b;}
    public void setB(String pB){ b = pB; }
    }
  • Once a bean is removed, the bean does not exist. Therefore, the primary can be used to create a new bean.
  • Rules for a primary key class: 1. The primary key class must be public, and must have a public constructor with no parameters. 2. All fields in the primary key class must be declared as public. 3. The names of the fields in the primary key class must be a subset of the names of the container-managed fields.(This allows the container to extract the primary key fields from an instance's container-managed fields, and vice versa.)
  • EntityContext interface extends from EJBContext interface and declares only three additional methods.
    1. EJBLocalObject getEJBLocalObject() Obtain a reference to the EJB local object that is currently associated with the instance.
    2. EJBObject getEJBObject() Obtain a reference to the EJB object that is currently associated with the instance.
    3. java.lang.Object getPrimaryKey() Obtain the primary key of the EJB object that is currently associated with this instance.
    Methods inherited from interface javax.ejb.EJBContext: getCallerPrincipal, getEJBHome, getEJBLocalHome, getRollbackOnly, getUserTransaction, isCallerInRole, setRollbackOnly
  • ths <prim-key-class> element contains the fully-quified runtime type of the primary key class

Read more!

No comments: