Generate Primary Key Values (EJB 2.1)

This article describes developing CMP bean that uses generation Primary key value feature for MySQL database. The document isn't intended to describe setup MySQL in App server 8.1.


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.

Setup database and App server

  1. create table:
       CREATE TABLE customer (
       id BIGINT(19) NOT NULL auto_increment, -- this column will be automat. generated
       lastName varchar(25) NOT NULL,
       firstName varchar(30) default NULL,
       PRIMARY KEY  (id))
  1. copy MySQL JDBC driver (mysql-connector-java-xxxx.jar) to $SUN_APP/domains/domain1/lib/ext
  2. restart server

Create EJB module

  1. create new EJB module CustomerModule
  2. generate CMP bean from customer table
  3. id column hasn't be mapped to CMP bean, select node representing id in project tab and invoke delete action
  4. open bean class in editor and remove id parameter from ejbCreate and ejbPost methods
  5. change create method's return parameter to java.lang.Object
  6. open CustomerLocalHome interface and remove id paramater form create method
  7. change parameter type of findByPrimaryKey method to java.lan.Object
  8. add new stateles session bean CustomerFacade with remote interface
  9. invoke Call EJB and select customer entity bean
  10. implement these business method and add these to remote interface:
    public long createCustomer(String lastName, String firstName) throws Exception {
        return ((Long)customerHome.create(lastName, firstName).getPrimaryKey()).longValue();

    public String getName(long id) throws Exception {
        CustomerLocal customer = customerHome.findByPrimaryKey(new Long(id));
        return customer.getFirstName() + " " + customer.getLastName();
  1. select connection pool node and change DataSource class name to: com.mysql.jdbc.jdbc2.optional.MysqlDataSource. See IZ.
  2. deploy module

Test CMP bean

  1. select CustomerFacadeBean in projcet tab under Sources and invoke Tools -> JUnit Test -> Create tests
  2. remove all testxxx methods
  3. implement setUp and two test methods
    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 testGetCustomerName() throws Exception {
        long id = customerFacade.createCustomer("Blaha", "Petr");
        assertEquals("Petr Blaha",customerFacade.getName(id));
  1. add appserv-rt.jar to Test classpath
  2. Run tests and see results


  • I get error JDO71030: There is no column in table myTable which can be used to support the servers implementation of unknown key classes. during deployment. This issue is caused by wrong configuration of your module. Check correct ejb-jar.xml and return type of create method, parameter of findByPrimaryKey.
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo