Lookups and Searches

Before a client can make any changes to an object, it needs to find that object. There are three scenarios for getting a reference to a distributed component:

  • Looking it up by its unique identifier

  • Searching for it based on a set of criteria

  • Asking another related component for a reference to it

The last scenario is the simplest and begs the question of the other two. Specifically, given a reference to a Customer component, you should be able to get all of that Customer’s Account objects. How did you get a reference to that Customer in the first place?

EJB uses a kind of meta-component called a home to manage operations on a component as a class, including lookups, searches, and creates. Whether you call it a home or something else, a distributed database application needs some way to get access to the business objects stored on the server. You will take advantage of the home metaphor, but you’ll put a new spin on it.

Getting a reference to an object using its unique identifier is fairly simple. A find( ) method in the home accepts an Identifier and an objectID as parameters and returns a reference to the component identified by that objectID:

public abstract Account find(Identifier id,
                             long oid)
throws FindException;

Using the persistence library that will be discussed in Chapter 9, you can then search the persistence store for an object that has the specified objectID.

Performing searches on criteria other than a unique identifier gets more complex. First, because ...

Get Database Programming with JDBC & Java, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.