Identify correct and incorrect statements or examples about an entity bean's primary key and object identity.

This page discusses - Identify correct and incorrect statements or examples about an entity bean's primary key and object identity.

Identify correct and incorrect statements or examples about an entity bean's primary key and object identity.

Identify correct and incorrect statements or examples about an entity bean's primary key and object identity.

From the viewpoint of the Bean Provider, entity objects have a runtime object identity that is maintained by the Container.

The Container maintains the persistent identity of an entity object on the basis of its PRIMARY KEY.

The primary key of an entity bean may or may not be visible as one or more cmp-fields of the instance, depending on the way in which it is specified. Once it has been set, the Bean Provider MUST NOT attempt to change the value of a primary key field by means of a set method on its cmp-fields.

When a new instance of an entity bean whose primary key fields are visible in the entity bean class is created, the Bean Provider MUST use the ejbCreate<METHOD>(...) method to set all the primary key fields of the entity bean instance before the instance can participate in a relationship, e.g. be used in a set accessor method for a cmr-field. The Bean Provider MUST NOT reset a primary key value by means of a set method on any of its cmp-fields after it has been set in the ejbCreate<METHOD>(...) method.

The container must be able to manipulate the primary key type of an entity bean. Therefore, the primary key type for an entity bean with container-managed persistence MUST follow the rules:

  • The Bean Provider MUST specify a primary key class in the deployment descriptor. (In case the class is not known until deployment, Bean Provider specify java.lang.Object class).

  • The primary key type MUST be a legal Value Type in RMI-IIOP (serializable).

  • The class MUST provide suitable implementation of the hashCode() and equals(Object obj) methods to simplify the management of the primary keys by the Container.

There are two ways to specify a primary key class for an entity bean with container-managed persistence:

  1. Primary key that maps to a SINGLE field in the entity bean class.

    The Bean Provider uses the primkey-field element of the deployment descriptor to specify the container-managed field of the entity bean class that contains the primary key. The field’s type must be the primary key type.

    								
    <entity>
    	<ejb-name>CabinEJB</ejb-name>
    	<home>com.titan.cabin.CabinHomeRemote</home>
    	<remote>com.titan.cabin.CabinRemote</remote>
    	<ejb-class>com.titan.cabin.CabinBean</ejb-class>
    	<persistence-type>Container</persistence-type>
    	<prim-key-class>java.lang.Integer</prim-key-class>
    	<reentrant>False</reentrant>
    	<cmp-version>2.x</cmp-version>
    	<abstract-schema-name>Cabin</abstract-schema-name>
    	<cmp-field><field-name>id</field-name></cmp-field>
    	<cmp-field><field-name>name</field-name></cmp-field>
    	<cmp-field><field-name>deckLevel</field-name></cmp-field>
    	<cmp-field><field-name>shipId</field-name></cmp-field>
    	<cmp-field><field-name>bedCount</field-name></cmp-field>
    	<primkey-field>id</primkey-field>
    	<security-identity><use-caller-identity/></security-identity>
    </entity>
    								
    								
  2. Primary key that maps to MULTIPLE fields in the entity bean class.

    The primary key class MUST be public, and MUST have a public constructor with NO PARAMETERS.

    ALL fields in the primary key class MUST be declared as public.

    The names of the fields in the primary key class MUST be a subset of the names of the container-managed fields.

public class ItemKey implements java.io.Serializable {
	public String productId; // public - mandatory
	public String vendorId; // public - mandatory
	public ItemKey() { }; // no-args constructor - mandatory
	
	public ItemKey(String productId, String vendorId) {
		this.productId = productId;
		this.vendorId = vendorId;
	}
	
	public String getProductId() {
		return productId;
	}

	public String getVendorId() {
		return vendorId;
	}
	
	public boolean equals(Object other) { // mandatory
		if (other instanceof ItemKey) {
			return (productId.equals(((ItemKey)other).productId) 
			&& vendorId.equals(((ItemKey)other).vendorId));
		}
		return false;
	}

	public int hashCode() { // mandatory
		return productId.hashCode();
	}
}
					
A primary key class MUST meet these requirements:
  • The access control modifier of the class is public.

  • All fields are declared as public.

  • Field names in the primary key clsss MUST match the corresponding container-managed fields in the entity bean class.

  • The class has a public default (no-args) constructor.

  • The class implements the hashCode() and equals(Object obj) methods.

  • The class is SERIALIZABLE (implements java.io.Serializable).

In special situations, the entity Bean Provider may choose not to specify the primary key class or the primary key fields for an entity bean with container-managed persistence. This case usually happens when the entity bean does not have a natural primary key, and/or the Bean Provider wants to allow the Deployer using the Container Provider’s tools to select the primary key fields at deployment time. The entity bean’s primary key type will usually be derived from the primary key type used by the underlying database system that stores the entity objects. The primary key used by the database system may not be known to the Bean Provider.

In this special case, entity bean must meet these requirements:

  • In the deployment descriptor, the primary key class is defined (by Bean Provider) as a java.lang.Object. The primary key field is not specified.

  • In the HOME interface, the argument of the findByPrimaryKey method MUST be a java.lang.Object.

  • In the entity bean CLASS, the return type of the ejbCreate method MUST be a java.lang.Object.

