Generating Database Tables at Deployment

See Also

When developing CMP entity beans, if your only requirement is that the beans are persisted to a database and you are not particular about the structure of the database, you can configure the application such that the database and mappings will be generated by the application server. In this case, you can create your CMP entity beans, the bean fields, and the relationships in the IDE. You then configure the application to generate the database tables and mappings when you deploy the application.

To generate database tables at runtime:

  1. Make sure that your entity beans are set to use container-managed persistence and have their CMP fields and container-managed relationships correctly configured.
  2. In the Projects window, expand Configuration Files and double-click sun-ejb-jar.xml to open the file in the Visual Editor.
  3. Click the XML tab to open the XML editor.
  4. Specify the JNDI name for your database resource in the <jndi-name> property. Make sure that the name matches a database resource configured on your application server.
  5. Set the value of the <create-tables-at-deploy> element to True.

    To prevent an error when redeploying the application, set Drop Table At Undeployment to True. Alternatively, you can delete the tables manually in the Services window of the IDE.

  6. Make sure the database server in which you are generating the tables is running. If you are using the bundled Java DB server, choose Tools > Java DB Database > Start Server.
  7. Deploy your application by right-clicking the project node and choosing Run.

When you click Run, a table is generated for each CMP bean. In each table, a column is created for each CMP field and the relationships between the tables are generated based upon the relationships between your CMP beans. You can see the created tables under the Tables node for your database connection in the Services window.

See Also
About EJB 2.1 CMP Mappings
Defining a CMP Field
Mapping Relationship Fields
Generating CMP Entity Beans from a Database
Configuring CMP Mappings

Legal Notices