HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Industrial Strength Enterprise JavaBeans"
Volume: 3 Issue: 9, p. 16

	

Listing 1.
 
public interface HotelRoom 
extends javax.ejb.EJBObject { 

public void reserve (int numberOfDays) 
throws java.rmi.RemoteException; 

// <more business methods only> 
} 

public interface HotelRoomHome 
extends javax.ejb.EJBHome { 

public HotelRoom create (RoomIdentifier roomNo) 
throws java.rmi.RemoteException, 
javax.ejb.CreateException; 

public HotelRoom findByPrimaryKey 
(RoomIdentifier roomNo) 
throws java.rmi.RemoteException, 
javax.ejb.FinderException; 

// <more create or find* methods only> 
} 
  

public class HotelRoomImpl 
implements javax.ejb.EntityBean { 

/* 
* implement business methods: 
*/ 
public void reserve (int numberOfDays) { 
// <business logic only> 
} 
// other methods from the HotelRoom interface 
  

/* 
* implement javax.ejb.EntityBean methods: 
*/ 
public void ejbActivate() {} // nothing 
public void ejbPassivate() {} // nothing 
public void ejbLoad() {} // nothing 
public void ejbStore() {} // nothing 
public void ejbRemove() {} // nothing 
public void setEntityContext 
(javax.ejb.EntityContext ctx) { 
this.ctx = ctx; 
} 
public void unsetEntityContext() { 
this.ctx = null; 
} 

/* 
* implement ejbCreate, ejbPostCreate, ejbFind* 
* methods corresponding to those in the 
* HotelHome interface: 
*/ 
public void ejbCreate (RoomIdentifier roomNo) { 
room_number = roomNo; 
} 
public void ejbPostCreate 
(RoomIdentifier roomNo) { 
// do nothing 
} 
public RoomIdentifier ejbFindByPrimaryKey 
(RoomIdentifier roomNo) 
{ 
// translate input params to primary key 
// (in this case, no computation required): 
return roomNo; 
} 

/* 
* public fields to be container-managed 
*/ 
RoomIdentifier room_number; 
// other fields, such as dates reserved. 
  

/* 
* private fields for internal use only 
*/ 

// EntityContext can be used by the bean to 
// explicitly control transactions & security: 
private javax.ejb.EntityContext ctx; 
} 

/* 
* Use JNDI to connect to the home: 
*/ 
javax.naming.Context ctx = 
new javax.naming.InitialContext(); 
HotelRoomHome home = (HotelRoomHome) 
javax.rmi.PortableRemoteObject.narrow( 
ctx.lookup("applications/hotel/rooms"), 
HotelRoomHome.class); 

RoomIdentifier key = new RoomIdentifier("351B"); 

/* 
* create a new record in the database: 
*/ 
HotelRoom room = home.create(key); 

/* 
* or get an object representing existing data: 
*/ 
room = home.findByPrimaryKey(key); 

/* 
* invoke business methods: 
*/ 
room.reserve(3); // reserve the room for 3 days 

/* 
* remove the record from the database: 
*/ 
room.remove(); 
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.