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.
- Unless you create
entity beans from a database connection, the IDE assumes you want to generate
the sun-cmp-mappings.xml file automatically. If you create CMP entity
beans using the New
Entity Bean template and manually set up your mappings by hand in a sun-cmp-mappings.xml
file, the IDE ignores your settings and configures the mappings during deployment.
To use your own sun-cmp-mappings.xml file, generate a CMP entity bean
from any database. You can then delete the bean. The IDE automatically switches
your project to pick up the existing sun-cmp-mappings.xml file.
To generate database tables at runtime:
- Make sure that your entity beans are set to use container-managed persistence
and have their CMP fields and container-managed relationships correctly configured.
- In the Projects window, expand Configuration Files and double-click sun-ejb-jar.xml
to open the file in the Visual Editor.
- Click the XML tab to open the XML editor.
- 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.
- 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.
- 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.
- 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