java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4084
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Retrieving multiple BMP entity beans in a single step E-mail
User Rating: / 1
PoorBest 

Sometimes you may want to to retrieve multiple entity beans without performing multiple JNDI lookup calls. This can be made via finder methods of the home interface which returns a collection of EJB references.

Returning a collection of data requires you to add some specific helper methods to the EJB home interface. In the home interface of a bean, define a finder method that returns a java.util.Collection instance instead of a single instance of an entity bean.

In this tip we describe BMP bean; its finder method returns a Collection of the primary keys for the entity bean matching. The container will replace each primary key with the entity bean instance it represents.

A home interface with finder:

public interface PersonBeanHome extends EJBHome {
    
    public PersonBean create(PersonKey holding)
       throws CreateException, RemoteException;
    
    public PersonBean findByPrimaryKey(PersonKey primaryKey)
       throws FinderException, RemoteException;
    
    // our own finder method, searching persond by the first name:
    public Collection findByFirstName(String firstName)
       throws FinderException, RemoteException;
}

A corresponding bean code:

public abstract class PersonBean implements EntityBean {
    
    ...
    
    public Collection ejbFindByFirstName(String firstName)
            throws ObjectNotFoundException {
        
        ArrayList array = new ArrayList();
        
        try {
            
            Connection conn = getConnection();
            
            Statement st = conn.createStatement(
                    "SELECT firstName, lastName FROM personTable " +
                    "WHERE firstName = " + firstName);
            conn.executeQuery();
            
            ResultSet rs = ps.getResultSet();
            rs.first();
            
            while (rs.next()) {
                // we have a contructor there: PersonKey(firstName, lastName):
                array.add(new PersonKey(rs.getString(1), rs.getString(2)));
            }
            
        catch (SQLException sqe) {
            
            throw new EJBException(sqe);
            
        finally {
            // release DB-connection...
        }
        
        return array;
    }
    
    ...
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.