当前页面: 开发资料首页 → Netbeans 专题 → Generating Primary Key Values in CMP Beans
摘要: The Sun Java System Application Server supports automatic primary key generation for EJB 1.1, 2.0, and 2.1 CMP beans. To specify automatic primary key generation, give the prim-key-class element in the ejb-jar-xml file the value java.lang.Object. CMP beans with automatically generated primary keys can participate in relationships with other CMP beans. The Sun Java System Application Server does not support database-generated primary key values.
If the database schema is created during deployment, the Sun Java System Application Server creates the schema with the primary key column, then generates unique values for the primary key column at runtime.
If the database schema is not created during deployment, the primary key column in the mapped table must be of type NUMERIC with a precision of 19 or more, and must not be mapped to any CMP field. The Sun Java System Application Server generates unique values for the primary key column at runtime.
Before you begin, you have to install Sun Java System Application Server Platform Edition 8.1 (download) on your computer. You cannot deploy enterprise applications to the bundled Tomcat server. You also have to register the application server in the IDE by choosing Tools > Server Manager.
This example also uses MySQL instead of PointBase as the database server. To learn how to set up MySQL in NetBeans and on the application server, see this document.
Our example uses a very simple database table that lists customers by ID, last name, and first name.
CREATE TABLE customer ( id BIGINT(19) NOT NULL, -- this column will be automat. populated lastName varchar(25) NOT NULL, firstName varchar(30) default NULL, PRIMARY KEY (id));
When you generate the CMP beans from the database connection, the IDE will automatically register the id column as the primary key and create a CMP field and finder method for it. This forces you to already know the primary key before you can run the ejbCreate method.
You need to edit the CMP bean to get the primary key from the application server instead and make the appropriate changes to the deployment descriptor and ejbCreate method.
public java.lang.Object ejbCreate(java.lang.String lastName, java.lang.String firstName) throws javax.ejb.CreateException { if (lastName == null) { throw new javax.ejb.CreateException("The field \"lastName\" must not be null"); } // TODO add additional validation code, throw CreateException if data is not valid setLastName(lastName); setFirstName(firstName); return null; } public void ejbPostCreate(java.lang.String lastName, java.lang.String firstName) { // TODO populate relationships here if appropriate }We've done the following:
... public org.bank.CustomerLocal create(java.lang.String lastName, java.lang.String firstName) throws javax.ejb.CreateException; ...
... org.bank.CustomerLocal findByPrimaryKey(java.lang.Object key) throws javax.ejb.FinderException; ...
Now we need to create a facade session bean that will access the CMP bean.
... private javax.ejb.SessionContext context; private CustomerLocalHome customerHome; ... public void ejbCreate() { customerHome = lookupCustomerBean(); } ...
public void createCustomer(String lastName, String firstName) throws Exception { customerHome.create(lastName, firstName); } public Collection getLastName(String firstName) throws Exception { ArrayList names = new ArrayList(); Iterator it = customerHome.findByLastName(firstName).iterator(); while(it.hasNext()){ names.add(((CustomerLocal)it.next()).getLastName()); } return names; }
When you create CMP beans from a database, the IDE automatically creates a connection pool and data source for the database connection. A known bug in NetBeans 4.1 is that the IDE generates connection pools for MySQL databases with the wrong datasource class name. You have to change the class name yourself.
Our EJB Module is now set up and ready to go. Let's put it through its paces by populating the database and writing a quick JUnit test case to test the two business methods.
... public class CustomerFacadeBeanTest extends TestCase { private CustomerFacadeRemote customerFacade; ...Note: If you the Source Editor complains that it cannot find CustomerFacadeRemote, just build the project by right-clicking the project node in the Projects window and choosing Build Project. The error disappears.
... protected void setUp() throws Exception { InitialContext ctx = new InitialContext(); Object obj = ctx.lookup("ejb/CustomerFacadeBean"); customerFacade = ((CustomerFacadeRemoteHome)PortableRemoteObject.narrow(obj, CustomerFacadeRemoteHome.class)).create(); } ...
... public void testCreateCustomer() throws Exception { System.out.println("PK: " + customerFacade.createCustomer("Foo","Bar")); } public void testGetLastName() throws Exception { long id = customerFacade.createCustomer("Joe", "User"); assertEquals(1, getLastName("name").length()); } ...
init: deps-jar: compile: Compiling 1 source file to C:\new\CustomerModule\build\test\classes compile-test: 03-Aug-2005 12:42:11 com.sun.corba.ee.spi.logging.LogWrapperBase doLog INFO: "IOP00710299: (INTERNAL) Successfully created IIOP listener on the specified host/port: all interfaces/1502" PK: 1123065661713 Testsuite: org.bank.CustomerFacadeBeanTest Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 3.855 sec ------------- Standard Output --------------- PK: 1123065661713 ------------- ---------------- --------------- ------------- Standard Error ----------------- 03-Aug-2005 12:42:11 com.sun.corba.ee.spi.logging.LogWrapperBase doLog INFO: "IOP00710299: (INTERNAL) Successfully created IIOP listener on the specified host/port: all interfaces/1502" ------------- ---------------- --------------- test-report: test: BUILD SUCCESSFUL (total time: 6 seconds)