Programming WebLogic Enterprise JavaBeans
|
|
EJBGen is an Enterprise JavaBeans 2.x code generator. If you annotate your Bean class file with JDK 1.5 annotations, you can use EJBGen to generate the Remote and Home classes and the deployment descriptor files for an EJB application.
BEA recommends that you use EJBGen to generate deployment descriptors; this BEA best practice allows for easier and simpler maintenance of EJBs. If you use EJBGen, you need to write and annotate only one bean class file, which simplifies writing, debugging, and maintenance. The following sections provide instructions on using EJBGen and define EJBGen command options and annotations:
These sections describe how to process an annotated bean file using EJBGen:
In this release of WebLogic Server, EJBGen and associated classes are included in weblogic.jar in the WL_HOME/server/lib of the WebLogic Server distribution. To use EJBGen, make sure weblogic.jar is in your CLASSPATH. EJBGen annotations can be found in the weblogic.ejbgen package.
If you have installed BEA WebLogic Server 9.0 examples, see WL_HOME\samples\server\examples\src\examples\ejb20\ and WL_HOME\samples\server\examples\src\examples\ejb21\ for an application that uses EJBGen.
Use this command to run EJBGen on a bean source file:
Note: To run this command, weblogic.jar must be in your CLASSPATH.
If you are invoking EJBGen for an EJB that has relationships with other EJBs, invoke the related EJBs by naming them, following your EJB, in the invocation, as follows:
com.bea.wls.ejbgen.EJBGen (YourBean).java (RelatedBean).java
Note: You can also invoke EJBGen via the ejbgen ant task, which is also included in weblogic.jar.
Table E-1 defines EJBGen command options.
Table E-1 EJBGen Command Options
|
If invoked with this option, EJBGen will not generate any classes but will search the classes supplied on the command line for tags that are not valid EJBGen tags. |
||
|
The directory under which all the descriptor files will be created, relative to the output directory specified with the -d [directory] option. |
||
|
If specified, EJBGen prints out the detailed documentation for this tag, including all the recognized attributes. Note that even though this option does not need any source file, you still need to specify an existing |
||
|
Print out all the tags known by EJBGen. Note that even though this option does not need any source file, you still need to specify an existing |
||
|
Force the generation of all files regardless of time stamps. |
||
|
If this flag is set, EJBGen ignores the package name of the Java files it generates and creates those in the output directory as specified by the -d flag (or in the current directory if no -d was specified). |
||
|
If this flag is set, EJBGen will generate deployment descriptors compatible with the Public Final Draft 1 of the EJB 2.0 specification. You should use this flag if you are using any WebLogic Server version earlier than 6.1. |
||
|
If this flag is set, EJBGen will generate deployment descriptors compatible with the Public Final Draft 2 of the EJB 2.0 specification. You should use this flag if you are using any WebLogic Server version earlier than 6.1. |
||
|
The name of a property file that EJBGen reads to define substitution variables. See Using Property Files with EJBGen. |
||
|
The prefix to use when generating the remote EJB home class. |
||
|
The suffix to use when generating the remote EJB home class. |
||
|
If specified, the |
||
|
Use this flag to specify that EJBGen generate WebLogic Server 7.0 deployment descriptors. |
||
|
Use this flag to specify that EJBGen generate WebLogic Server 7.0 DTD-based deployment descriptors. |
||
|
Use this flag to specify that EJBGen generate WebLogic Server 7.1 DTD-based deployment descriptors. |
||
|
Use this flag to specify that EJBGen generate WebLogic Server 8.1 DTD-based deployment descriptors. |
||
|
Use this flag to specify that EJBGen generate WebLogic Server 9.0 XSD-based deployment descriptors. |
||
|
The encoding to use when generating XML files (default: UTF-8). |
These sections describe key EJBGen features and how to use them:
By default, EJBGen generates the following files:
You can selectively disable the generation of these files by using the weblogic.ejbgen.FileGeneration tag. This can be done on a per-bean basis.
EJBGen can gather information for generation from property files, as discussed in this section.
Use the -propertyFile option to tell EJBGen to parse a properties file.
Use the following syntax to invoke EJBGen with the -propertyFile option:
com.bea.wls.ejbgen.EJBGen-propertyFile property-file AccountBean.java
EJBGen recognizes two kinds of variables in a properties file: user variables and predefined variables.
EJBGen tags can use variables instead of strings. These variables must be enclosed with "${" and "}".
Variables can be used anywhere after an EJBGen tag, so they can contain whole tag definitions.
EJBGen recognizes a number of predefined variables. These variables are not supposed to be used in tags but EJBGen will use them at various places depending on their role. Recognized variables are:
remote.baseClassIf specified, the value of this variable will be used as the base class for all generated remote classes.
home.baseClass
local.baseClassIf specified, the value of this variable will be used as the base class for all generated local classes.
localHome.baseClassIf specified, the value of this variable will be used as the base class for all generated local home classes.
value.baseClassIf specified, the value of this variable will be used as the base class for all generated value classes.
value.packageIf specified, the value of this variable will be used as the package for all generated value classes.
value.interfacesYou can make these variables more specific by prefixing them with an EJBName. For example, consider the following property file.
#
# property-file
#
Account.home.baseClass = BaseAccountHome
home.baseClass = BaseHome
value.package = value
containerManaged.value.package=valuePackageForContainerManaged
All homes generated by EJBGen extend the class BaseHome except the home of EJB "Account", which extends BaseAccountHome.
When you specify an attribute that contains spaces, surround the attribute value with double quotes. For example:
group-names = "group1, group2"
One of the main features in EJBGen 2.x is tag inheritance. You inherit EJBGen annotations the same way you do in Java. For example, assume that you have an EJB named AccountEJB with a base class BaseAccount, as in Listing 8-6:
/*** @JndiName(remote = "BaseRemote")
*/
public class BaseAccount implements EntityBean {
// ...
}
/***@Entity(ejbName = containerManaged,*tableName = ejbAccounts,*datasourceJndiName = examples-dataSource-demoPool)*
*Note that we inherit the JNDI name defined in BaseAccount
*/
public class AccountEJB extends BaseAccount {
// ...
These sections contain example source code annotated with EJBGen tags.
By default, entity beans are mapped to one table, with the attribute tableName on the tag @Entity. If you want to map your entity bean to more than one table, you can use the table-name attribute on individual @CmpFields. All the container-managed persistence fields that do not have a tableName attribute will use the table specified on @Entity (which can therefore be considered as the "default" table).
If you want to map an entity bean to several tables, you need to specify a comma-separated list of tables in the tableName attribute (and also on a column). Make sure that the number of tables matches the number of columns, and that the columns exist in the corresponding table.
You can specify relationship caching elements with the @RelationshipCaching tag. This element can be nested (recursive). In other words, you can specify a tree representing all the relationship elements that you want to see cached.
In order to support this tree structure, @RelationshipCaching has two attributes, id and parentId. These attributes do not correspond to any XML, they simply allow you to specify your tree structure.
For example, if a cachingElement A needs to have two children elements B and C, all you need to specify is an id for A (say "root0") and a parentId for B and C equal to that name.
Listing 8-7 illustrates specifying relationship caching.
Listing 8-7 Specifying Relationship Caching
* @RelationshipCachingElements(
* @RelationshipCachingElement(cachingName = cacheMoreBeans,
* cmrField = employee,
* groupName = emp-group,
* id = A),
*
* @RelationshipCachingElement(cachingName = cacheMoreBeans,
* cmrField = employee_ssn,
* groupName = emp-group,
* parentId = A),
*
* @RelationshipCachingElement(cachingName = cacheMoreBeans,
* cmrField = employee_zip,
* groupName = emp-group,
* * parentId = A)
* )
Unidirectional relationships are achieved by specifying only the first two parameters of the relation tag (no CMR_field, no joint table).
Many-to-many relationships must specify an extra table (parameter "joint table") which must contain at least two columns. The names of these columns must match the names of the columns containing the foreign keys of the two beans being joined.
If you have EJBs based on a version of the EJB 2.0 specification that predates PFD2, you must update them. As of PFD2, relationships must be based on local interfaces, not remote ones. To upgrade your EJBs:
@JndiName
remote = AccountHome
local = AccountLocalHome
@LocalMethod tags to any method you want to see appear on the local home interface. A method can have both @LocalMethod and @RemoteMethod./**
* @CmrField
*
* @RemoteMethod
* @LocalMethod
*/
abstract public LocalCustomer getCustomer();
The following enum values are used for many of the EJBGen attributes.
Valid values for Constant.Bool are as follows:
Valid values for Constants.TransactionAttribute are as follows:
Constants.TransactionAttribute.UNSPECIFIEDConstants.TransactionAttribute.NOT_SUPPORTEDConstants.TransactionAttribute.SUPPORTSConstants.TransactionAttribute.REQUIREDConstants.TransactionAttribute.REQUIRES_NEWConstants.TransactionAttribute.MANDATORYConstants.TransactionAttribute.NEVERValid values for Constants.IsolationLevel are as follows:
Valid values for Constant.RefType are as follows:
Valid values for Constants.HomeLoadAlgorithm are as follows:
Constants.HomeLoadAlgorithm.UNSPECIFIEDConstants.HomeLoadAlgorithm.ROUND_ROBINConstants.HomeLoadAlgorithm.RANDOMConstants.HomeLoadAlgorithm.WEIGHT_BASEDConstants.HomeLoadAlgorithm.ROUND_ROBIN_AFFINITYConstants.HomeLoadAlgorithm.RANDOM_AFFINITYConstants.HomeLoadAlgorithm.WEIGHT_BASED_AFFINITYValid values for Contants.Interface are as follows:
Constants.Interface.UNSPECIFIEDConstants.Interface.HOMEConstants.Interface.REMOTEConstants.Interface.LOCAL_HOMEConstants.Interface.LOCALValid values for Constants.ConcurrencyStrategy are as follows:
Constants.ConcurrencyStrategy.UNSPECIFIEDConstants.ConcurrencyStrategy.READ_ONLYConstants.ConcurrencyStrategy.EXCLUSIVEConstants.ConcurrencyStrategy.DATABASEConstants.ConcurrencyStrategy.OPTIMISTIC
This section provides a complete reference for the following EJBGen annotations you can use in your bean class file:
The following is a list of container annotations that can only contain an array of annotations.
Note: A single class or method can be annotated by no more than one instance of any annotation.
For more information on an annotation, including the array of annotations it can contain, click the annotation name.
This annotation does not have any attributes, other than the array of weblogic.ejbgen.ActivationConfigProperty annotations.
|
The column where this CMP field will be mapped. See Mapping an Entity Bean to Several Tables with EJBGen. |
|||
|
If See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
The table(s) where this field should be mapped. Mapping an Entity Bean to Several Tables with EJBGen. |
|||
|
Comma-delimited names of the groups this field belongs to. Surround the list of group names with double quotes. |
|||
|
The number where this field must appear in signatures and constructors. For this ordering to work, all cmr and cmp fields must set this attribute to a distinct numeric value. |
|||
|
If See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
The type of this column.Valid values for this attribute are:
|
|||
|
Whether this field is part of the compound primary key. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Whether this field is the default DBMS value. See Constants.Bool for the list of valid values for this attribute. The default value is |
Enum (Constants.Bool) |
|
If See Constants.Bool for the list of valid values for this attribute. The default value is |
Enum (Constants.Bool) |
||
|
Comma-delimited names of the groups this field belongs to. Surround the list of group names with double quotes. |
|||
|
The number where this field must appear in signatures and constructors. For this ordering to work, all cmr and cmp fields must have this attribute to a distinct numeric value. |
|||
|
If True, this field will not be generated in the value object. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
|
Whether a cmp-field of type byte[] mapped to a Blob in an Oracle database should be serialized. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Whether to disable String trimming. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Whether a cmp-field of type byte[] mapped to an OracleBlob should be serialized. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Whether to allow read only create and remove. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Whether to allow finder methods to return null. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
This tag is deprecated; use weblogic.ejbgen.JarSettings instead.
This tag is deprecated; use weblogic.ejbgen.JarSettings instead.
This tag is deprecated; use weblogic.ejbgen.JarSettings instead.
|
See Constants.RefType for the list of valid values for this attribute. |
Enum (Constants.RefType) |
||
This annotation does not have any attributes, other than the array of weblogic.ejbgen.EjbLocalRef annotations.
|
See Constants.RefType for the list of valid values for this attribute. |
Enum (Constants.RefType) |
||
This annotation does not have any attributes, other than the array of weblogic.ejbgen.EjbRef annotations.
|
Names the Java class of the primary key. In case of a compound primary key, this class will be generated by EJBGen. |
|||
|
Specifies the rows in a table that should be checked when optimistic concurrency is used. |
|||
|
Specifies whether the clients are collocated with the EJB on the same server. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Targets the ejb-name of a read-only entity bean to be invalidated when this container-managed persistence entity EJB is modified. |
|||
|
Determines whether or not this EJB uses the callers identity. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Specifies whether to cache the persistent data of an entity bean across (between) transactions. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Sets the default transaction attribute to be applied to all methods that do not have a transaction attribute setting. See Constants.TransactionAttribute for the list of valid values for this attribute. The default value is |
|||
|
Specifies whether the container checks for the existence of a bean for each method call. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Specifies the column that holds the timestamp for optimistic concurrency. |
|||
|
Determines whether to sort the database operations. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Gives the name of the principal in case the role maps to several principals |
|||
|
Specifies until when database inserts should be delayed. Valid values for this attribute are as follows: The default value is |
|||
|
Specifies whether the container will call this EJB by reference. See Constants.Bool for the list of valid values for this attribute. |
Enum (Constants.Bool) |
||
|
Specifies the method used to validate the tables created by the EJB container. Valid values for this attribute are as follows: The default value is |
|||
|
Sets the database locking order of this bean when a transaction involves multiple beans. |