EJB 2 is widely used but it comes at a cost - procedural, redundant code. EJB 3 is a different animal. By adopting a POJO programming model and Java 5 annotations, it dramatically simplifies enterprise development. A cool new feature, its Java Persistence API, creates a standard for object-relational mapping. You can use it for any Java application, whether inside or outside the EJB container. With EJB 3 you will create true object-oriented applications that are easy to write, maintain and...
"EJB 3 in Action" tackles EJB 3 and the Java Persistence API head-on, providing practical code samples, real-life scenarios, best practices, design patterns, and performance tuning tips.
Preface xixAcknowledgments xxiAbout this book xxivOverview of the EJB Landscape 1What's what in EJB 3 3EJB overview 5EJB as a component 5EJB as a framework 7Layered architectures and EJB 9Why choose EJB 3? 12Understanding EJB types 14Session beans 16Message-driven beans 16Entities and the Java Persistence API 17Getting inside EJB 19Accessing EJB sendees: the EJB container 20Accessing JPA services: the persistence provider 20Gaining functionality with EJB services 21Renaissance of EJB 23HelloUser Example 24Simplified programming model 25Annotations instead of deployment descriptors 26Dependency injection vs. JNDI lookup 27Simplified persistence API 27Unit-testable POJO components 29EJB 3 and Spring 30Summary 32A first taste of EJB 33New features: simplifying EJB 35Replacing deployment descriptors with annotations 35Introducing dependency injection 40Introducing the ActionBazaar application 41Starting with the architecture 42An EJB 3-based solution 43Building business logic with session beans 45Using stateless beans 45The stateless bean client 47Using stateful beans 50A stateful bean client 54Messaging with message-driven beans 56Producing a billing message 57Using the order billing message processor MDB 59Persisting data with EJB 3 JPA 61Working with the Java Persistence API 62Using the EntityManager 65Summary 67Building Business Logic with EJB 3 69Building business logic with session beans 71Getting to know session beans 72Why use session beans? 73Session beans: the basics 75Understanding the programming rules 78Conversational state and session bean types 79Bean lifecycle callbacks 79Stateless session beans 83The BidManagerBean example 84Using the @Stateless annotation 86Specifying bean business interfaces 87Using bean lifecycle callbacks 90Stateful session beans 93Additional programming rules 94The BidderAccountCreatorBean example 94Business interfaces for stateful beans 98Stateful bean lifecycle callbacks 98Session bean clients 102Using the @EJB annotation 104Injection and stateful session beans 104Performance considerations for stateful beans 105Using stateful session beans effectrvely 105Stateful session bean alternatives 107Session bean best practices 108Summary 109Messaging and developing MDBs 110Messaging concepts 111Message-oriented middleware 112Messaging in Action Bazaar 112Messaging models 114Introducing Java Messaging Service 117Developing the JMS message producer 117The JMS message interface 119Working with message-driven beans 121Why use MDBS? 122Programming rules 124Developing a message consumer with MDB 124Using the @MessageDriven annotation 127Implementing the MessageListener 127Using ActivationConfigProperty 128Using bean lifecycle callbacks 132Sending JMS messages from MDBs 135Managing MDB transactions 136MDB best practices 137Summary 139Learning advanced EJB concepts 140EJB internals 141EJB behind the scenes 141EJB context: accessing the runtime environment 143Accessing resources using DI and JNDI 146Resource injection using @Resource 146The @Resource annotation in action 152Looking up resources and EJBs 155AOP in the EJB world: interceptors 157What is AOP? 157What are interceptors? 158Specifying interceptors 160Implementing business interceptors 162Lifecycle callback methods in the interceptor class 165Scheduling: the EJB 3 timer service 167What are timers? 168Using the timer service 169When to use EJB timers 174Summary 175Transactions and security 176Understanding transactions 177A transactional solution in ActionBazaar 178ACID properties 178Transaction management internals 181Two-phase commit 183Transaction management in EJB 184Container-managed transactions 185Snag-It ordering using CMT 185The @TransactionManagement annotation 186The @TransactionAttribute annotation 187Marking a CMT for rollback 191Transaction and exception handling 193Bean-managed transactions 196Snag-It ordering using BMT 196Getting a UserTransaction 198Using UserTransaction 199The pros and cons of BMT 201Exploring EJB security 202Authentication vs. authorization 202Users, groups, and roles 203A security problem in ActionBazaar 203EJB 3 and Java EE security 205Declarative security 208Using EJB programmatic security 210Summary 2133 Diving into the Java Persistence API (JPA) 215Implementing domain models 217Domain modeling and the JPA 218Introducing domain models 219The ActionBazaar problem domain 219Domain model actors 222The EJB 3 Java Persistence API 224Domain objects as Java classes 225Implementing domain objects with JPA 227The @Entity annotation 228Persisting entity data 229Specifying entity identity 233The Embeddable annotation 238Entity relationships 239@OneToOne 240@OneToMany and @ManyToOne 244@ManyToMany 247Summary 249Object-relational mapping 250The impedance mismatch 251Mapping objects to databases 252Introducing O/R mapping 257Mapping entities 258Specifying the table 261Mapping the columns 262Using @Enumerated 264Mapping CLOBs and BLOBs 265Mapping temporal types 265Mapping an entity to multiple tables 266Generating primary keys 267Mapping embeddable classes 270Mapping entity relationships 273Mapping one-to-one relationships 273One-to-many and many-to-one 278Many-to-many 281Mapping inheritance 284Single-table strategy 285Joined-tables strategy 287Table-per-class strategy 288Mapping polymorphic relationships 291Summary 292Manipulating entities with EntityManager 293Introducing the EntityManager 294The EntityManager interface 294The lifecycle of an entity 297Persistence contexts, scope, and the EntityManager 301Using the EntityManager in ActionBazaar 303Creating EntityManager instances 304Container-managed EntityManagers 305Application-managed EntityManager 308Managing persistence operations 313Persisting entities 314Retrieving entities by primary key 319Updating entities 325Deleting entities 328Controlling updates with flush 330Refreshing entities 331Entity lifecycle listeners 333Using an entity listener 334Default listener classes 336Listener class execution order and exclusion 337Entity operations best practices 338Summary 338Using the query API and JPQL to retrieve entities 340Introducing the query API 341The big picture 341Anatomy of a query 343Defining named queries 344Executing the queries 345Creating a query instance 346Working with the Query interface 347Specifying query hints 353Introducing JPQL 354Defining statement types 356Using the From clause 358Conditional expressions and operators 362Working with JPQL functions 367Using a Select clause 370Using aggregations 372Ordering the query result 374Using subqueries 374Joining entities 376Bulk updates and deletes 379Native SQL queries 380Using dynamic queries with native SQL 381Using a named native SQL query 382Summary 383Putting EJB 3 Into Action 385Packaging EJB 3 applications 387Packaging your applications 389Dissecting the Ear file 391Loading the Ear module 392Exploring class loading 394Class-loading basics 394Exposing the classic parent delegation model 396Class loading in Java EE applications 398Dependencies between Java EE modules 398Packaging session and message-driven beans 400Packaging EJB-JAR 400Deployment descriptors vs. annotations 402Overriding annotations with deployment descriptors 406Specifying default interceptor settings 407Using vendor-specific annotations and descriptors 408Packaging entities 410Exposing the persistence module 410Describing the persistence module with persistence.xml 411Performing O/R mapping with orm.xml 418Best practices and common deployment issues 422Packaging and deployment best practices 422Troubleshooting common deployment problems 423Summary 424Effectively integrating EJB 3 across your application tiers 425Design patterns and web technologies 426Presentation tier 427Using the Entity Access Object pattern 429Visiting the Session Facade pattern 434Accessing session beans from the web tier 439Accessing session beans using dependency injection 441Referencing session beans from helper classes 442Dealing with transactions 445Working with stateful session beans 446Using JPA from the web tier 447Using a container-managed entity manager 448Using an application-managed EntityManager with JTA transactions 450Accessing an application-managed EntityManager outside the container 451Summary 454Taming wild EJBs: performance and scalability 455Handling entity locking issues 457Understanding locking types 458Optimistic locking and entity versioning 461EntityManager and lock modes 462Improving entity performance 464Remodeling and schema redesign 464Tuning the JDBC layer 468Reducing database operations 470Improving query performance 474Caching 478Improving performance of EJB 3 components 483Session bean performance 484Improving MDB performance 487Clustering EJB applications 488Collocated architecture 489Load-balancing stateless session beans 490Clustering statefal session beans 493Entities and clustered cache 495Summary 496Migration and Interoperability Issues 497Migration to EJB 3 499Backward compatibility and interoperability with EJB 2 500Packaging EJB 2 and EJB 3 together 501Invoking EJB 2 from EJB 3 502Using EJB 3 from EJB 2 503Migrating session beans 505Converting interface and bean classes 506Resource usage 510Transactions and security settings 511Client applications 511Migrating message-driven beans 313Migrating CMP 2 entity beans to the EJB 3 JPA 513Redesign your domain model 514Using DTOs as entities 517Entity bean classes and interfaces 519Client applications 526Migrating JDBC DAOs to use the EJB 3 JPA 529Identifying entities 529Reworking a DAO implementation class to use the EntityManager API 530Swapping SQL queries for JPQL 531Helping O/R frameworks to use the EJB 3 JPA 533Approaches to migration 535Strategies 535Manual vs. automated 536Summary 536Exposing EJBs as web services 537What is a web service? 539Identifying web service components 541Web services styles 547Approaches to developing web services 548JAX-WS: Java EE 5 web services platform 549Introducing the web services platform 549Why choose EJB over a POJO for a web service? 550Developing EJB web services with JAX-WS 2.0 551Using the @WebService annotation 553Specifying the web service style with @SOAPBinding 555Using @WebMethod 557Using the @WebParam annotation 558Using the @WebResult annotation 560Using @OneWay and @HandlerChain 561Accessing a web service from an EJB 562Accessing the PlaceBid web service 562EJB as a web service client 564Best practices for web service development 565Summary 567EJB 3 and Spring 568Introducing the Spring framework 570Benefits of the Spring framework 571The inversion of control principle 571The separation of concerns principle 572Using JPA with Spring 573Building JPA EAOs for Spring 574Configuring Spring to use the JPA 577Combining the power of EJB 3 and Spring 581Developing Spring-enabled EJBs 581Using session beans from Spring beans 585Summary 585RMI and JNDI 587Reviewing relational databases 596Annotations reference 603Deployment descriptors reference 623Installing and configuring the Java EE 5 SDK 646Resources 660Index 663