Book Image

EJB 3.1 Cookbook

By : Richard M. Reese
Book Image

EJB 3.1 Cookbook

By: Richard M. Reese

Overview of this book

<p>Enterprise Java Beans enable rapid and simplified development of secure and portable applications based on Java technology.Creating and using EJBs can be challenging and rewarding. Among the challenges are learning the EJB technology itself, learning how to use the development environment you have chosen for EJB development, and the testing of the EJBs.<br /><br />This EJB 3.1 Cookbook addresses all these challenges and covers new 3.1 features, along with explanations of useful retained features from earlier versions. It brings the reader quickly up to speed on how to use EJB 3.1 techniques through the use of step-by-step examples without the need to use multiple incompatible resources. The coverage is concise and to the point, and is organized to allow you to quickly find and learn those features of interest to you.<br /><br />The book starts with coverage of EJB clients. The reader can choose the chapters and recipes which best address his or her specific needs. The newer EJB technologies presented include singleton beans which support application wide needs and interceptors to permit processing before and after a target method is invoked. Asynchronous invocation of methods and enhancements to the timer service are also covered. <br /><br />The EJB 3.1 CookBook is a very straightforward and rewarding source of techniques supporting Java EE applications.</p>
Table of Contents (19 chapters)
EJB 3.1 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Validating null fields


Object references are used extensively in Java. Assigning a null value is not an uncommon practice though they need to be assigned a value before a method can be executed against it. Fields of a database may or may not be allowed to contain a null value. The @Null and @NotNull annotations can be used to indicate whether a reference field can be assigned a null value or not.

Getting ready

We will use the LicenseBean and LicenseBeanFacade classes from the ValidationApplication as discussed in the Validating persistent fields and properties recipe.

How to do it...

If we want to prevent a column of a database from being assigned a value of null, then we can enforce this with the @NotNull annotation.

  @NotNull
  private String name;

If the field must contain a null, then the @Null annotation is used.

  @Null
  private String name;

How it works...

These annotations are used to control the assignment of null values to fields. The Validator class is used in conjunction with these...