Digital Edition

EJB 3.0 Preview
The advanced features Part 2

Last month's article on EJB 3.0 (Vol. 9, issue 11) focused primarily on the basic features of the specification. Part 2 dives much deeper into the specification to talk about more advanced features like dependency injection, dependent objects, secondary tables, and inheritance.

Dependency Injection
Dependency injection is the opposite of jndiContext.lookup(). The idea of dependency injection is that objects and services specify which resources and configuration values they require, and their container automagically injects these values. Dependency injection is supported for any session bean type and will also be supported in the greater J2EE specification in items such as servlets. This approach requires no JNDI lookup at all and can greatly simplify code. Let's look at an example.

public class ShoppingCartBean implements ShoppingCart
   @Inject private UserTransaction userTx;
   @Inject private EntityManager entityManager;

   private Petstore store;

   @EJB(name="petstore") public void setPetstore(Petstore store) { = store;

When an instance of ShoppingCartBean is allocated, the EJB container will look up the UserTransaction service and set the userTx variable. It will also get a reference to the EJB whose ejbName is "petstore" and call the setPetstore() method. Values can be injected either with an explicit field set or by calling a setter method. A great side effect of injection is that it becomes possible to test beans outside the context of a container. @Resource is another annotation for injecting things like DataSources and JMS connections.

Dependent Objects
The EJB 3.0 specification defines a full object/relational mapping for dependent value classes. You can map the properties of an aggregated value object in your entity to specific columns of the entity's table.

public class Address implements {

   private String street;
   private String state;
   private String city;

   public String getState() { return state; }

Your dependent value class can either have its properties defined as get/set methods or directly as fields. Next, define the mapping of a @DependentObject within your entity bean (see Listing 1).

Multi-Table Mappings
Many application developers find it necessary to map an entity bean to multiple tables within a database, especially when they have to map objects to a legacy data schema. EJB 3.0 provides mappings for this using the @SecondaryTable annotation.

@SecondaryTable(name="ADDRESS", join={@JoinColumn(name="address_id")})
public class Customer {
   @Column(name = "street", secondaryTable = "ADDRESS")
   public String getStreet()
      return street;

The @SecondaryTable is defined as a class annotation and specifies the table's name as well as the columns to use to join the main and subtable together. The @JoinColumns of the secondary table must map directly to the primary key of the entity.

To map a specific property to the secondary table, specify the secondaryTable annotation member value from the @Column annotation.

Entity Inheritance
Another missing feature in EJB 2.1 is the ability to support inheritance and map a complex class hierarchy to a relational database. EJB 3.0 supports inheritance and polymorphic queries. Three types of inheritance mapping strategies are supported: one table per class hierarchy (SINGLE_TABLE), a join table per subclass (JOINED), and a distinct table per class (TABLE_PER_CLASS). Only SINGLE_TABLE is required by the EJB 3.0 specification, so we'll focus on an example covering that.

The SINGLE_TABLE specifies that there should be one and only one table per class hierarchy. This table should have a column for each unique field for every class in the hierarchy. The table must have an additional column that identifies the object's type. By default, its type is a string with the default value being the fully qualified class name of the object stored. The @DiscriminatorColumn maps the object identity to a specific database column. The SINGLE_TABLE strategy is the optimal strategy for performance as the persistence engine doesn't have to do any complex joins when loading such an object. For example, say we have an Animal superclass entity bean, and a Dog subclass. The Java code would look like Listing 2.

The table mapping would be one gigantic table:

create table Animal (
ID Number,
TYPE varchar(255),
BREED varchar(255)

All entities that subclass from Animal can be queried polymorphically:

Query query = entityManager.createQuery("from Animal a where a.averageWeight > 10");

This query could return an instance of a Dog, Cat, Elephant, whatever entities that are currently defined in the Animal hierarchy.

Finally Usable
EJB 3.0 finally makes EJB persistence a reality. With EJB 2.1 entities you continually had to escape to direct JDBC, rely on vendor proprietary extensions, or move to an entirely different object/relational mapping strategy altogether. Features such as a full object relation mapping including a fully featured query language, inheritance, secondary tables, and dependent objects finally make the EJB specification.

With the use of annotations, XML deployment descriptors that have long been the bane of EJB developers can be completely removed if so desired by the developer.

All in all, EJB has come a long way since the 1.0 days and is morphing itself to the specifications of the community for which it was written.


  • JSR 220: Enterprise JavaBeans 3.0:
  • JBoss, EJB 3.0:
  • About Bill Burke
    Bill Burke is chief architect of JBoss Inc., member of the EJB3 expert group, and co-author of the JBoss 4.0 Workbook in O'Reilly's Enterprise JavaBeans, 4th Edition.

    In order to post a comment you need to be registered and logged in.

    Register | Sign-in

    Reader Feedback: Page 1 of 1

    Subscribe to the World's Most Powerful Newsletters


    The standardization of container runtimes and images has sparked the creation of an almost overwhelm...
    As DevOps methodologies expand their reach across the enterprise, organizations face the daunting ch...
    Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As au...
    The best way to leverage your Cloud Expo presence as a sponsor and exhibitor is to plan your news an...
    DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novemb...
    With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, @CloudEXPO an...
    DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, ...
    As you move to the cloud, your network should be efficient, secure, and easy to manage. An enterpris...
    DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing w...
    @DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novem...
    DXWorldEXPO LLC announced today that Dez Blanchfield joined the faculty of CloudEXPO's "10-Year Anni...
    There is a huge demand for responsive, real-time mobile and web experiences, but current architectur...
    DXWorldEXPO LLC announced today that "Miami Blockchain Event by FinTechEXPO" has announced that its ...
    Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will ...
    Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to ...
    "We started a Master of Science in business analytics - that's the hot topic. We serve the business ...
    "NetApp is known as a data management leader but we do a lot more than just data management on-prem ...
    We call it DevOps but much of the time there’s a lot more discussion about the needs and concerns of...
    The dynamic nature of the cloud means that change is a constant when it comes to modern cloud-based ...
    "Since we launched LinuxONE we learned a lot from our customers. More than anything what they respon...