An introduction to creating Java EE applications in Rational Application Developer Version 8.0 September, 2010 © Copyright IBM Corporation 2010. 1 Overview Java Enterprise applications (Java EE applications) are applications that conform to the Java Platform, Enterprise Edition (Java EE) specification. Prior to Java EE, the specification name was Java 2 Platform, Enterprise Edition (J2EE). The term Java EE includes Java EE and J2EE specifications. In the Java EE specifications, programming requirements have been streamlined, and XML deployment descriptors are optional. Instead, you can specify many details of assembly and deployment with Java annotations. Java EE provides default values in many situations so explicitly specification of these values are not required. In Rational Application Developer, code validation, content assistance, Quick Fixes, and refactoring simplify working with your code. Code validators check your projects for errors. When an error is found, you can double-click it in the Problems view in the workbench to go to the error location. For some error types, you can use a Quick Fix to correct the error automatically. For both Java source and Java annotations, you can rely on content assistance to simplify your programming task. When you refactor source code, the tools automatically update the associated metadata. For additional information about Java EE, see the official specification: JSR 316: Java Platform, Enterprise Edition 6 (Java EE 6) Specification. What is Java EE Using the Java™ Platform, Enterprise Edition (Java EE) architecture, you can build distributed Web and enterprise applications. This architecture helps you focus on presentation and application issues, rather than on systems issues. You can use the Java EE tools and features to create applications that are structured around modules with different purposes, such as Web sites and Enterprise Java beans (EJB) applications. When you use EJB 3.1 components, you can create a distributed, secure application with transactional support. When you develop applications that access persistent data, you can use the new Java Persistence API (JPA). This standard simplifies the creation and use of persistent entities, as well as adding new features. For developing presentation logic, you can use technologies such as JavaServer Pages (JSP) or JavaServer Faces (JSF). Using the Java EE Platform Enterprise Edition (Java EE), you can develop applications more quickly and conveniently than in previous versions. Java EE significantly enhances ease of use providing © Copyright IBM Corporation 2010. 2 • • • Reduced development time Reduced application complexity Improved application performance Java EE provides a simplified programming model, including the following tools: • • • • Inline configuration with annotations, making deployment descriptors now optional Dependency injection, hiding resource creation, and lookup from application code Java persistence API (JPA) allows data management without explicit SQL or JDBC Use of plain old Java objects (POJOs) for Enterprise Java beans and Web services Java EE provides simplified packaging rules for enterprise applications: • • • • • • • • • Web applications use .WAR files Resource adapters use RAR files Enterprise applications use .EAR files The lib directory contains shared .JAR files A .JAR file can be specified in the application.xml of the EAR either as an application client or as an EJB module A .JAR file not specified by the application.xml of the EAR is defined as follows: • A .JAR file with an application-client.xml implies an application client • A .JAR file with an ejb-jar.xml implies an EJB module • A .JAR file with META-INF/MANIFEST.MF specified Main-Class implies an application client • A .JAR file with any @Stateless, @Stateful, or @MessageDriven annotations implies an EJB application A .JAR file with Main-Class implies an application client A .JAR file with @Stateless annotation implies an EJB application Many simple applications no longer require deployment descriptors, including • EJB applications (.JAR files) • Web applications that use JSP technology only • Application clients • Enterprise applications (.EAR files) Java EE provides simplified resource access using dependency injection: • In the Dependency Injection pattern, an external entity automatically supplies an object's dependencies. The object need not request these resources © Copyright IBM Corporation 2010. 3 • • explicitly. In Java EE, dependency injection can be applied to all resources that a component needs. Creation and lookup of resources are hidden from application code. Dependency injection can be applied throughout Java EE technology: • EJB containers • Web containers • Clients • Web services Creating and configuring Java EE modules using annotations The goal of Java™ EE 6 platform development is to minimize the number of artifacts that you have to create and maintain, thereby simplifying the development process. Java EE supports the injection of annotations into your source code, so that you can embed resources, dependencies, services, and lifecycle notifications in your source code, without having to maintain these artifacts elsewhere. An annotation is a modifier or metadata tag that provides additional data to Java classes, interfaces, constructors, methods, fields, parameters, and local variables. Annotations replace boilerplate code, common code that is required by certain applications. For example, an annotation can replace the paired interface and implementation required for a Web service. Annotations can also replace additional files that programs require, which are maintained separately. For example, annotations can replace the need for a separately maintained deployment descriptor for enterprise Java beans. Annotations: • Replace descriptors for most purposes • Remove the need for marker interfaces (like java.rmi.Remote) • Allow application settings to be visible in the component they affect Java EE provides annotations for the following tasks, among others: • • • • • • Developing Enterprise Java bean applications Defining and using Web services Mapping Java technology classes to XML Mapping Java technology classes to databases Mapping methods to operations Specifying external dependencies © Copyright IBM Corporation 2010. 4 • Specifying deployment information, including security attributes Java EE defines a number of annotations that can be injected into your source code. To declare an annotation, you simply precede the keyword with an "at" sign (@). EJB 3.1 overview You can use Rational Application Developer to develop and test enterprise beans that conform to the distributed component architecture defined in the Sun Microsystems Enterprise JavaBeans™ (EJB) 3.1 specification. Using this specification, you can develop beans more simply than in the 2.1 standard. You can annotate your Java™ source code to provide information that was previously contained in XML deployment descriptors. Using Java™ annotations allows you to create EJBs and Java Persistence Architecture (EJB 3.1) beans quickly and easily from “plain old Java objects” (POJOs). EJBs can be created without implementing Enterprise JavaBean interfaces. This product supports the Enterprise JavaBeans™ 2.1 and 3.1 specification levels. All the EJB tools in the product are accessible from the Java EE perspective in the workbench. You can create session beans (stateful or stateless) or message-driven beans, by simply specifying the component defining annotation in your POJO. Additional configuration for your bean can be done by specifying additional annotations in the Java class. The Java editor provides validation, content assistance, and QuickFixes for your EJB 3.1 annotations, as well as support for refactoring beans. For richer assistance with the EJB 3.1 annotations, you can use the Annotations view to add or delete annotations, and to modify the attribute values of annotations. Deployment descriptors for your EJB 3.1 modules are optional, but can be created for additional configuration. Developing JPA applications The Java™ Persistence API (JPA) defines the management of persistence and © Copyright IBM Corporation 2010. 5 object/relational mapping within Java Enterprise Edition (Java EE) and Java Standard Edition (Java SE) environments. The Java Persistence API (JPA) represents a simplification of the persistence programming model. JPA manages persistence and object/relational mapping within the Java EE specification for Enterprise Java Beans 3.0. The JPA specification defines the object/relational mapping within its own guidelines instead of relying on vendor-specific mapping implementations. These features make applications that use JPA easier to implement and manage. JPA combines the best features from previous persistence mechanisms such as Java Database Connectivity (JDBC) APIs, Object Relational Mapping (ORM) frameworks, and Java Data Objects (JDO). Creating entities under JPA is as simple as creating serializable classes. JPA supports the large data sets, data consistency, concurrent use, and query capabilities of JDBC. Like objectrelational software and object databases, JPA allows the use of advanced object-oriented concepts such as inheritance. JPA avoids vendor lock-in because it does not rely on a strict specification like JDO and EJB 2.x entities. The JPA implementation does not mandate that you migrate existing applications. Existing EJB 2.x Container Manager Persistence applications continue to execute without changes. JPA may not be ideal for every application, however, for many applications it provides a better alternative to other persistence implementations. With the Rational Application Developer JPA tools, you can use wizards to create and automatically initialize mappings. You can create new database tables from existing entity classes (top-down mapping) or new entity beans from existing database tables (bottom-up mapping). You can also use the tools to create mappings between existing database tables and entity beans (meet-inthe-middle mapping), where names or other attributes differ. For flexibility in designing your data access application, you can choose from a range of mapping types. You can create mappings from several types of Java class, and you can specify entity inheritance with several options for database design. JPA is described in the JSR 220 EJB 3.1 specification: JSR 220: Enterprise JavaBeans™ 3.0. JPA architecture The Java™ Persistence API represents a simplification of the persistence programming model. Data persistence, the ability to maintain data between application sessions in some form of nonvolatile storage (such as a relational database), is crucial to enterprise applications. Applications that are developed for this environment © Copyright IBM Corporation 2010. 6 must either manage data persistence themselves or make use of third-party solutions to handle database updates and retrievals. JPA provides a mechanism for managing data persistence and object-relational mapping and functions for the EJB 3.1 specifications. JPA is based on the Java programming model that applies to Java EE environments, but JPA can also function within the Java SE environment. The JPA specification defines the object-relational mapping internally, rather than relying on vendor-specific mapping implementations, and uses either annotations or XML to map objects into database tables. JPA is designed to operate both inside and outside of a Java Enterprise Edition (Java EE) container. When you run JPA inside a container, applications can use the container to manage the persistence. If there is no container to manage JPA, the application must handle the persistence management itself. Applications designed for container managed persistence cannot be used outside a container, while applications that manage their own persistence can function either in a container environment or a Java SE environment. JPA also provides a query language - JPQL - that you can use to retrieve objects without writing SQL queries specific to the database you are working with. Java EE containers that support JPA must supply a persistence provider. A JPA persistence provider uses the following elements to persist data in an EJB 3.1 environment: • Entity objects: An entity is a simple Java class that represents a row in a database table. Entities can be concrete classes or abstract classes. They maintain states by using properties or fields. • EntityManager: An EntityManager object maintains the active collection of entity objects that are being used by the application. The EntityManager object handles the database interaction and metadata for object-relational mappings. An instance of an EntityManager object represents a persistence context. An application in a container can obtain the EntityManager either through injection into the application or by looking it up in the Java component namespace. If the application manages its persistence, the EntityManager is obtained from the EntityManagerFactory. The application server containers typically supply this function, but the EntityManagerFactory is required if you are using JPA application-managed persistence. Note: Injection of the EntityManager is only supported for the following artifacts: • EJB 3.1 session beans. • EJB 3.1 message-driven beans. © Copyright IBM Corporation 2010. 7 • Servlets, but injection is not supported in JSPs. • The main class of the application client. • EntityManagerFactory: The factory is used to create an EntityManager for database interactions. • Persistence unit: A persistence unit consists of the declarative metadata that describes the relationship of entity class objects to a relational database. The EntityManagerFactory uses this data to create a persistence context that can be accessed through the EntityManager. • Persistence context: The persistence context is the set of active instances that the application is currently handling. The persistence context can be created manually or through injection. © Copyright IBM Corporation 2010. 8 Tools for Java EE development The Rational Application Developer Java™ EE development tools enable you to create enterprise applications including Enterprise Application projects, EJBs, and Java Persistent API (JPA) applications. The Java EE provides a rich set of tools to create, develop, test, debug, and deploy enterprise applications. Some of the Java EE tools include: • Enterprise explorer view: The Enterprise explorer view allows you to manage and maintain your enterprise applications in one location. • Annotations view: The Annotations view provides a way for you to create, edit, browse, and generally keep track of the annotations that you use in your applications. © Copyright IBM Corporation 2010. 9 • Project and object creation wizards: You can use Java EE wizards to create enterprise applications, including Application client projects, Enterprise Java bean projects, Connector projects, Web projects, and Web Services applications. • Export and import wizards: You can import existing projects and modules into your current project; you can use export wizards to build, package, and export projects with a single click. • Code validation, content assistance, Quick Fixes, and refactoring: These tools simplify working with your code. Code validators check your projects for errors. When one is found, you can double-click it, in the Problems view in the product workbench, to go to the error location. For some types of error, you can also choose a Quick Fix, which automatically corrects the error. For both Java™ source and Java annotations, you can rely on content assistance to simplify your programming task. When you refactor source code, the tools automatically update the associated metadata. • Tools for managing projects: The tools for Java EE allow you to manage projects easily. To share applications outside of a version-control system, you can import and export projects as archive files. To migrate your projects to the current version of the Java EE specification, you can take advantage of a migration wizard. © Copyright IBM Corporation 2010. 10 For more information To learn more about Java EE and Rational Application Developer, see these resources: • Rational Application Developer Web site: http://www-01.ibm.com/software/awdtools/developer/application/index.html • Rational Application Developer Information Center: http://publib.boulder.ibm.com/infocenter/radhelp/v7r5/index.jsp • Rational Application Developer wiki: https://www.ibm.com/developerworks/wikis/display/rad/Home © Copyright IBM Corporation 2010. 11
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
advertisement