We are using 2 elements in DD for defining primary keys: <prim-key-class> and <primkey-field> :

  • SINGLE-FIELD primary key: <prim-key-class> is a MANDATORY, <primkey-field> is a MANDATORY.

  • COMPOUND primary key: <prim-key-class> is a MANDATORY, <primkey-field> MUST NOT be defined.

The primary key class can be specific to a particular entity bean class. That is, each entity bean can define its own primary key class. Or multiple entity beans CAN share the same primary key class.

If two entity objects with the SAME home interface have the SAME primary key, they are considered IDENTICAL entity objects. If they have a different primary key, they are considered different entity objects.

There are 2 ways of comparing Entity Beans:

  1. Using isIdentical(EJBObject obj) method of javax.ejb.EJBObject interface on bean's REMOTE interface, or isIdentical(EJBLocalObject obj) method of javax.ejb.EJBLocalObject interface on bean's LOCAL interface.

    Item item1 = ...;
    Item item2 = ...;
    
    if (item1.isIdentical(item2)) {
    	// item1 and item2 refer to the same entity object
    }
    							
  2. Comparing primary keys of two entity object references (ONLY FROM THE SAME HOME INTERFACE !).

    ItemHome itemHome = ...; // get home interface
    Item item1 = itemHome.findByName(...);
    Item item2 = itemHome.findByCountry(...);
    
    if (item1.getPrimaryKey().equals(item2.getPrimaryKey())) {
    	// item1 and item2 refer to the same entity object
    }
    							

Tutorials

  1. Appendix A. First Appendix
  2. Second Section
  3. Third Section
  4. Part II. Appendixes
  5. From a list, identify the responsibility of the bean provider and the responsibility of the container provider for a message-driven bean.
  6. Chapter 6. Component Contract for Container-Managed Persistence (CMP)
  7. Identify correct and incorrect statements or examples about persistent relationships, remove protocols, and about the abstract schema type of a CMP entity bean.
  8. Identify the interfaces and methods a CMP entity bean must and must not implement.
  9. Match the name with a description of purpose or functionality, for each of the following deployment descriptor elements: ejb-name, abstract-schema-name, ejb-relation, ejb-relat
  10. Identify correctly-implemented deployment descriptor elements for a CMP bean (including container-managed relationships).
  11. From a list, identify the purpose, behavior, and responsibilities of the bean provider for a CMP entity bean, including but not limited to: setEntityContext, unsetEntityContext, ejbC
  12. Chapter 7. CMP Entity Bean Life Cycle
  13. Identify correct and incorrect statements or examples about the rules and semantics for relationship assignment and relationship updating in a CMP bean.
  14. From a list, identify the responsibility of the container for a CMP entity bean, including but not limited to: setEntityContext, unsetEntityContext, ejbCreate, ejbPostCreate, ejbActi
  15. Given a code listing, determine whether it is a legal and appropriate way to programmatically access a caller's security context.
  16. Chapter 10. Message-Driven Bean Component Contract
  17. Identify correct and incorrect statements about the purpose and use of the deployment descriptor elements for environment entries, EJB references, and resource manager connection factory r
  18. Identify the use and the behavior of the ejbPassivate method in a session bean, including the responsibilities of both the container and the bean provider.
  19. Chapter 12. Exceptions
  20. Identify correct and incorrect statements or examples about the client view of an entity bean's local component interface (EJBLocalObject).
  21. Identify EJB 2.0 container requirements.
  22. Chapter 1. EJB Overview
  23. Identify correct and incorrect statements or examples about EJB programming restrictions.
  24. Chapter 9. EJB-QL
  25. Identify correct and incorrect statements or examples about the purpose and use of EJB QL.
  26. Identify correct and incorrect conditional expressions, BETWEEN expressions, IN expressions, LIKE expressions, and comparison expressions.
  27. Identify correct and incorrect statements or examples about the client view of a entity bean's remote component interface (EJBObject).
  28. Given a list, identify which are requirements for an EJB-jar file.
  29. Match EJB roles with the corresponding description of the role's responsibilities, where the description may include deployment descriptor information.
  30. Chapter 2. Client View of a Session Bean
  31. Chapter 13. Enterprise Bean Environment
  32. Chapter 8. Entity Beans
  33. Identify the use, syntax, and behavior of, the following entity bean home method types, for Container-Managed Persistence (CMP); finder methods, create methods, remove methods, and home me
  34. Identify correct and incorrect statements or examples about an entity bean's primary key and object identity.
  35. Identify correct and incorrect statements or examples about the client's view of exceptions received from an enterprise bean invocation.
  36. Identify correct and incorrect statements or examples about application exceptions and system exceptions in entity beans, session beans, and message-driven beans.
  37. Given a particular method condition, identify the following: whether an exception will be thrown, the type of exception thrown, the container's action, and the client's view.
  38. Given a list of responsibilities related to exceptions, identify those which are the bean provider's, and those which are the responsibility of the container provider. Be prepared to recog
  39. SCBCD Study Guide
  40. Identify the use and behavior of the MessageDrivenContext interface methods.